Waiting on multiple events C++

前端 未结 4 562
后悔当初
后悔当初 2021-02-10 05:37

Is there a recommended way to wait on multiple inputs. For example I would like my program to be able to receive input from 3 sources:

Listen on a thread condition e.g.

4条回答
  •  自闭症患者
    2021-02-10 06:34

    You can listen on multiple file descriptors without using multiple threads using the select(2) system call. You can use pthread_cond_timedwait to wait on a condition variable with a timeout, such that you don't wait more than a particular amount of time.

    I think it's highly unusual to want to simultaneously wait on either a condition variable or a file descriptor of some sort -- if you're absolutely sure that that's what you want to do, you'll have to use multiple threads, with one thread calling either pthread_cond_wait/pthread_cond_timedwait, and the other thread calling select or some other I/O function.

提交回复
热议问题