CreateMutex replacment using pthread_mutex ?
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:
Code:
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:
Code:
pthread_mutex_t *mutex;
pthread_mutexattr_t *attr;
// initialize them with their _init methods
and then
Code:
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
Re: CreateMutex replacment using pthread_mutex ?
Why not use QMutex or QSystemSemaphore depending on what you want?
Re: CreateMutex replacment using pthread_mutex ?
Quote:
Originally Posted by
wysota
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.
Re: CreateMutex replacment using pthread_mutex ?
Quote:
Originally Posted by
afail
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."
Re: CreateMutex replacment using pthread_mutex ?
Quote:
Originally Posted by
Methedrine
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 ?
Re: CreateMutex replacment using pthread_mutex ?
Quote:
Originally Posted by
afail
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."
Re: CreateMutex replacment using pthread_mutex ?
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?
Re: CreateMutex replacment using pthread_mutex ?
Separate instance of what? What is exactly that you are trying to do and how are you trying to do it?