(vc++)我用createpipe创建了一个匿名管道,并把读句柄传给子进程。我想在程序中用
PeekNamedPipe(hReadPipe,NULL,NULL,NULL,&tempdword,0);看管道内还有多少字节的信息,结果函数就阻塞在那了。这是为什么
我是这么写的
SECURITY_ATTRIBUTES sa;
sa.nLength=sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor=NULL;
sa.bInheritHandle=true;
CreatePipe(&hReadPipe,&hMyWritePipe,&sa,0);
CreatePipe(&hMyReadPipe,&hWritePipe,&sa,0);
STARTUPINFO si;
PROCESS_INFORMATION pi;
si.cb = sizeof(STARTUPINFO);
GetStartupInfo(&si);
si.hStdOutput = hWritePipe;
si.hStdInput = hReadPipe;
si.wShowWindow = SW_HIDE;
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
CreateProcess((LPSTR)(LPCTSTR)exepath,NULL,NULL,NULL,TRUE,NULL,NULL,NULL,&si,&pi));
在大部分情况下
PeekNamedPipe(hReadPipe,NULL,NULL,NULL,&tempdword,&nextword);
不会阻塞,但是某些情况下就会,不知道为什么?谢谢~
PeekNamedPipe 阻塞
答案:2 悬赏:70 手机版
解决时间 2021-01-29 01:27
- 提问者网友:wodetian
- 2021-01-28 03:32
最佳答案
- 五星知识达人网友:行路难
- 2021-01-28 03:48
Note :
The PeekNamedPipe function can block thread execution the same way any I/O function can when called on a synchronous handle in a multi-threaded application.
To avoid this condition, use a pipe handle created forasynchronous I/O.
The PeekNamedPipe() problem on XP only happens when another thread has entered ReadFile() on the read end of the pipe and has not yet returned. Namely, a ReadFile() blocked on the read end of the pipe in one thread will cause any subsequent PeekNamedPipe() in a second thread to also block -- until ReadFile() in the first thread returns.
英语不好,就不翻译出来了。看看对你有没有帮助。
The PeekNamedPipe function can block thread execution the same way any I/O function can when called on a synchronous handle in a multi-threaded application.
To avoid this condition, use a pipe handle created forasynchronous I/O.
The PeekNamedPipe() problem on XP only happens when another thread has entered ReadFile() on the read end of the pipe and has not yet returned. Namely, a ReadFile() blocked on the read end of the pipe in one thread will cause any subsequent PeekNamedPipe() in a second thread to also block -- until ReadFile() in the first thread returns.
英语不好,就不翻译出来了。看看对你有没有帮助。
全部回答
- 1楼网友:撞了怀
- 2021-01-28 04:36
PeekNamedPipe(hReadPipe,NULL,NULL,NULL,&tempdword,&nextword);
return tempdword+nextword;
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯