Skip to main content

Task synchronization using busy/wait

Short answer

Busy-wait synchronization coordinates tasks by repeatedly testing a shared flag until a condition is met. It is simple for short waits but wastes CPU cycles, may cause priority inversion, and increases power consumption.

What is task synchronization and explain the task synchronization using "Busy/Wait".

Task synchronisation is the coordination of tasks so that they execute in the correct order and access shared resources one at a time, to avoid race conditions and inconsistent data. For example, a consumer task must wait until a producer task has produced data.

Busy/Wait synchronisation

In busy waiting (spin waiting), a task that needs a resource or event keeps looping and testing a shared flag (variable) until the condition becomes true.

volatile int flag = 0;       /* shared flag */

void producer(void) { prepare_data(); flag = 1; }

void consumer(void)
{
    while (flag == 0)        /* busy wait: spin until set */
        ;
    use_data();
    flag = 0;
}

Mutual exclusion can be done in the same way with a lock variable: while (lock == 1); lock = 1; /* critical section */ lock = 0; (a test-and-set instruction is needed to make the test and the set atomic).

Advantages: simple to implement; no context switch when the wait is very short; fine for short waits in interrupts or multi-core systems.

Disadvantages: it wastes CPU cycles while spinning; with pre-emptive priority scheduling on one CPU, if a high-priority task spins, the lower-priority task that would set the flag never gets the CPU, so the system can hang; it uses more power; and it is not suitable for long waits. For these reasons, blocking primitives (semaphores, sleep/wake) are preferred.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Embedded System chapter 6 questions →