Are function-local static mutexes thread-safe?

前端 未结 3 707
时光取名叫无心
时光取名叫无心 2020-12-28 12:46

In the following program I attempt the make the print function thread-safe by using a function-local mutex object:

#include 
#in         


        
3条回答
  •  暖寄归人
    2020-12-28 13:05

    As long as the mutex is static, yes.

    Local, nonstatic would defintely NOT be safe. Unless all your threads use the same stack, which also means you've now invented memory where one cell can hold many different values at the same time, and are just waiting for the Nobel committee to notify you for the next Nobel prize.

    You must have some sort of "global" (shared) memory space for mutexes.

提交回复
热议问题