在使用pthread_create创建的线程之间读取和写入管道时,是否需要关闭 fds
Do I need to close fds when reading and writing to the pipe among threads created using pthread_create?
我正在开发一个客户端服务器应用程序。下面是来自客户端的代码。
pipe_input
,pipe_output
是共享变量。
int fds[2];
if (pipe(fds)) {
printf("pipe creation failed");
} else {
pipe_input = fds[0];
pipe_output = fds[1];
reader_thread_created = true;
r = pthread_create(&reader_thread_id,0,reader_thread,this);
}
void* reader_thread(void *input)
{
unsigned char id;
int n;
while (1) {
n = read(pipe_input , &id, 1);
if (1 == n) {
//process
}if ((n < 0) ) {
printf("ERROR: read from pipe failed");
break;
}
}
printf("reader thread stop");
return 0;
}
还有一个编写器线程,它从服务器写入事件更改的数据。
void notify_client_on_event_change(char id)
{
int n;
n= write(pipe_output, &id, 1);
printf("message written to pipe done ");
}
我的问题是我是否需要关闭读取器线程中的写入端并在写入器线程的情况下关闭读取结束。在析构函数中,我正在等待读取器线程退出,但有时它不会从读取器线程退出。
[...]我是否需要关闭读取器线程中的写入端,并在写入器线程的情况下关闭读取端[?
由于这些 fds ">是共享的",因此在一个线程中关闭它们将关闭所有线程的它们。我怀疑这不是你想要的。
相关文章:
- 在提升multi_index容器中,是否定义了"default index"?
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 检查输入是否不是整数或数字
- 如何使客户端在将数据写入 C++ 管道之前检查服务器是否完成了从管道的读取操作
- 确定文件句柄是否为管道句柄
- C 是否可以加密通过命名管道发送的数据
- 检查文件是否是C++中的命名管道 (fifo)
- 是否可以使用C++流类缓冲来自管道的读取
- 在使用pthread_create创建的线程之间读取和写入管道时,是否需要关闭 fds
- 如何检查std::cin是否与端子或管道关联
- 如何检查文件指针/描述符/句柄是否关联到同一个文件/管道/终端
- 如何判断管道上是否有新数据
- 使用匿名管道是否为线程间通信引入了内存屏障
- pclose() 返回管道的终止状态是否在所有平台上都向左移动了 8 位?
- 有没有办法检查外部数据是否通过管道传输到程序或程序是否自行运行
- 命名管道在c++ Linux检查缓冲区是否已满
- 是否有一个进程内的本地管道在Qt
- 是否有像WaitNamedPipe这样的函数或在c++ /linux上实现此功能的方法?(因此进程不会在管道上阻塞无限时
- 是否有相当于Windows命名管道的Mac OS X。
- 从安全角度来看,在 Windows 中使用管道是否被认为是危险的