PDA

View Full Version : CreateMutex replacment using pthread_mutex ?



afail
6th April 2009, 16:56
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_WA(
{m_hMutex=CreateMutex(0, FALSE, (TCHAR*)id().utf16());},
{m_hMutex=CreateMutexA(0, FALSE, id().toLocal8Bit().data());}
);
switch(WaitForSingleObject(m_hMutex, INFINITE))
{
case WAIT_TIMEOUT:
CloseHandle(m_hMutex);
m_hMutex = 0;
break;
}


And for QT_WS_X11 I use:



pthread_mutex_t *mutex;
pthread_mutexattr_t *attr;
// initialize them with their _init methods

and then



pthread_mutex_lock(mutex);
// critical data
pthread_mutex_unlock(mutex);


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

wysota
6th April 2009, 16:57
Why not use QMutex or QSystemSemaphore depending on what you want?

afail
6th April 2009, 17:17
Why not use QMutex or QSystemSemaphore depending on what you want?

because as far as I see there is no way to make the mutex on system level, it is only for the pid of the application.

Methedrine
6th April 2009, 17:22
because as far as I see there is no way to make the mutex on system level, it is only for the pid of the application.

To quote the docs:

"Unlike QSemaphore, a QSystemSemaphore can also be accessed from multiple processes."

afail
6th April 2009, 17:24
To quote the docs:

"Unlike QSemaphore, a QSystemSemaphore can also be accessed from multiple processes."

ahm.. what I used to read was Qt 4.4.3.
Is there some kind of workaround for Qt < 4.5 ?

Methedrine
6th April 2009, 17:27
ahm.. what I used to read was Qt 4.4.3.
Is there some kind of workaround for Qt < 4.5 ?

To quote the docs again...

"This class was introduced in Qt 4.4."

afail
27th April 2009, 12:42
Sorry to reopen this issue but the semaphore behaves a little bit strange.

If I start 2 processes one after another, all is fine and the lock is done correctly. If I start 10 processes one after another (i.e. in GNU/Linux: # ./exefile & ./exefile & ./exefile & ./exefile) then either some of the instances crashes or starts as separate instance (which shouldn't).

Any ideas?

wysota
27th April 2009, 19:06
Separate instance of what? What is exactly that you are trying to do and how are you trying to do it?