Created
July 25, 2015 16:59
-
-
Save nabijaczleweli/e859fb5b65232fff928e to your computer and use it in GitHub Desktop.
Find first unlocked mutex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <mutex> | |
#include <vector> | |
#include <iostream> | |
#include <algorithm> | |
using namespace std; | |
int main() { | |
vector<mutex> mutexes(20); | |
for(int i = 0; i < 10; ++i) | |
mutexes[i].lock(); | |
const auto result = find_if(mutexes.begin(), mutexes.end(), [&](auto & mtx) { | |
bool acquired = mtx.try_lock(); | |
if(acquired) | |
mtx.unlock(); | |
return acquired; | |
}); | |
cout << (result - mutexes.begin()) << '\n'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment