Re: [Boost-users] [interprocess] Interprocess mutex not working / throwing exception when trying to write lock
Copying over here my initial reply: Hi, 1. Your lockWithWriteLock and lockWithReadLock create a lock, which locks on construction and unlocks on destruction, hence when the function returns no lock is held on the mutex. 2. The fact that you put a scope here makes me think that your intention is for the lock to be unlocked at the end of this scope (which it won't, as it is unlocked way before, as per #1). { std::cout<<"Program 1: Before first locking -------------------------- 1 v\n"; temp.sharedMutex->lockWithWriteLock(); const unsigned int SLEEP_TIME_IN_SECOND = 60; std::cout<<"Program 1: sleep for "<< SLEEP_TIME_IN_SECOND << "\n"; sleep(SLEEP_TIME_IN_SECOND); std::cout<<"Program 1: Finished sleeping\n"; } In this case though, you would perform the wait in bold while still holding the lock, and hence preventing the 2nd program from locking it. Once out of the scope you destroy the object right away. NOTE: both the issue in #1 and #2 lead to your first program possibly deleting the mutex before the 2nd program is done with it. Which leads to the issue you are experiencing. 3. If you need to have the 1st program wait for the 2nd program to be done, then do not use sleeps, use a condition_variable. -- View this message in context: http://boost.2283326.n4.nabble.com/interprocess-Interprocess-mutex-not-worki... Sent from the Boost - Users mailing list archive at Nabble.com.
Try running your two programs at the same time. Due to the fact that you are unlocking immediately after locking (my previous post #1), if you start the two programs at the same time you do not have issues, because the 2nd program completes before the 1st. The 2nd program will crash only if he tries to access the mutex after the 1st program completes, because you destroy the mutex at the end of program 1. Since program 1 creates the mutex, it must start before program 2. Due to the fact that your program 2 is supposed to end after (from what I see), I would suggest moving seg->destroy<SharedMemData>("TrackOutput"); to the end of the 2nd program. Mind thought that you also need to correct the issue with locking I mentioned in #1 of my previous post, or now it will be program 1 the one crashing. -- View this message in context: http://boost.2283326.n4.nabble.com/interprocess-Interprocess-mutex-not-worki... Sent from the Boost - Users mailing list archive at Nabble.com.
participants (1)
-
Malko