I'm trying to find out if there is a way to init a pthread_mutex so that he starts locked.
With pthread_mutex_init() it starts unlocked by default and I don't know how to change that.
Any suggestions?
Thanks in advance.
Printable View
I'm trying to find out if there is a way to init a pthread_mutex so that he starts locked.
With pthread_mutex_init() it starts unlocked by default and I don't know how to change that.
Any suggestions?
Thanks in advance.
why not call pthread_mutex_lock() after pthread_mutex_init(), this is not atomic but as you're in the initialization phase, only one thread/process is doing it, right? this is done once
I thought of that but isn't that bad programming?
Not in of itself. In fact, that's your only option.
The next question is "why?".
gg
No, it's fine. There is a macro, PTHREAD_MUTEX_INITIALIZER, which you can use to statically initialize a mutex, but it initializes it to an unlocked state. There is no initializer macro which initializes to a locked state.
I sort of wonder why the mutex should begin its life in a locked state...
The question really was why was I doing it.
It turns out I should use a posix semaphore instead.
Thanks for all the help anyway.