You get a bonus - 1 coin for daily activity. Now you have 1 coin

Asynchronous semaphore in multithreaded applications

Lecture



An asynchronous semaphore is a structure that is used in asynchronous programming models. Its purpose is to block a fire-action, allowing it to be executed only after a group of other, asynchronous actions have been completely executed.

Asynchronous semaphore is a specific type of abstract semaphore data structure in the asynchronous programming world.

The concept of semaphores was invented by the Dutch scientist Edsger Dijkstra.

Asynchronous semaphore structure

The asynchronous semaphore takes a function as an argument: This argument is usually called a “fire function”. The fire function will be executed after all the asynchronous actions are completed.

The asynchronous semaphore also has a blocking variable: a counter that stores the number of outstanding asynchronous actions currently in the queue. When this counter becomes zero, all asynchronous actions have already been completed and the fire function is called.

The asynchronous semaphore has two additional functions:

The v () function: when this function is called, the value of the internal blocking variable increases. This increase characterizes the addition of one of the asynchronous actions to the queue and the expectation of processor time for execution.

P () function: when an asynchronous action completes, the p () function is called. The p () function reduces the value of the internal blocking variable. The completed asynchronous action is completed and removed from the queue. The p () function is called from within the completed asynchronous action of the callback function. The p () function also checks if the variable is equal to zero, which characterizes the number of asynchronous actions in the queue. If so, all actions have been completed, and the fire function can be called.

Using asynchronous semaphore

Whenever the programmer calls one of the asynchronous actions (which will actually be executed later, when the processor time is available), the programmer also calls the v () function of the asynchronous semaphore in order to increase the internal lock variable, which characterizes the fact that the asynchronous action is added to the queue.

When the callback of this asynchronous function has completed its execution, the p () function is called, which reduces the internal blocking variable. This reflects the fact that the asynchronous action is complete.

The p () function also checks whether the lock variable is equal to zero. If this is the case, then all the asynchronous actions have been completed and the “fire function” will be called.

An asynchronous semaphore is effective when asynchronous actions are performed independently.

see also

  • Critical section
  • Mutex
  • Futex
  • Race condition
  • Shared memory
  • Condition variable

See also


Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Operating Systems and System Programming

Terms: Operating Systems and System Programming