pthread issue in program

来源:互联网 发布:arm linux qt 编辑:程序博客网 时间:2024/06/06 01:11

I meet one pthread issue today. After the condition signal, another thread which was waiting for the signal didn't wake up. While the same code works well in x86, but it can't work in one mipsel product.

The issue was found after changed some other pthread issues. But it found that the program was unstable. Even one struct name can affect the result!

The root cause was wrong use mutex in condition. Here is the code episode (glib code):

      GMutex *mutex = g_mutex_new();
      ........
      if (!g_cond_timewait(cond, mutex, time))

obviously, it lacks the g_mutex_lock(). We know, the g_cond_timewait() will call g_mutex_unlock(pthread_unlock), which is undefined result in some enviroment.