Any way to block a thread until another has started?

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
JetForMe
Posts: 99
Joined: Mon Jan 31, 2011 8:12 am

Any way to block a thread until another has started?

Post 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!
JetForMe
Posts: 99
Joined: Mon Jan 31, 2011 8:12 am

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

Post by JetForMe »

Oh! The create function returns the pointer to the thread! Now to see if it does the right thing.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

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

Post 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
JetForMe
Posts: 99
Joined: Mon Jan 31, 2011 8:12 am

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

Post 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.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

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

Post 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
JetForMe
Posts: 99
Joined: Mon Jan 31, 2011 8:12 am

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

Post 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.
Post Reply