Answer the question
In order to leave comments, you need to log in
How to write MutexLocker correctly?
Created a self-written MutexLocker wrapper, according to RAII.
class pthread_mutex_locker
{
public:
pthread_mutex_locker(pthread_mutex_wrapper * mu);
~pthread_mutex_locker();
private:
pthread_mutex_wrapper * mutex;
};
// Тело какой-то функции.
{
pthread_mutex_locker(&mutex);
//Какой-то код
}
Answer the question
In order to leave comments, you need to log in
Your variant will not work - the mutex will be destroyed immediately after creation. Strictly speaking, in C++ there is no such thing as an "anonymous instance", there is a "temporary instance".
For a named instance, destruction is guaranteed when it goes out of scope - the compound operator {}.
pthread_mutex_locker locker (&mutex);
Name it something, and everything will be okay.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question