Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/155.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 释放模式下的boost线程崩溃_C++_Multithreading_Boost_Boost Thread - Fatal编程技术网

C++ 释放模式下的boost线程崩溃

C++ 释放模式下的boost线程崩溃,c++,multithreading,boost,boost-thread,C++,Multithreading,Boost,Boost Thread,我是boost新手,尝试在单独的线程中实现自由函数、静态函数和成员函数。它在调试模式下运行良好,但在发布模式下会崩溃。通常它表示未初始化的数组或值,但我找不到问题 class test { public: static void func2() { cout<< "function2"<<endl; } void func3(string msg) { cout<<msg<<endl;

我是boost新手,尝试在单独的线程中实现自由函数、静态函数和成员函数。它在调试模式下运行良好,但在发布模式下会崩溃。通常它表示未初始化的数组或值,但我找不到问题

class test {
public:
    static void func2() {
        cout<< "function2"<<endl;
    }

    void func3(string msg) {
        cout<<msg<<endl;
    }

    void operate() {
        // Constructs the new thread and runs it. Does not block execution. 
        thread t2(&test::func2);   // static function               
        thread t3(boost::bind(&test::func3,this,"function3"));  // member function

        //Makes the main thread wait for the new thread to finish execution, therefore blocks its own execution.
        t2.join();
        t3.join();
    }
};

void func1(string msg) {
    cout<<msg<<endl;
}

int main() {
    test example;
    example.operate();
    thread t1(&func1,"function1"); // free function
    t1.join();
    return 0;
}
类测试{
公众:
静态void func2(){

cout一个简单的解决方法是使用互斥来保证cout只使用一次

std::mutex mut;

void log(const std::string& ref)
{
    std::lock_guard<std::mutex> lock(mut);
    std::cout<<ref<<std::endl;
}
有三件事需要注意:

  • 我正在通过常量ref传递字符串
  • 我不再使用boost绑定了
  • 您仍然可以直接使用cout,因此,如果您想在编译时阻止它,则需要在单独的单元(.h+.cpp)中声明日志函数,并删除主程序的
    #include

希望这会有所帮助,

FWIW,在发布模式下在MSVC10中编译时运行良好。但是,请注意,
std::cout
不是线程安全的,因此您有争用条件。在一些
cout
实现中,此争用条件可能会导致崩溃。您正在尝试使用哪个平台,如果是linux,您是否链接到pthread库也处于发布模式?(在gcc 4.9.0和boost 1.55.0中试用过,在没有pthread的情况下崩溃,在pthread下工作)您的程序崩溃,因为STL流不是线程安全的。
class test {
public:
    static void func2() {
        log("function2");
    }

    void func3(const std::string & msg) {
        log(msg);
    }

    void operate() {
        // Constructs the new thread and runs it. Does not block execution. 
        std::thread t2(&test::func2);   // static function               
        std::thread t3(&test::func3,this,"function3");  // member function

        //Makes the main thread wait for the new thread to finish execution, therefore blocks its own execution.
        t2.join();
        t3.join();
    }
};