The Assembly Programming Master Book

Semaphores are global objects that allow you to synchronize the operation of several processes or threads. From the programmer's point of view, a semaphore is simply a global counter that can be manipulated only using special functions. If this counter is equal to N , this means that N processes have access to the resource. Consider functions for working with semaphores in more detail.

Consider the algorithm of working with the semaphore. First, create a semaphore using the CreateSemaphore function, and assign its descriptor to the global variable. Before attempting to access the resources, the access to which must be limited, the thread must call the WaitForSingleObject function. When access is provided, the function returns 0. Having completed work with the resource, it is necessary to call the ReleaseSemaphore function. Thus, the access counter is released by 1, and the WaitForSingleObject function, in turn , decreases the counter value. Using semaphores, it is possible to control the number of threads that can simultaneously access the resource. The maximum counter value specifies how many threads can simultaneously access the object. Usually, as I have already mentioned, this value is assumed to be 1.

Категории