I have 10 threads and 4 mutex variables. Every thread tries to lock the mutex variables. If it's unlocked, let's say it locks it and sleeps for 3 seconds. If it is locked, it uses a conditional variable and waits for it to become unlocked (it becomes unlocked when other thread unlocks it).
That's the way it should work: first, threads 0,1,2,3 lock the mutex variables. All of them sleep for 3 seconds, and unlock the mutex variables. After this, another
4 threads should lock the mutex variables and sleep for 3 seconds. And so on, let's say it should last for ever (but it isn't so).
That's the way it works now: first, threads 0,1,2,3 lock the mutex variables. All of them sleep for 3 seconds, and unlock the mutex variables. After this, the same threads 0,1,2,3 lock the mutex variables. And so on, no other thread locks the mutex variables. It looks like there's something wrong with the conditional variable.
I will post some code now:
this function locks a mutex variable (tries to until it succeeds)
for (i = 0; i < nr_strunguri; i++)
if (pthread_mutex_trylock (&semm[i]) != EBUSY)
// daca nu mai e nici un strung liber
pthread_cond_wait (&cond, &mutexc);
this function unlocks a mutex variable
mutex_unlock (int i)
and this is the function of the thread:
ocupa (char *sind)
indm = mutex_lock (); // returneaza numarul strungului care a fost ocupat
PS: I apologize if you don't call them "mutex variable" and "conditional variable". I just translated word by word from my language. I think you can understand what I am talking about. Thank you.