What part of memory does a mutex lock? (pthreads)

前端 未结 7 1990
独厮守ぢ
独厮守ぢ 2021-01-02 15:34

All the documentation I\'ve read on the pthreads mutex states only that a mutex prevents multiple threads from accessing shared memory, but how do you specify in the program

7条回答
  •  盖世英雄少女心
    2021-01-02 16:01

    When locked, a mutex prevents any other thread from obtaining a lock on that same mutex. So, if you want some particular data to be thread safe (i.e., a critical section of memory), you should obtain a lock on the mutex before reading from it or writing to it, then release the lock when finished. The mutex itself does not refer to any particular section of memory. It is simply a tool you can use.

提交回复
热议问题