0
我錯過了什麼嗎?管道和選擇:示例代碼不起作用
我想通過在另一個線程中調用寫入來選擇出來......它從來沒有出來選擇。
代碼在OSX雪中測試。
fd_set rio,wio;
int pfd [2];
空隙測試(INT SLEEP_TIME)
{
睡眠(SLEEP_TIME);
char buf[] = "1";
write(pfd[1], buf, 1);
}
INT主(INT ARGC,CHAR *的argv [])
{
char buff[80];
int ended = 0;
pipe(pfd);
FD_ZERO(&rio);
FD_ZERO(&wio);
FD_SET(pfd[1], &wio);
FD_SET(pfd[0], &rio);
pthread_t tid; /* the thread identifier */
pthread_attr_t attr; /* set of thread attributes */
pthread_attr_init(&attr);
pthread_create(tid, NULL, test, 3);
while (!ended)
{
// Check my numbers ... they do not go over 1 ... so 2
if (select(2, &rio, &wio, NULL, 0) < 0)
perror("select");
else
{
if (FD_ISSET(pfd[1], &wio))
{
if ((read(pfd[0], &buff, 80))<0)
perror("read");
ended = 1;
}
}
}