I am writing a threaded program, it has to be threaded, no way around this, if you are only gonna suggest I do not do this threaded pleases top reading now and do not reply.
I have one thread that has a buffer, it puts the request into a que, another thread picks up the request from the que, and fills the buffer (part of the request). The thread that made the request needs to wait until he buffer is full and then continue.
Here is the code I was thinking would do the trick, I cannot test it at the moment, I am wondering if it will work or if there is a better way.
my $Buffer; #The buffer that needs to be filled.
push(@Que, \$Buffer); #Put the buffer into the que, this is a line I p
+ut in for simplicty, the actual request is a lot more complicated.
while (1) #Infinite loop
{
threads->yield(); #Yield each iteration
lock($Buffer); #Lock the buffer, this way if data is still being p
+laced intot eh buffer in the other thread it will be locked there and
+ we cannot get data early. will it be unlocked for the next iteration
+? or is the loop not considered leaving it's own {} per iteration?
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!
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.