GZGQ.COM
welcome to my space
X
Search:  
 HOME   use of pthread_cond_timed_wait while reading from socket
use of pthread_cond_timed_wait while reading from socket
Published by: jane 2009-01-08
Welcome to:gzgq.com

  • 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?


  • 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


  • 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)...


  • 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


  • 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





  • Traditional University or MLM University? You Choose
    15 Questions to Ask Your Software Vendor

    You are looking at:gzgq.com's use of pthread_cond_timed_wait while reading from socket, click gzgq.com to home
  • jury duty
  • hhonors points for aa miles how long can it take
  • new service jfk zrh
  • the concorde
  • connection time mia
  • health choice miles
  • question on twa upgrades for aa plts
  • how to get comped for platinum
  • how i got money magazine to post miles
  • sea nrt hkg advice
  • dumbest net saaver sale ever
  • aa to begin jfk zurich service on oct 28
  • oag online now free to use useful in these times
  •  
  • million mile spiff
  • aolaadvantage survey i got into one
  • flown miles or dollars spent
  • 65 miles 500 mile certificate
  • why do they make you register for promos
  • are there any visa debit cards to earn aadvantage miles
  • discount tix for codeshares possible
  • mci
  • just made exp
  • 500 miles for 5 donation new aa promo
  • liberty mutual still giving miles
  • clr on boarding pass
  • new 25 000 miles promo
  • planaahead award embargo dates
  •  Homepage | Add to favorites | Contact us | Exchange links | LOGIN | Site map | 
    Copyright© 2008 gzgq.com        Site made:CFZ