threadA 浏览这个片段
{
global_a = 100; // 1
{
pthread_mutex_lock(&b_mutex)
...
pthread_mutex_unlock(&b_mutex)
} // 2
}
threadB 浏览这个片段
{
{
pthread_mutex_lock(&b_mutex)
...
pthread_mutex_unlock(&b_mutex)
} // 3
int tmp = global_a; // 4
}
并假设从观察者的角度来看,执行顺序确实是
- 线程A --- 1
- 线程A --- 2
- 线程B --- 3
- 线程B --- 4
threadB 的代码可以"int tmp = global_a;"
看到 threadA 的设置"global_a = 100;"
吗?
欢迎任何建议。