c ++似乎变量不会与多线程“刷新”

时间:2012-10-07 10:40:11

标签: c++ multithreading

is_master_ def:

volatile bool is_master_;
另一个线程将

is_master _ 值设置为true,但似乎 is_master _ 值dosnt flush(它没有发生致命错误已发生...) 。如果我添加cout<< “foo”<

void MasterSlaveSynchronize::validateSingleMaster(){
    if(is_master_){
        cout << "FATAL ERROR HAS OCCURRED BOTH MASTER";
        if(!is_leader_master_){
            cout << "CHOSE AS VICTIM IN MASTER-MATSER. SET THIS HOST AS SLAVE";
            is_master_ = false;
        }
    }
}

来电者代码:

while(1){
        int n = recvfrom(sockId, buf, HEARBEAT_SIZE, 0, (struct sockaddr *) &from,
                &length);
        if (n < 0) {
            REGISTER_ERROR("Failed to recieved hearbeat");
        } else {
            gettimeofday(&instance_->last_hearbeat_got_, NULL);
            instance_->validateSingleMaster();
        }
}

1 个答案:

答案 0 :(得分:3)

您希望我发表评论作为答案:

  

可能确实如此,但是因为你没有使用换行符输出流   没有冲洗。

这种行为在这里得到了相当好的解释:

Why does printf not flush after the call unless a newline is in the format string?