Question

I want to wait in a function out of a thread ( Run in main function ) until a mutex is released. Is it possible, or can mutexes only be used by created threads? So can we act like main is a thread ? Right now, it is giving me the following error :

_pthread_mutex_lock: Assertion `mutex->__data.__owner == 0' failed.

The function which I call in main :

void SetFrame()
{
    pthread_mutex_lock( &Framemutex );
    pthread_cond_wait( &Framecondition, &Framemutex );
    pthread_mutex_lock( &UsableFrame );
    IplImage *UsableFrame=cvCloneImage(Frame);
    pthread_mutex_unlock( &UsableFrame );
    pthread_mutex_unlock( &Framemutex );

}

I was expecting to be able to wait for condition in main. But I suppose it is not so possible. Or is it ?

Thanks.

Was it helpful?

Solution

Mutex can be used by any threads where they are accessible (even different processes if they are in shared memory).

Another approach using semaphores could be the following.

You could do something as follows, using a global,:

sem_t sem;

At the beginning of your main, you could have (if you share between thread, not process):

sem_init(&sem, 0, 0); //the counter of the semaphore is init with value 0

Then, in main:

sem_wait(&sem);

where you want the main to wait.

Finally, in your thread code, when you want to make the main run, you add:

sem_post(&sem);

Note that the above only works for two thread (otherwise, sem_post will be called too many times incrementing the semaphore counter too many times).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top