Operating System - HP-UX
1827214 Members
2646 Online
109716 Solutions
New Discussion

How can i implement something like poll()

 
SOLVED
Go to solution
Victor.Lin_1
Occasional Advisor

How can i implement something like poll()

In my program, a thread write data into a data queue (designed by myself, not message queue), another thread read from it. Because there is someother things to do, I can not block into reading the queue.
But polling the queue is wasting CPU time if the queue is empty. I want to implement something like poll() on the data queue. If the queue is empty and there is nothing to do, reading thread registers a readable event on it and block. After the data queue is writed and is no more empty, reading thread is waked up.
Is this idea naive ?
7 REPLIES 7
Rainer von Bongartz
Honored Contributor

Re: How can i implement something like poll()

You can build this functionality around the select () system call.

see man 2 select

Regards
Rainer

He's a real UNIX Man, sitting in his UNIX LAN making all his UNIX plans for nobody ...
Victor.Lin_1
Occasional Advisor

Re: How can i implement something like poll()

Really ? select() can only deal with file describer. How can it deal with semaphore(the data queue works based on semaphore)?
Rainer von Bongartz
Honored Contributor
Solution

Re: How can i implement something like poll()

ok, in this case you can't use select()

He's a real UNIX Man, sitting in his UNIX LAN making all his UNIX plans for nobody ...
rick jones
Honored Contributor

Re: How can i implement something like poll()

i'm a little confused - how is it you cannot block trying to read something from the empty queue, but you can block in a poll-like call?
there is no rest for the wicked yet the virtuous have no pillows
A. Clay Stephenson
Acclaimed Contributor

Re: How can i implement something like poll()

Hi Victor,

The situation you describe seems to be a perfect fit for message queues. msgrcv() and msgsnd() will do just the king of blocking you seem to be looking for - unless you need to transfer fairly large amounts of data. You may be trying to avoid the overhead of the copies or you may need to transfer more than the maximum allowed message size. I may have a solution: Use message queues essentially as semaphores to handle the blocking but do the
actual data transfers (or pointer references to avoid the copy) in shared memory. This should give you the best of both worlds.

Food for thought, Clay
If it ain't broke, I can fix that.
rick jones
Honored Contributor

Re: How can i implement something like poll()

ignore my question - i misinterpreted the text of the original problem statement.

if there is nothing on your queue, you could grab a mutext and add to a notification queue the thread id and signal you want. then when something later adds to your queue, it can notice that there were notifications regsitered and send your original thread a signal via pthread_kill. of course, this presumes the original thread registers a signal handler first :)

a variation would be that the original thread could register some form of FD instead of a signal under that notification mutex. say perhaps one end of a loopback TCP connection or a unix domain socket or something. then the thread that adds to the queue will write something - perhaps just a junk byte - perhaps something with more meaning - to the connection. the other thread then will see this if they are in a select() or poll() for other FD's
there is no rest for the wicked yet the virtuous have no pillows
Victor.Lin_1
Occasional Advisor

Re: How can i implement something like poll()

I am sorry. Because I can access Internet only two days one week in the company I am working in. Sometimes I can not reply and assign points in time.