it is necessary to block N motehov but I don't want to write Tokyo code.
std::lock(arr[0], arr[1], ...., arr[N-1]);
If you run over all mutexes and call lock ()
for(auto& mtx : arr) { mtx.lock(); }
this can lead to deadlocks.
it is necessary to block N motehov but I don't want to write Tokyo code.
std::lock(arr[0], arr[1], ...., arr[N-1]);
If you run over all mutexes and call lock ()
for(auto& mtx : arr) { mtx.lock(); }
this can lead to deadlocks.
namespace details { template<typename T, size_t N, size_t ... Indexes> void lock_all_impl(std::array<T, N> & muts, std::index_sequence<Indexes...>) { std::lock(muts[Indexes]...); } }//end of namespace details template<typename T, size_t N> void lock_all(std::array<T, N> & muts) { details::lock_all_impl(muts, std::make_index_sequence<N>()); } //использование: std::array<std::mutex, mutex_count> arr; lock_all(arr);
mutex mutboss; array<mutex> arrmut; condition_variable sigboss; bool flagwaitsignal = false ; size_t guest_count = 0; mutex mutguests; void GlobalLock(){ mutboss.lock(); unique_lock<std::mutex> lock(mutguests); while(guest_count) { flagwaitsignal = true ; sigboss.wait(lock); } } void GlobalUnLock(){ mutboss.unlock(); } void PassIn(size_t ind){ mutboss.lock(); mutguests.lock(); ++guest_count; mutguests.unlock(); mutboss.unlock(); arrmut[ind].lock(); } void PassOut(size_t ind){ arrmut[ind].unlock(); mutguests.lock(); --guest_count; if(flagwaitsignal and guest_count == 0) { flagwaitsignal = false; sigboss.notify_one(); } mutguests.unlock(); }
Everything is written on the члаз
- the syntax may be wrong, but this is a classic implementation, all in memory.
Source: https://ru.stackoverflow.com/questions/975103/
All Articles