7hacker
7hacker

Reputation: 1978

Perl ithreads: Do some math instead of sleeping

I am using perl ithreads and things work fine, unless I decide to have threads sleep.

Lets say my routine thread_job is passed as an entry for several threads to start running concurrently.

thread_job()
{
...
sleep 2;

#do other stuff here

}

If I dont have a sleep I have no issues with the threads running and they do their tasks fine. If I add a sleep, my script hangs. I am running this off a windows command prompt, if that helps.

Since I do need to sleep and Im guessing there's an issue with using this sleep on my current setup, I intend to have the thread do something, for a while, instead of sleeping. Is there any such mathematical operation which I could perform?

Upvotes: 1

Views: 715

Answers (2)

Cat
Cat

Reputation: 11

Calling sleep() blocks the entire process (that is all the threads).

You can instead block a single thread by calling select(). Do something like this:

thread_job() {
...
$delay = 2;
select(undef, undef, undef, $delay);
...
}

Upvotes: 1

Ether
Ether

Reputation: 53966

Try using Win32::Sleep instead. (Note that it takes milliseconds as an argument, not seconds.)

Upvotes: 2

Related Questions