Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
10 years, 4 months ago.
Signal or restart threads?
Hey guys. I'm working on a project in which I have a "player" thread that "plays" objects containing sequences of instructions. Am I better off to start this thread at boot time and have it idle until a object comes in on a queue, or restart the thread every time there's an object to play using the thread arguments and let it terminate when it's done?
Question relating to:
1 Answer
10 years, 4 months ago.
It's usually best to have the threads be created at boot time, then sit in a "wait for signal" "do stuff" "Wait for signal" loop.
The two main reasons for this are: 1) Every individual thread uses some memory and CPU simply to exist, and the acts of creating and destroying a thread takes time to do.
2) If the order of processing matters, you'll need a queue. A small number of fixed "player" threads lets you use the built-in signal queueing. If you started a new thread every time a new object to process is generated, you risk running out of memory and "ticks", and as they'll also execute concurrently 'shorter' queues will complete first, even if they came in afterwards and so you'd need to roll your own queueing system.
Aside from that, quite a few embedded RTOS don't allow you to create new threads at runtime, so it's a good idea to get use to that idea for later projects that might use other RTOS.