This is an example of how to use the mutex_lock function.
#include <iostream>
#include <thread>
static constexpr std::size_t k_num_mutexes = 2;
void mutex_work(const int key, const int value, int* array) {
{
const int index = key % k_num_mutexes;
auto guard = metall::utility::mutex::mutex_lock<k_num_mutexes>(index);
array[index] = array[index] + value;
}
}
int array[k_num_mutexes] = {0, 0};
std::thread t1(mutex_work, 0, 1, array);
std::thread t2(mutex_work, 1, 2, array);
std::thread t3(mutex_work, 2, 3, array);
std::thread t4(mutex_work, 3, 4, array);
t1.join();
t2.join();
t3.join();
t4.join();
std::cout << array[0] << std::endl;
std::cout << array[1] << std::endl;
return 0;
}
int main()
Definition: jgraph.cpp:24