Training
×

Semaphores: Mutual Exclusion (revisited)

Thread A and thread B must obey the protocol: wait for the unit from the semaphore before using the shared resource (buffer) and release the unit after completion.
char          cShared[10];
RTHANDLE     hSema4;
// do this once:
hSema4 = CreateRtSemaphore (1, 1, FIFO_QUEUEING);
// do this for every access to cShared:
wRem = WaitForRtSemaphore (hSema4, 1, WAIT_FOREVER);
// manipulate the buffer, for example:
cShared[5] = 99;
ReleaseRtSemaphore (hSema4, 1);
 
You obviously will do error checking in production quality code. We just do not include it here to show the key idea of the code.