Hello, I am developing a Qt application which needs to lock its critical data from accesing on different instances. Thus I implement the singleton concept and I needed more low-level way to create Mutex and lock the access of critical data.

For the QT_WS_WIN scope I use:

Qt Code:
  1. QT_WA(
  2. {m_hMutex=CreateMutex(0, FALSE, (TCHAR*)id().utf16());},
  3. {m_hMutex=CreateMutexA(0, FALSE, id().toLocal8Bit().data());}
  4. );
  5. switch(WaitForSingleObject(m_hMutex, INFINITE))
  6. {
  7. case WAIT_TIMEOUT:
  8. CloseHandle(m_hMutex);
  9. m_hMutex = 0;
  10. break;
  11. }
To copy to clipboard, switch view to plain text mode 

And for QT_WS_X11 I use:

Qt Code:
  1. pthread_mutex_t *mutex;
  2. pthread_mutexattr_t *attr;
  3. // initialize them with their _init methods
To copy to clipboard, switch view to plain text mode 
and then

Qt Code:
  1. pthread_mutex_lock(mutex);
  2. // critical data
  3. pthread_mutex_unlock(mutex);
To copy to clipboard, switch view to plain text mode 

Whatsoever, the result is not as expected. For Windows the above method works well, but using the posix way, with pthread_mutex, the locking is not done as it is supposed. I think it might be related to the way the Mutex for Win32 is identified within the QT_WA macro.

Any help for finding the best replacement would be appreciated.

thanks