UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » Threads
  • » use of pthread_cond_timed_wait while reading from socket

#1 2008-02-12 05:44 AM

meena_selvam
Member
Registered: 2008-02-10
Posts: 5

Re: use of pthread_cond_timed_wait while reading from socket

How I can use the pthread_cond_timed_wait() to wait on a condition that occurs, but at the same time do some routine

processing.

For example,


thread 1:

while (1){
lock()
while (condition == false)
ret = pthread_cond_timed_wait (..,timeout)
if (ret == timeout)
{
// Do routine actions required (on timeout)
read from socket
process command that was read from socket
unlock();
}
if (ret == 0)
{
// do the action for signalling
unlock()
}

}

thread 2:

lock()
set some condition which thread1 should be aware of
unlock()
pthread_cond_signal()


.

Now thread1 should do a different action, like closing the socket and do other cleanup, when thread2 signals. I have added the pthread_cond_timed_wait in thread1 such that it is not being blocked always, ie to be able to do the
reading also happen. Reading from socket happens as often as the value of timeout say ( 100 milli seconds). It will block for sometime in case no msgs were in.


blocking read will cause delay in signalling the condition, because it will hold the lock during that time, and thread 2 will be blocked when it is about to signal the condition. So this causes unnecessary delay in signaling the condition. How can I get around this?

Offline

#2 2008-02-12 11:52 AM

biologz
Administrator
From: Puking on the pavement
Registered: 2005-11-02
Posts: 396

Re: use of pthread_cond_timed_wait while reading from socket

I deleted you post in the Networking section because it was just the same as this one ok?

Plus in this one you used CODE TAGS which is good :D


gethostbyintuition() is still a dream of mine

                                                 -- quoted from bash

Offline

#3 2008-02-12 01:38 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,829
Website

Re: use of pthread_cond_timed_wait while reading from socket

I'm not fully sure I understand the question, but why don't you simply use non-blocking
I/O, if the blocking read is causing too long delays?  Or, at least verify that there's
some data to read first before trying to, using select()/poll()/etc...

In fact, in a case like this where you want the thread to process socket I/O as well
as wait for a signal from another thread, instead of a pthread_cond_t, I'd simply use
a pipe() or socketpair() between the two threads for signalling...  Then, you could
throw that FD into your select()/poll() checking, and handle it right along with the
socket I/O...  The signalling would consist of thread2 simply writing a message into
the pipe/socket, which would make it select() as readable to thread1 (which would
then read it to clear it out)...

Offline

#4 2008-02-12 06:33 PM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: use of pthread_cond_timed_wait while reading from socket

Just a couple of thoughts looking at the example you provided...

Even if you made your socket(s) non-blocking you are going to have multiple threads delving down into the system / kernel doing selects to determine if the socket associated with each thread is readable... followed by another dive down into the system to actually perform the read operation...

Now there is nothing technically 'wrong' with this but there are a couple of issues:

* select is a 'slow' ( io bound ) operation and may very well cause a context switch between threads - potentially wasting cpu cycles if a lot of threads happen to enter this area of code at the same time...

* the read ops are also obviously io bound and may have the same side effect as the select operation call...

* it looks like you are missing some brackets in the code - as I am uncertain what task the while ( condition == false ) loop against the condition variable wait is performing... (?)

Anyway... a suggestion is...

Have a central / single thread that works as a data pump... and which has the sole duty of checking the individual sockets for readability and actually performs the read operation - storing incoming data into an application level buffer... since you would be checking multiple sockets every time you called select - this would decrease the number of times select is called and save a lot of CPU cycles when the system is under heavy load / has a lot of connections...

You could also increase the priority of the 'data pump' thread to a higher level than the other threads in the application... this is something that would have little effect with your current setup - as having a lot of reading threads all with their priority boosted is going to gain you nothing more than potentially a higher rate and number of thread context switches... where as with a single thread perform those tasks you - may - see a higher level of throughput...

Oh... and finally... you would create a a set of predicate / flag variables that - when the threads waiting against the condition variable were signaled or timed out - could check to see if the data pump thread had read / written data and see what action(s) they needed to perform...

One catch with this setup... the potential exists to do a very bad job when coding it... especially as pertains to the 'buffering' of data... so you should probably be careful about the number of times you allocate / free memory in the data pump thread... and worry about overhead when copying from the data pump queue to the child threads... which means an implementation of an efficient block allocated and memory re-use scheme would be a good thing... but also another item you would have to code and test...


Michael


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

#5 2008-02-12 06:36 PM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: use of pthread_cond_timed_wait while reading from socket

Just one additional note...

While I focused on reading the sockets in the above message... the same scheme could be utilized for writing operations also...  I simply focused on the read component as that is the operation your code fragment was performing...


Michael


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

#6 2008-09-29 10:23 PM

Lihualee
Guest

Re: use of pthread_cond_timed_wait while reading from socket

just my two centsbump

  • Index
  • » Threads
  • » use of pthread_cond_timed_wait while reading from socket

Board footer

Powered by FluxBB