Results 1 to 17 of 17

Thread: Interprocess nonblocking semaphore / mutex / shared memory.

Hybrid View

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

    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...

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    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.


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

    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()...

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    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.


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

    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.

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
  •  
Qt is a trademark of The Qt Company.