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
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 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.


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

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 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
  •  
Qt is a trademark of The Qt Company.