Andy Shulman
Andy Shulman

Reputation: 1925

Sleeping a PThread other than the one doing the calling

So I have a bunch of pthreads, where one is the "main" thread and determines if a worker thread should be running or sleeping. But the POSIX definition for sleep says that The sleep() function shall cause the calling thread to be suspended from execution...

Obviously I could do something clumsy like have each worker thread check to see if a flag is set, but I'm looking for something a little better. I'm hoping I'm missing something obvious, because this is throwing a wrench in my plans.

Upvotes: 0

Views: 194

Answers (1)

Martin James
Martin James

Reputation: 24857

If you're hacking Cilk anyway, I guess you can do whatever you want

How about having each pthread acquire a semaphore unit before dequeueing, (or stealing), a work object, and releasing it after doing the work? There may be a little latency, sure, but the number of threads available to do work will match the number of units signaled to the semaphore. To reduce the number of available threads by N from your control thread, wait for and acquire N units, so choking off N work threads. To start 'em again, signal N units.

Would this work for your system?

Rgds, Martin

Upvotes: 1

Related Questions