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

前端 未结 7 1973
独厮守ぢ
独厮守ぢ 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:00

    a mutex prevents multiple threads from accessing shared memory

    The above is an incorrect statement. By itself, a mutex does not do that. It lets you build code that prevents multiple threads from accessing shared memory or other resources concurrently, but it does not lock anything by itself.

    You can build a program that uses a mutex to prevent multiple threads from executing specific pieces of code concurrently. If these pieces of code happen to be accessing a shared memory region, and no other code would try accessing that region concurrently without locking the mutex, then the effect would be that of "locking" that region of memory.

提交回复
热议问题