Page 1 of 1

Any way to block a thread until another has started?

Posted: Sun Feb 06, 2011 11:16 am
by JetForMe
My main thread sets up some other threads (starts them), and then needs to send them messages. The problem is, it tries to send the message too soon, and the threads haven't yet started.

Is it possible for a thread to block on a message send to a thread that hasn't started yet? Right now, I only know how to get the current thread,which means I don't even know the thread until it has entered its entry point, at which point i call chThdSelf() and store the result.

Is there a way to get the Thread* object from a thread's working area, and send it a message after having called chThdCreateStatic, but perhaps before the thread has begun executing, and just have that call block until the thread starts up and can receive the message?

TIA!

Re: Any way to block a thread until another has started?

Posted: Sun Feb 06, 2011 11:18 am
by JetForMe
Oh! The create function returns the pointer to the thread! Now to see if it does the right thing.

Re: Any way to block a thread until another has started?

Posted: Sun Feb 06, 2011 11:40 am
by Giovanni
Sending messages to a created thread is not an issue even if it hasn't reached the chMsgWait() yet, the sender would block and the receiver would then retrieve the message.

The start of a working area can be casted to a Thread but it is not recommended, the WA organization may change, better use the "official" thread reference released by the various chThdCreatexxx() functions.

Giovanni

Re: Any way to block a thread until another has started?

Posted: Mon Feb 07, 2011 1:13 am
by JetForMe
Might be useful to provide a macro to give the Thread reference given a working area, but I don't need that now that I know I can get it from the create function.

Re: Any way to block a thread until another has started?

Posted: Mon Feb 07, 2011 9:20 am
by Giovanni
The problem is that future implementation could also move the Thread structure out of the working area. This will be required, for example, when a memory protection mechanism will be implemented (using a MMU or MPU), in that case the stack would be thread-private and the Thread structure would be system-private. The safest approach is to use the returned pointer.

Giovanni

Re: Any way to block a thread until another has started?

Posted: Mon Feb 07, 2011 9:56 am
by JetForMe
Fair enough. It's all good. I was only asking at the time because that's all I thought I had. The returned pointer is perfect.