Results 1 to 17 of 17

Thread: Interprocess nonblocking semaphore / mutex / shared memory.

  1. #1
    Join Date
    May 2011
    Posts
    19
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Interprocess nonblocking semaphore / mutex / shared memory.

    Hello!

    I need lock some resources (e.g. files) used by my programs. I've got few process and I want synchronize beetwen them. QSystemSemaphore or QSharedMemory look nice, but unfortunetly those classes don't have non-blocking function to lock (like QMutex's tryLock() ). Could anyone pass me a nice multiplatform solution for that?

    best regards
    m.p
    Last edited by m.p; 10th May 2011 at 11:22.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Interprocess nonblocking semaphore / mutex / shared memory.

    QSharedMemory look nice, but unfortunetly those classes don't have non-blocking function to lock
    QSharedMemory::lock()
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    May 2011
    Posts
    19
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Interprocess nonblocking semaphore / mutex / shared memory.

    Quote Originally Posted by high_flyer View Post
    No, no, no. QSharedMemory::lock() is locking function, from doc:

    "This is a semaphore that locks the shared memory segment for access by this process and returns true. If another process has locked the segment, this function blocks until the lock is released."

    I need something non blocking.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Interprocess nonblocking semaphore / mutex / shared memory.

    Sorry read to fast.
    But you can do it your self.
    For example, you can override QSharedMemory so:
    Qt Code:
    1. // in the header:
    2. bool m_bLocked;
    3.  
    4. //and
    5. bool isLocked() {return m_bLocked; }
    6.  
    7.  
    8. //in the implementation:
    9. //initialize in the ctor
    10.  
    11. bool MyQSharedMemory::tryLock()
    12. {
    13. if(!m_bLocked){
    14. m_bLocked = QSharedMemory::lock();
    15. return m_bLocked;
    16. }
    17.  
    18. return false;
    19. }
    To copy to clipboard, switch view to plain text mode 

    But I would go through the QSharedMemory doc thoroughly, as my guess is that the trolls didn't build it in in the first place because probably it conflicts with the way QSharedMemory work, and the way it should be used.
    Since I didn't work with it yet, I can't say.

    Another way is to use to protect the resource with a mutex, and only do something with it if you managed to lock the mutex.
    Qt Code:
    1. if(mutex.tryLock()){
    2. //Do something with resource
    3. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by high_flyer; 11th May 2011 at 09:33.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    May 2011
    Posts
    19
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Interprocess nonblocking semaphore / mutex / shared memory.

    Yours solutions are fine, but not for me. They wont work for more than one process.

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Interprocess nonblocking semaphore / mutex / shared memory.

    What do you mean "more than one process"?
    Can you explain a bit more?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    May 2011
    Posts
    19
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Interprocess nonblocking semaphore / mutex / shared memory.

    What do you mean "more than one process"?
    Can you explain a bit more?
    Yes. I have two or more separate application wrote by me (or more than one instance of one app), and the can use common resources (e.g. files), and I want provide exclusive acces at the time to them. So I must synchronize those processes.

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Interprocess nonblocking semaphore / mutex / shared memory.

    One relative easy way of doing this is by creating a temp file, that all sharing applications know about.
    If the file exist, the application knows the resource is used by another application.
    So you can in your application just use QFileInfo::exists(), to know if other applications are using the resource.
    Something like:
    1. look if file exist.
    2. If exist, continue with something else.
    3. if does not exist, create file, and access shared resource.
    4. delete file when shared resource when finished dealing with shared resource.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  9. #9
    Join Date
    May 2011
    Posts
    19
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Interprocess nonblocking semaphore / mutex / shared memory.

    I have been thinking about that solution, but what if one of process that acquired acces to resources and created temp file crashed before it can delete this temp file? Other proces will be thinking, that resources are loocked.

  10. #10
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Interprocess nonblocking semaphore / mutex / shared memory.

    You can handle that by catching and handling SIGSEGV signal.
    You can search in the forum there are several threads about it, or google it.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  11. #11
    Join Date
    May 2011
    Posts
    19
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Interprocess nonblocking semaphore / mutex / shared memory.

    SIGSEGV is a POSIX signal. I need multiplatform solution.

  12. #12
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Interprocess nonblocking semaphore / mutex / shared memory.

    Well, then you will just have to implement the segmentation fault catching per system, and guard it with #ifdef <your OS>.
    Just few more lines of code.
    But any OS should have some sort of signal on segmentation fault, either POSIX or something else.
    OR -
    On windows, you can for example use exceptions, and raise a SIGSEGV your self, and then you need only one signal handler for POSIX and Windows.
    http://msdn.microsoft.com/en-us/libr...=vs.71%29.aspx
    The SIGILL, SIGSEGV, and SIGTERM signals are not generated under Windows NT. They are included for ANSI compatibility. Thus you can set signal handlers for these signals with signal, and you can also explicitly generate these signals by calling raise.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  13. #13
    Join Date
    May 2011
    Posts
    19
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Interprocess nonblocking semaphore / mutex / shared memory.

    I rather write some os dependend code for mutex (CreateMutex() for Win and pthread for POSIX) than write segfaults handlers...

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

    Default Re: Interprocess nonblocking semaphore / mutex / shared memory.

    Create a protected resource that will determine if the resource you are after is available. Then regular lock() is fine because you only do the locking for a short moment.

    Qt Code:
    1. class Sync {
    2. public:
    3. Sync(const QString &key) : mem(key){
    4. dat = 0;
    5. if(mem.create(sizeof(bool))) dat = (bool*)mem.data();
    6. else return;
    7. mem.lock();
    8. *dat = true;
    9. mem.unlock();
    10. }
    11. ~Sync() { if(dat) mem.detach(); }
    12. bool tryLock() {
    13. mem.lock();
    14. if(*dat) {
    15. /* lock the real resource here */;
    16. *dat = false;
    17. mem.unlock();
    18. return true;
    19. } else { mem.unlock(); return false; }
    20. }
    21. void lock() {
    22. mem.lock();
    23. if(*dat) { /* lock the resource, etc. */ } else {
    24. mem.unlock();
    25. // lock on a QSystemSemaphore
    26. }
    27. }
    28. // unlock in a similar fashion
    29. private:
    30. QSharedMemory mem;
    31. bool *dat;
    32. }
    To copy to clipboard, switch view to plain text mode 

    This is ALMOST foul proof.
    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.


  15. #15
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Interprocess nonblocking semaphore / mutex / shared memory.

    I rather write some os dependend code for mutex (CreateMutex() for Win and pthread for POSIX) than write segfaults handlers...
    The both are not related.
    You raised a concern that one application may crash while it holds a resource.
    You need to handle that case, no mater what type of locking mechanism you are using, be it deleting a file, or something else.
    Google for "inter process communication", and choose the one best fits your needs.
    There is nothing in Qt I know of for that, except may DBus but that is not supported on all platforms.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  16. #16
    Join Date
    May 2011
    Posts
    19
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Interprocess nonblocking semaphore / mutex / shared memory.

    Quote Originally Posted by wysota View Post
    Create a protected resource that will determine if the resource you are after is available. Then regular lock() is fine because you only do the locking for a short moment.

    Qt Code:
    1. bool tryLock() {
    2. mem.lock();
    3. [*]
    4.  
    5. if(*dat) {
    6. /* lock the real resource here */;
    7. *dat = false;
    8. mem.unlock();
    9. return true;
    10. } else { mem.unlock(); return false; }
    11. }
    To copy to clipboard, switch view to plain text mode 

    This is ALMOST foul proof.
    And what about when app will hang up in moment marked as[*]? Other app will also hangup becouse of blocking nature of lock()...

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

    Default Re: Interprocess nonblocking semaphore / mutex / shared memory.

    Why would it hang here?
    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.


Similar Threads

  1. shared memory wont attach
    By daemonna in forum Qt Programming
    Replies: 2
    Last Post: 25th August 2010, 01:11
  2. Does Qt support shared memory ?
    By Shawn in forum Qt Programming
    Replies: 11
    Last Post: 3rd November 2009, 17:07
  3. QT shared memory
    By sweetsubha2020 in forum Qt Programming
    Replies: 2
    Last Post: 18th January 2009, 05:30
  4. MySQL and shared-memory connection
    By patrikd in forum Qt Programming
    Replies: 1
    Last Post: 1st August 2008, 16:53
  5. How to clear shared memory?
    By THRESHE in forum Qt Programming
    Replies: 4
    Last Post: 22nd May 2008, 18:28

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.