如何在 C++ 中解除对 Linux 系统调用的阻塞?例如,如果我在线程中有以下循环 :
bool shouldRun;
void foo(){
while(shouldRun){
length = read( file_descriptor, buffer, buffer_length);
//do something
}
return;
}
main(){
shouldRun = true;
std::thread myThread(foo);
//do some other stuff
shouldRun = false;
//-->here I want to unblock "read" in foo
}
一般来说,read方法应该是阻塞的,我只想在需要的时候解除阻塞。
最佳答案
通话
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
这将使文件描述符成为非阻塞的。
关于c++ - 如何阻止 Linux "read"系统调用阻塞?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25061375/