Open lioriz opened 1 year ago
i have the same question with the simple code.
#include <boost/interprocess/sync/named_mutex.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/interprocess/sync/named_condition.hpp>
#include <thread>
#include <exception>
#include <iostream>
using namespace std;
using namespace boost::interprocess;
int main(int argc, char *argv[])
{
try {
if (argc == 1) {
named_condition::remove("my_named_condition");
named_mutex::remove("my_named_mutex");
named_mutex mutex(create_only, "my_named_mutex");
named_condition condition(create_only, "my_named_condition");
while (true) {
cout << "work..." << endl;
this_thread::sleep_for(chrono::seconds(1));
condition.notify_all();
}
} else {
named_mutex mutex(open_only, "my_named_mutex");
named_condition condition(open_only, "my_named_condition");
while (true) {
cout << "wait..." << endl;
scoped_lock<named_mutex> lock(mutex);
condition.wait(lock);
cout << "notified" << endl;
}
}
} catch (std::exception &e) {
std::cout << e.what() << std::endl;
}
return 0;
}
one notifier and some waiter, when terminate a waiter, then the notifier and others all blocking. i want use named_condition to sync process, but meet this question, and i don't konw how to solve it.
See: Stack overflow question
I have an
interprocess
condition variable
to sync my processes, there is onenotifier
thatnotify
and twowaiters
thattimed_wait
fornotification
ortimeout
.If one of the
waiters
crashes and restarts, the three processes are blocked.The way to fix it is to kill them, delete the shared memory, and restart them.
Processes sync shared memory class:
First process
notifier
:Second/Third processes
waiters
:Boost version
1.78.0
Linux
Ubuntu 18.04.5 LTS
Arch
aarch64
On a normal run, everything works as expected, and when one
waiter
crashes (non-boost related issues) and restarts the three processes are blocked on thecondition variable
,notifier
blocked onnotify
, firstwaiter
blocked ontimed_wait
, crashedwaiter
blocked onmutex lock
.In case the
notifier
is up but not in the while loop yet, thewaiters
can crash and restart, and the behavior of waiting until timeout will continue, but when thenotifier
enters the while loop andnotify
the three of them get stuck,notifier
block onnotify
, firstwaiter
block ontimed_wait
, crashed waiter block onmutex lock
.It happened just when I crashed one of the
waiters
when it blocked ontimed_wait
, not when I crashed thenotifier
.