Use ThreadX Queues, Semaphores and Event Flags
Pass messages, signal events and wait on several conditions in ThreadX with tx_queue, tx_semaphore and tx_event_flags calls, with an example.

Threads that never talk to each other are rare. ThreadX gives you three main ways to coordinate them: message queues that carry data, counting semaphores that carry only a count, and event flags that carry a set of single-bit conditions. They overlap, and picking the right one is mostly a question of what the waiting thread needs to know when it wakes. This post builds one small program that uses all three, on ThreadX 6.4, and says when each fits. The calls are documented in the ThreadX user guide. Thread creation was covered in the previous ThreadX post, so the thread boilerplate is kept short here.
The common rules
Every blocking call takes a wait option as its last argument: TX_NO_WAIT to return at once, TX_WAIT_FOREVER to block until the condition holds, or a number of ticks to wait at most that long. Every call returns a UINT status. TX_SUCCESS means it worked, and a timeout returns TX_QUEUE_EMPTY, TX_NO_INSTANCE or TX_NO_EVENTS depending on the object. Check the status, especially when you pass a timeout, because a timeout is a normal outcome and your code should decide what to do about it.
All three objects are created the same way: a static control block, a name and the parameters of the object, usually in tx_application_define().
Message queues
A queue carries messages of a fixed size, measured in 32-bit words. You choose the message size from a set of constants, TX_1_ULONG up to TX_16_ULONG, and give the queue a block of memory. The queue holds as many messages as fit in that memory:
#define QUEUE_MSGS 8
typedef struct {
ULONG id;
ULONG value;
} sample_msg;
static TX_QUEUE sample_queue;
static ULONG queue_mem[QUEUE_MSGS * 2];
tx_queue_create(&sample_queue, "samples", TX_2_ULONG,
queue_mem, sizeof(queue_mem));The message here is two words, so TX_2_ULONG, and the memory is eight messages of two words each. The queue copies messages in and out by value, so the sender's buffer is free as soon as tx_queue_send() returns. The memory size is in bytes, which is why the example uses sizeof. Sending and receiving look like this:
sample_msg out = { .id = 1, .value = 42 };
UINT status = tx_queue_send(&sample_queue, &out, TX_WAIT_FOREVER);
sample_msg in;
status = tx_queue_receive(&sample_queue, &in, TX_WAIT_FOREVER);A sender blocks when the queue is full, and a receiver blocks when it is empty. Messages leave in the order they arrived, except that tx_queue_front_send() puts a message at the head for urgent items. A thread that sends from an interrupt handler must use TX_NO_WAIT, since interrupt service routines cannot block, and handle the full-queue case.
Queues are the right tool when the data matters: sensor readings, commands, packets. They also decouple the producer from the consumer's speed up to the queue's depth.
Counting semaphores
A semaphore is a counter that threads wait on. tx_semaphore_put() increments it and tx_semaphore_get() decrements it, blocking if it is zero. No data moves, so it is the lightest of the three:
static TX_SEMAPHORE data_ready;
tx_semaphore_create(&data_ready, "ready", 0);The initial count of 0 means the first get blocks until someone calls put. A typical use is an interrupt handler signaling a thread that work is waiting, since tx_semaphore_put() is safe from interrupt context. The count also remembers signals: if the handler puts the semaphore three times before the thread runs, the thread gets three immediate returns. That is the difference from a flag, which cannot count. A semaphore with a maximum of one in practice is a binary semaphore; ThreadX has tx_semaphore_ceiling_put() to cap the count.
Do not use a semaphore for mutual exclusion of a shared resource when you can use a mutex. ThreadX has mutexes with priority inheritance, tx_mutex_create(), which avoid unbounded priority inversion. Semaphores are for signaling and for counting resources.
Event flags
An event flags group is a 32-bit word of independent flags. A thread can wait for any one of several flags, or for all of a set, which neither a queue nor a semaphore does:
#define EVT_DATA 0x01
#define EVT_STOP 0x02
#define EVT_TIMEOUT 0x04
static TX_EVENT_FLAGS_GROUP events;
tx_event_flags_create(&events, "events");Setting is done by any thread or handler, and waiting names the bits and the rule:
tx_event_flags_set(&events, EVT_DATA, TX_OR);
ULONG actual;
UINT status = tx_event_flags_get(&events, EVT_DATA | EVT_STOP, TX_OR_CLEAR,
&actual, TX_WAIT_FOREVER);The TX_OR option on set ORs the bits into the group. On get, the rule TX_OR means the call returns when any of the requested bits is set, TX_AND means all of them, and the _CLEAR variants clear the matched bits on return, which makes an event consumable. The actual variable receives the flags that were set when the thread woke, so the thread can tell which condition occurred. Event flags suit state signaling to a thread that waits on several sources, such as "data arrived or a stop was requested".
A program that uses all three
A producer thread reads a fake sensor, queues the sample, and signals; a consumer thread waits for either data or a stop request:
static void producer(ULONG input)
{
sample_msg m = { .id = 0, .value = 0 };
for (int i = 0; i < 3; i++) {
m.id = i;
m.value = 100 + i;
tx_queue_send(&sample_queue, &m, TX_WAIT_FOREVER);
tx_event_flags_set(&events, EVT_DATA, TX_OR);
tx_thread_sleep(50);
}
tx_event_flags_set(&events, EVT_STOP, TX_OR);
}
static void consumer(ULONG input)
{
ULONG flags;
sample_msg m;
while (1) {
tx_event_flags_get(&events, EVT_DATA | EVT_STOP, TX_OR_CLEAR,
&flags, TX_WAIT_FOREVER);
while (tx_queue_receive(&sample_queue, &m, TX_NO_WAIT) == TX_SUCCESS) {
printf("sample %lu = %lu\n", m.id, m.value);
}
if (flags & EVT_STOP) {
printf("stop requested\n");
break;
}
}
}Both threads are created with tx_thread_create() as before, with the consumer at a higher priority so it handles each sample as it arrives. The output, with printf retargeted to a serial port:
sample 0 = 100
sample 1 = 101
sample 2 = 102
stop requestedThe consumer drains the queue with TX_NO_WAIT after each wake, which handles the case where two samples arrive between wakeups, since a flag is a single bit and cannot count. The semaphore is not needed in this design, because the flags carry the signal and the queue carries the data. A design with only the semaphore and the queue works too and is the more common pairing: the semaphore counts the items and the consumer calls tx_queue_receive() once per count.
Choosing
Use a queue when the receiver needs the data itself. Use a semaphore when the receiver needs only to be told that something happened, and you want every occurrence counted. Use event flags when a thread waits on several independent conditions, or when many threads should be woken by one event. A thread waiting on both a queue and a flag at once is not directly supported: ThreadX offers tx_queue_send_notify() and similar notification callbacks, which let a queue set an event flag, so a thread can wait on the flags and then read the queue.
Pitfalls
Message size mismatches corrupt data: the structure you send must be exactly the size the queue was created for, in words. A receive buffer smaller than the message overruns the stack. Priority inversion can occur with semaphores, so prefer mutexes for protecting data. Event flags set with TX_AND clear nothing, so a flag set once stays set until a clearing get or a TX_AND set with a mask; forgetting to clear gives a consumer that wakes for an event it has already handled. And objects are not freed when threads end, so delete them with the matching tx_*_delete() call if you create them dynamically.


