相关疑难解决方法(0)

赋值运算符'='原子?

我正在使用全局变量实现线程间通信.

//global var
volatile bool is_true = true;

//thread 1
void thread_1()
{
    while(1){
        int rint = rand() % 10;
        if(is_true) {
            cout << "thread_1: "<< rint <<endl;  //thread_1 prints some stuff
            if(rint == 3)
                is_true = false;  //here, tells thread_2 to start printing stuff
        }
    }
}

//thread 2
void thread_2()
{
    while(1){
        int rint = rand() % 10;
        if(! is_true) {  //if is_true == false
            cout << "thread_1: "<< rint <<endl;  //thread_2 prints some stuff
            if(rint == 7) …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading communication thread-safety visual-c++

24
推荐指数
2
解决办法
1万
查看次数