Skip to content

Instantly share code, notes, and snippets.

@nabijaczleweli
Created July 25, 2015 16:59
Show Gist options
  • Save nabijaczleweli/e859fb5b65232fff928e to your computer and use it in GitHub Desktop.
Save nabijaczleweli/e859fb5b65232fff928e to your computer and use it in GitHub Desktop.
Find first unlocked mutex
#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