LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-06-2005, 07:50 PM   #1
zahadumy
Member
 
Registered: May 2005
Location: Cluj, Romania
Distribution: Fedora Core 6
Posts: 226

Rep: Reputation: 31
Threads synchronisation problem (mutex variables and conditional variables)


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)
Code:
int
mutex_lock ()
{
  int i;
  while (1)
  {
    for (i = 0; i < nr_strunguri; i++)
      if (pthread_mutex_trylock (&semm[i]) != EBUSY)
        return i;

    // daca nu mai e nici un strung liber
    pthread_mutex_lock (&mutexc);
    pthread_cond_wait (&cond, &mutexc);
    pthread_mutex_unlock (&mutexc);
  }
}
this function unlocks a mutex variable
Code:
int
mutex_unlock (int i)
{
  pthread_mutex_unlock (&semm[i]);
  pthread_cond_signal (&cond);
}
and this is the function of the thread:
Code:
void *
ocupa (char *sind)
{
  int indm;
  while (1)
  {
    indm = mutex_lock ();       // returneaza numarul strungului care a fost ocupat

    sleep (3);

    mutex_unlock (indm);
  }
}
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.

Last edited by zahadumy; 12-06-2005 at 08:02 PM.
 
Old 12-06-2005, 10:35 PM   #2
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Virginia, USA
Distribution: Debian 12
Posts: 8,336

Rep: Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548
In mutex_lock () I don't know what values &semm[i] and &mutexc contain. I think that they should both be the same variable, not two different variables. Also in mutex_lock () I think that you have code that duplicates some of the mainline code. If the way that you use &semm[i] and &mutexc happens to be correct then the duplicate code may cause your problem. Possibly the problem is the result of both errors interacting in some way that cannot be debugged with the information given in this post. My first attempt to solve your problem would be to code up mutex_lock () this way:

Code:
int
mutex_lock ()
{
  int i;
  while (1)
  {
    for (i = 0; i < nr_strunguri; i++)
    {  
      if (pthread_mutex_trylock (&semm[i]) != EBUSY)
        return i;
      // daca nu mai e nici un strung liber
      pthread_mutex_lock (&semm[i]);
      return i;
    }
  }
}

-----------------------
Steve Stites

Last edited by jailbait; 12-06-2005 at 10:41 PM.
 
Old 12-06-2005, 11:15 PM   #3
butters64
LQ Newbie
 
Registered: Nov 2005
Distribution: Mandrake LE2003 64
Posts: 13

Rep: Reputation: 0
I think semm[] is a list of 4 mutexes, and mutexec is a mutex for use with the condition variable. I think he uses the for loop to find an open mutex in semm[]. If there is none, he uses the condition variable to block until one of the semm[] mutexes is released. So I think jailbait's suggestion isn't what he wants (not sure though....)

Does pthread_cond_signal() actually *schedule* the thread that is woken? (I'm not familar enough with pthreads to know the answer.) If not, that could be his problem: After some thread calls mutex_unlock(), it immediately calls mutex_lock() and acquires the mutex that it just released. zahadumy, have you tried sleeping after mutex_unlock()?
 
Old 12-06-2005, 11:42 PM   #4
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Virginia, USA
Distribution: Debian 12
Posts: 8,336

Rep: Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548
"So I think jailbait's suggestion isn't what he wants (not sure though....)"

I agree that there are problems with my logic. In rereading the problem definition I have trouble with this statement:

"If it is locked, it uses a conditional variable and waits for it to become unlocked (it becomes unlocked when other thread unlocks it)."

The problem is dealing with 4 variables. The quoted statement would make more sense to me if it said that if all 4 variables were locked then the thread would wait for any one of the 4 variables to become unlocked and then lock on that variable. I will await further clarification before creating any more spagetti logic.

--------------------
Steve Stites
 
Old 12-07-2005, 07:43 AM   #5
zahadumy
Member
 
Registered: May 2005
Location: Cluj, Romania
Distribution: Fedora Core 6
Posts: 226

Original Poster
Rep: Reputation: 31
Quote:
Originally Posted by butters64
I think semm[] is a list of 4 mutexes, and mutexec is a mutex for use with the condition variable. I think he uses the for loop to find an open mutex in semm[]. If there is none, he uses the condition variable to block until one of the semm[] mutexes is released. So I think jailbait's suggestion isn't what he wants (not sure though....)
You're right. That's how it is.

Quote:
Originally Posted by jailbait
I agree that there are problems with my logic. In rereading the problem definition I have trouble with this statement:

"If it is locked, it uses a conditional variable and waits for it to become unlocked (it becomes unlocked when other thread unlocks it)."

The problem is dealing with 4 variables. The quoted statement would make more sense to me if it said that if all 4 variables were locked then the thread would wait for any one of the 4 variables to become unlocked and then lock on that variable. I will await further clarification before creating any more spagetti logic.
That's right, too. I wait for any of the 4 mutex variables to become open.
Now, that you understood what I'm trying to do, do you see any problem in the way I thought to do it? If you need more code, please tell me. I thought what I posted should be enough to have an idea of what I'm trying to do.

*** EDIT ***
Quote:
Originally Posted by butters64
zahadumy, have you tried sleeping after mutex_unlock()?
That solved my problem. Can you tell me why, please? Because I really don't see any reason why I need a sleep there.

Last edited by zahadumy; 12-08-2005 at 05:08 AM.
 
Old 12-07-2005, 12:16 PM   #6
lordofring
Member
 
Registered: Feb 2005
Posts: 91

Rep: Reputation: 15
After the thread 1 slept 3 seconds and signaled the condition variable, the other threads get a chance to run.(But not to run immediately.). The the thread 1 is still running. It backs to the beginning of the loop and it locks the semephone again because the other threads are not start yet.

I guess you run your original code with days, you will see the other threads start. After it runs in a very long time, there must be a chance that the scheduling just happens after the thread 1 signals the contidion variable and before it captures the semephone again.
 
Old 12-07-2005, 12:30 PM   #7
zahadumy
Member
 
Registered: May 2005
Location: Cluj, Romania
Distribution: Fedora Core 6
Posts: 226

Original Poster
Rep: Reputation: 31
Oka, thank you. I thought I did something wrong in my program, but it seems to me it's not my fault. Thank you.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
please ..help me on variables bruse Linux - Newbie 8 04-16-2005 11:57 PM
diff. between semaphores/mutex usage on threads skywalker27182 Programming 6 08-16-2004 11:34 PM
php problem accepting variables rootyard Programming 3 02-11-2004 07:35 AM
Shel scripting: variables pointing to variables and case Dark_Helmet Programming 5 06-08-2003 11:07 AM
environment variables and gdm problem cpv204 Slackware 1 05-27-2003 08:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration