Problem with pThread sync issue

前端 未结 5 1089
温柔的废话
温柔的废话 2020-12-29 00:27

I am facing a sync issue with pthread. threadWaitFunction1, is a thread wait function. I expect line no. 247 flag = 1 to be executed only after 243-246 has fini

5条回答
  •  天命终不由人
    2020-12-29 01:06

    Paul E. McKenney has written a book titled "Is Parallel Programming Hard, And, If So, What Can You Do About It?", which has really good information (and some nice pictures) on memory barriers.

    Back to your question, flag isn't protected by anything. While you may think that pthread_mutex_lock() and pthread_mutex_unlock provides some strong ordering and visibility guarantees, the only guarantees it provides are for accesses inside the critical region and for the mutex itself.

    What's more, on some architectures pthread_mutex_lock() uses an acquire barrier and pthread_mutex_unlock() uses a release barrier, which means that accesses before and after the mutex protected region can spill into the mutex protected region. Strong ordering guarantees are provided between a CPU releasing a mutex and another CPU acquiring the same mutex, but pretty much everything else doesn't need (and maybe doesn't get) such strong guarantees.

    Edit:

    Apparently I was wrong with respect to pthreads, they seem to require full memory barriers (if you interpret synchronize memory with respect to other threads as requiring that). More about this, and some info on the guarantees provided in real-world implementations at Reordering Constraints for Pthread-Style Locks by Hans Boehm.

    I'm also still wondering about NPTL on IA64 1, 2.

提交回复
热议问题