Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: need a thread to wait until a buffer is full.

by archfool (Monk)
on Jul 24, 2007 at 21:42 UTC ( [id://628581]=note: print w/replies, xml ) Need Help??


in reply to need a thread to wait until a buffer is full.

Have you looked into Thread::Queue? I believe it handles the mutexes and locking for you. It can be :shared between threads. You ->enqueue your data in one thread, and ->dequeue (blocking) in the other thread. (Or ->dequeue_nb for non-blocking).
  • Comment on Re: need a thread to wait until a buffer is full.

Replies are listed 'Best First'.
Re^2: need a thread to wait until a buffer is full.
by exodist (Monk) on Jul 25, 2007 at 07:27 UTC
    Ok, here is a better way to ask my question:

    I have 2 threads, one creates shared variable that acts as a buffer, it puts a reference to the buffer in an array, then it waits until a second thread puts something into the buffer before continuing.

    The second thread will pop/shift stuff out of the array every once in a while, fill the buffers according to some request that is sent with them.

    I need to make sure the first thread waits until there is something in the buffer, I do not know when the buffer is going to be filled, and I have no idea how much data will be in the buffer when it is done, but it will be binary data, so it can be ANYTHING.

    My idea was to create a loop in the first thread where it does the following (not necessarily in this order)
    • Lock the buffer, this way if it is being written to we wait instead of assuming the other thread has finished with it.
    • If the buffer contains data continue
    • If the buffer is still empty unlock it I know to unlock it it needs to fall out of scope, ie end of {} block... I am not sure if a loop iteration counts as ending the scope.
    • Yield to other thread so it can get to the buffer.

    this is a loop, I figure the yield should come first since the first iteration will almost certainly be too soon. then it locks it, once it obtains the lock it checks it, if it is empty we reach the end of the loop. this is the part that worries me: at the end of the loop, the loop starts over, I am not sure if a loop iterating a second time will release the lock or not, I am sure it is, but it would be hard to debug if not.
    so the code I get to do this is roughly this:
    while (1) #Infinite loop { threads->yield(); #Yield each iteration lock($Buffer); #Lock buffer to prevent taking a partialy filled bu +ffer. last if ($Buffer); #If the buffer is filled continue. }

    --------------------------------------

    I would rather take 30 minutes to re-invent the wheel then take 30 days to learn how to use someone else's. Before pestering me about my re-invention prepare to defend yourself with a way of learning how to use the wheel in less time than it takes for me to make one, one that I might add is specialized to my specific task!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://628581]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2024-04-23 16:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found