开发者

Posix thread communication Linux

I have a worker thread which is alive from the start of the programme and would consistently take an object from a queue to process. I am wondering wha开发者_JS百科t is the best way to block the thread? The object would be pushed into the queue at about magnitude of every tens of microseconds( between 10 - 100 micro seconds). Should I set the thread to sleep at a constant period or should I work out some signalling mechanism between the threads? I would like mainly focusing on the performance issue. Any ideas?

Thanks.


On the other hand you could use condition variables as long as it is generic feature of pthreads. Condition variables is designed upon pthread mutexes so they are very effective sync primitives (depending on the actual platform of course).

Follow them.


Posix message queues looks like a good candidate, if your data is not too big. You can also use a POSIX semaphore :

The producer thread put data on a queue, and do a sem_post
The consumer thread wait using sem_wait, and remove data from the queue.

It's easier to use IMO than condition variable. Of course you need to protect your queue. Depending on the size of the object, it might be more suited than message queues, but you need to implement your own queue.

Both can be used between process instead of thread. Should you decide to use process instead of thread, you could keep your synchronisation mechanism, which is not the case with condition variables.


Use POSIX message queues in blocking mode (mq_open, etc.), which is really simple, and see if they satisfy your performance requirements. If not, ask another question :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜