Results 1 to 8 of 8

Thread: CreateMutex replacment using pthread_mutex ?

  1. #1
    Join Date
    Apr 2009
    Posts
    12
    Thanks
    2
    Qt products
    Qt4

    Default 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:

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: CreateMutex replacment using pthread_mutex ?

    Why not use QMutex or QSystemSemaphore depending on what you want?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Apr 2009
    Posts
    12
    Thanks
    2
    Qt products
    Qt4

    Default Re: CreateMutex replacment using pthread_mutex ?

    Quote Originally Posted by wysota View Post
    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.

  4. #4
    Join Date
    May 2006
    Location
    Germany
    Posts
    108
    Thanks
    2
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: CreateMutex replacment using pthread_mutex ?

    Quote Originally Posted by afail View Post
    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."
    "If you lie to the compiler, it will get its revenge." - Henry Spencer

  5. #5
    Join Date
    Apr 2009
    Posts
    12
    Thanks
    2
    Qt products
    Qt4

    Default Re: CreateMutex replacment using pthread_mutex ?

    Quote Originally Posted by Methedrine View Post
    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 ?

  6. #6
    Join Date
    May 2006
    Location
    Germany
    Posts
    108
    Thanks
    2
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: CreateMutex replacment using pthread_mutex ?

    Quote Originally Posted by afail View Post
    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."
    "If you lie to the compiler, it will get its revenge." - Henry Spencer

  7. The following user says thank you to Methedrine for this useful post:

    afail (6th April 2009)

  8. #7
    Join Date
    Apr 2009
    Posts
    12
    Thanks
    2
    Qt products
    Qt4

    Default 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?

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.