When should all thread synchronization objects be used?

Under what circumstances should each of the following synchronization objects be used?

  • ReaderWriter Lock
  • Semaphore
  • mutex
+3
source share
3 answers
  • Since wait () will be returned once for each post () call, semaphores are the basic model of the producer-consumer - the simplest form of inter-thread message, with the possible exception of signals. They are used, so one thread can tell another thread that something happened, that it is interested (and how many times), and to control access to resources that can have no more than a fixed finite number of users. They offer custom warranties required for multi-threaded code.

  • , - " ". , "" . , . , , .

, , - , " ", . , "".

  • ReaderWriter - , , , , . , - . , ( , , ) , . ReaderWriter, , , - , .

  • , , , " ", , " " -, " " -. , , - ( - ).

  • , (, ) , ​​( , /) , . , , ( ) , ( , ).

Btw, # (, , " " ). , # , . , mutex/condition, .

+5

, "" - ; -)

+1

: .

( ).

, ( ), - ( . Onebyone

, ReaderWriteLockSlim ReaderWriterLock ( , ).

+1

Source: https://habr.com/ru/post/1706584/


All Articles