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

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.