I just compiled a project I've been working on under Windows for Linux and found that it hangs at a certain point. Since I am using std::async and std::mutex my first assumption was, that it could be a deadlock problem. However, then I wonder why it runs fine on Windows. Here is the code:
void BorderExtractor::preprocessImageAsync(const PreprocessingSettings& settings) {
_preprocessingMutex.lock();
if (!_preprocessingActive) {
_preprocessingActive = true;
std::async(std::launch::async, &BorderExtractor::preprocessImage, this, settings);
//this point is never reached on linux
_preprocessingUpToDate = true;
} else {
_cachedSettings = settings;
_preprocessingUpToDate = false;
}
_preprocessingMutex.unlock();
}
This is the function that never returns under Linux. It runs until the async call and then it just stops. It nearly appears as if the function wasn't launched asynchronously and the program waits for it to return, what wouldn't work, because the other function will try to lock the same mutex.
Here is the function that's called asynchronously:
void BorderExtractor::preprocessImage(PreprocessingSettings settings) {
//here some image processing stuff is done
_preprocessingMutex.lock();
//this point is never reached on linux
if (!_preprocessingUpToDate) {
_preprocessingUpToDate = true;
_preprocessingMutex.unlock();
std::async(std::launch::async, &BorderExtractor::preprocessImage, this, _cachedSettings);
} else {
_preprocessingUpToDate = true;
_preprocessingActive = false;
_preprocessingMutex.unlock();
}
}
The point after it tries to lock the mutex is never reached under linux.
Now, what is the problem? Is it my code that is faulty, or is there something special I have to pay attention to on Linux? (compiler flags, etc) To me this appears as if the async call is synchronous and thus causes a deadlock. But why should that be