JustAMartin
JustAMartin

Reputation: 13733

How to combine two different waiting mechanisms in a single POSIX thread?

I'm developing an application using POSIX threads.

The thread in question is receiving incoming data. Most of the time it is sleeping and waiting for something to happen.

But there are two issues here:

  1. in some situations the thread has to be fully stopped; and normally a simple POSIX condition signal should be good enough for the purpose.
  2. the thread is waiting for events and data from TCP sockets using select() and also waiting for events from virtual "ports", which might have no support for select() (e.g. some specific Bluetooth / USB connections etc.). I will implement a notification mechanism for these ports - again, a simple POSIX condition signal should be enough.

Now the question is - how to combine waiting on both select() and pthread_cond_wait() in a single runloop of a thread to wake it up on all of these events?

Number of ports (both TCP and "virtual") is expected to be low, under 5.

The solution should be reliable and work on Linux and BSD-like platforms (including Android and iOS).

Upvotes: 1

Views: 55

Answers (1)

user3458
user3458

Reputation:

Use select() for everything. Use pipe() for things that do not natively map to an fd.

Upvotes: 2

Related Questions