c++ - 如何阻止 Linux "read"系统调用阻塞?

标签 c++ linux system-calls

如何在 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/

相关文章:

linux - kexec_load 是如何使用的?

c++ - 使用 ODBC 驱动程序时 Qt 应用程序崩溃 (macOS)

c++ - std::make_map?统一初始化

c++ - 单 channel 图像的滑动窗口标准过滤器

c++ - HashMap 错误: no match for call to ‘(const __gnu_cxx::

后跟 exec 时的克隆、 fork 、vfork 行为

linux - 32位Linux汇编代码

阻塞模式下的 Linux TCP 套接字

linux - 进程的内存详细信息

python - Linux epoll 系统调用,等待数据可用