According to this website, the usage of MPI::COMM_WORLD.Send(...)
is thread safe. However in my application I often (not always) run into deadlocks or get segmentation faults. Enclosing each call of MPI::COMM_WORLD
methods with a mutex.lock()
and mutex.unlock()
consistently removes deadlocks as well as segfaults.
This is how I create threads:
const auto communicator = std::make_shared<Communicator>();
std::vector<std::future<size_t>> handles;
for ( size_t i = 0; i < n; ++i )
{
handles.push_back(std::async(std::launch::async, foo, communicator));
}
for ( size_t i = 0; i < n; ++i )
{
handles[i].get();
}
Communicator
is a class which has a std::mutex
member and exclusively calls methods such as MPI::COMM_WORLD.Send()
and MPI::COMM_WORLD.Recv()
. I do not use any other methods of sending/receiving with MPI. foo
takes a const std::shared_ptr<Commmunicator> &
as argument.
My question: Is the thread safety promised by MPI not compatible with threads created by std::async
?