开发者

Make Thread sleep first before it runs

开发者 https://www.devze.com 2023-03-08 15:37 出处:网络
How can I make my thread sleep first before it runs? I know how to get sleep to work, however, whenever my program is run, the thread immediately runs. I want it to WAIT on开发者_如何学Cce it is first

How can I make my thread sleep first before it runs? I know how to get sleep to work, however, whenever my program is run, the thread immediately runs. I want it to WAIT on开发者_如何学Cce it is first created to start running. (I am using handlers)


You cannot control when threads are scheduled. If you want it to go to sleep, have the first statement in the thread subroutine do a wait on a condition or something like that and when you are ready you can broadcast to that condition. In pseudo-code:

get-lock
if (we-are-still-supposed-to-sleep)
  pthread_cond_wait()
release-lock

I suppose you could have the parent hold the lock while creating the children and then all they have to do is:

get-lock
release-lock

and avoid the condition thing.


What OS? Windoze allows you to create threads in a suspended state. When you have loaded up the thread fields in the ctor, you can resume the thread. Failing that, pass some synchro object in the thread start parameter for the new thread to wait on.

Rgds, Martin.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号