Reputation: 13
Take the following code as an example. In pthread_con_wait, activeMutex is locked. Then is the line "pthread_mutex_unlock(&activeMutex)" necessary? Because the main function will end soon. Or the mutex will be automatically unlocked after main is finished if I delete that line.
#include <pthread.h>
#include <iostream>
using namespace std;
int activeThreads = 0;
pthread_mutex_t coutMutex;
pthread_mutex_t activeMutex;
pthread_cond_t allDoneCondition;
void* Thread(void* v)
{
unsigned long myId = (unsigned long)v;
pthread_mutex_lock(&coutMutex);
cout << "Hello from thread " << myId << endl;
pthread_mutex_unlock(&coutMutex);
pthread_mutex_lock(&activeMutex);
activeThreads--;
if (activeThreads == 0)
{
pthread_cond_signal(&allDoneCondition);
}
pthread_mutex_unlock(&activeMutex);
}
int main(int argc, char** argv)
{
int totalThreads = 8;
pthread_mutex_init(&coutMutex, 0);
pthread_mutex_init(&activeMutex, 0);
pthread_cond_init(&allDoneCondition, 0);
pthread_mutex_lock(&activeMutex);
activeThreads = totalThreads;
for (int i = 0; i < totalThreads; ++i)
{
pthread_t t;
pthread_create(&t, 0, Thread, (void*)i);
}
cout << "Hello from main before exit" << endl;
pthread_cond_wait(&allDoneCondition, &activeMutex);
pthread_mutex_unlock(&activeMutex);
}
Upvotes: 1
Views: 2292
Reputation: 24249
There are two sides to this issue:
This is C++ so you have the option of RAII and doing something similar to what the C++11 <thread>
header does:
#include <iostream>
#include <utility>
#include <pthread.h>
struct mutex_lock_t {
pthread_mutex_t* mutex_ { nullptr };
public:
mutex_lock_t() = default;
mutex_lock_t(pthread_mutex_t& mutex) : mutex_(&mutex) { pthread_mutex_lock(mutex_); }
void unlock()
{
if (mutex_) { pthread_mutex_unlock(std::exchange(mutex_, nullptr)); }
}
~mutex_lock_t() noexcept { unlock(); }
mutex_lock_t(const mutex_lock_t&) = delete;
mutex_lock_t(mutex_lock_t&&) = delete;
mutex_lock_t operator=(const mutex_lock_t&) = delete;
mutex_lock_t operator=(mutex_lock_t&&) = delete;
};
int main()
{
pthread_mutex_t mutex {};
pthread_mutex_init(&mutex, nullptr);
mutex_lock_t lock(mutex);
std::cout << "end of main\n";
} // auto unlock at end of scope
The advantage here is that "do I have to" is no-longer a question, you just are.
If you don't, then a typical mutex is held in process memory and will simply cease to exist along with anything that might have access to it when the process terminates.
But if, in the future, your program changes and uses a mutex in shared memory with other processes, then it's better to depend on RAII cleanup rather than process cleanup.
Upvotes: 1
Reputation: 33864
At the end of the main
all of the program resources will be cleaned up by the OS. There will likely not be any negative consequences in not releasing this mutex. The real question is though, why would you not release it? There is no benefit gained by not releasing it, and you get into the good habit of having an unlock for every lock (ie. good resource management).
This is for your specific code, you need to be much more careful if it is the opened thread that forgets to release the mutex, see here.
Upvotes: 4