gpt4 book ai didi

c++似乎变量不具有多线程的 "flush "

转载 作者:行者123 更新时间:2023-11-28 00:53:26 26 4
gpt4 key购买 nike

is_master_def:

volatile bool is_master_;

is_master_ 值被另一个线程设置为 true,但似乎 is_master_ 值 dosnt 刷新(它没有计算出发生 fatal error ...) .如果我添加 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();
}
}

最佳答案

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

Maybe it does, but because you don't use a newline your output stream doesn't flush.

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

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

关于c++似乎变量不具有多线程的 "flush ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12768066/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com