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 10-24-2010, 06:58 PM   #1
Carlos2313
LQ Newbie
 
Registered: Oct 2010
Posts: 7

Rep: Reputation: 0
Question C Program with Threads


I guess, that I am still not understanding the logic of working with threads. I have a program that has an amount of servers in different stations that are going to help with the thread requests.I am going to have an array of Threads. So every server should process the thread. In this case I just have to get the arriving time of every thread and the time when the thread exists . However, the Thread does not get to Critical Region

Here is the code:

void *Thread(void *parm)
{
int i;
int n
for(i=0; i< stations; i++)
{
tidp[n].Schedule[n]=time();

pthread_mutex_lock(&(mutex[i]));

if(StationArr[i] > 0)
StationArr[i]--;

pthread_cond_wait(&(cond[i]), &(mutex[i]) );

printf("Processing Thread request");

pthread_mutex_unlock(&(mutex[i]));

sleep(Exp(Exp[i]));

pthread_mutex_lock(&(mutex[i]));

StationArr[i]++;

tidp[n].Schedule[n]=time();
printf("Service is finished");

pthread_cond_signal(&(cond[i]));

pthread_mutex_unlock(&(mutex[i]));
}


}

How can I create a working thread?

Last edited by Carlos2313; 10-24-2010 at 08:48 PM.
 
Old 10-25-2010, 01:37 AM   #2
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
If I'm following your code correctly,
it looks like that for every thread you have,
you do a for loop over stations, For station 0, you acquire mutex[0] and
wait for cond[0] to be signaled. Every thread gets stuck here and none of
them get signaled to move forward.

Maybe I'm missing some code?
 
Old 10-25-2010, 06:05 AM   #3
rupertwh
Member
 
Registered: Sep 2006
Location: Munich, Germany
Distribution: Debian / Ubuntu
Posts: 297

Rep: Reputation: 49
ToniT is right, but it is actually even worse (or maybe that's what ToniT meant): The cond wait is protected by the very same mutex that the thread just locked. Which means guaranteed deadlock.
You cannot lock the same mutex twice. (That's the whole point of a mutex)

Furthermore, some obvious errors:
  1. Declaration of n is missing semicolon
  2. n is then used uninitialized
  3. tidp[n].Schedule[n] is almost certainly an error.
  4. Exp(Exp[i])? Trying to win an obfuscation contest?

Have you even tried to compile the code?

And please: when posting source code, use CODE tags to preserve the indenting. (the #-Button)
 
Old 10-25-2010, 08:38 AM   #4
Carlos2313
LQ Newbie
 
Registered: Oct 2010
Posts: 7

Original Poster
Rep: Reputation: 0
Red face

Sorry ! When I was translating the variables names to English I commit some mystakes...

This would be the actual code:

Code:
void *Thread(void *parm)
{
int i;
for(i=0; i< stations; i++)
{
tidp[i].Schedule[i]=time();

pthread_mutex_lock(&(mutex[i]));

if(StationArr[i] > 0)
StationArr[i]--;

pthread_cond_wait(&(cond[i]), &(mutex[i]) );

printf("Processing Thread request");

pthread_mutex_unlock(&(mutex[i]));

sleep(Exp(ExpAv[i])); //It is calling the method that is going to calculate the exponential distribution according the average per estation

pthread_mutex_lock(&(mutex[i]));

StationArr[i]++;

tidp[i].Schedule[i]=time();
printf("Service is finished");

pthread_cond_signal(&(cond[i]));

pthread_mutex_unlock(&(mutex[i]));
}


}
So according the explanation you are giving me

Quote:
For station 0, you acquire mutex[0] and
wait for cond[0] to be signaled. Every thread gets stuck here and none of
them get signaled to move forward.
Well, What I was trying to do is that if the server is helping other thread the other threads have to wait...

Quote:
The cond wait is protected by the very same mutex that the thread just locked. Which means guaranteed deadlock.
I need a clue about how to make it work.... The thread is suppose to wait if all the servers are busy. So usually, it would be if the Stations array is less or equals zero
 
Old 10-25-2010, 10:05 AM   #5
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
From the manpage:
pthread_cond_wait atomically unlocks the mutex (as per pthread_unlock_mutex) and waits for the con‐
dition variable cond to be signaled. The thread execution is suspended and does not consume any CPU
time until the condition variable is signaled. The mutex must be locked by the calling thread on
entrance to pthread_cond_wait. Before returning to the calling thread, pthread_cond_wait re-
acquires mutex (as per pthread_lock_mutex).

So rubertwh's argument about locking the same mutex you use in the cond_wait being bad is not valid.


But still even with the new code, the problem of every thread getting stuck into waiting cond[0] holds.
You can break the blockage by calling the pthread_cond_signal after every thread is started, but
it has several different race conditions bundled into it.
Even though you probably don't want the code to get locked down (which is reachable by removing all lock and cond calls),
it is not clear to me what you are trying to achieve here, so I can't really know what the right code should look like.
 
Old 10-25-2010, 10:16 AM   #6
rupertwh
Member
 
Registered: Sep 2006
Location: Munich, Germany
Distribution: Debian / Ubuntu
Posts: 297

Rep: Reputation: 49
Quote:
Originally Posted by ToniT View Post
So rubertwh's argument about locking the same mutex you use in the cond_wait being bad is not valid.
Oops, yes, ToniT is absolutely right.
 
Old 10-26-2010, 11:39 AM   #7
Carlos2313
LQ Newbie
 
Registered: Oct 2010
Posts: 7

Original Poster
Rep: Reputation: 0
:)

It's fine... Thanks for your time
 
  


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
Problem with the timmings of a program that uses 1-8 threads on a server that has 4 D stois21 Programming 4 07-27-2010 10:21 AM
what should I be care of multi-threads C program naihe2010 Programming 4 11-05-2006 09:36 PM
Java threads listed using kill -3 does not contain all threads found using ps -auxww coneheed Programming 2 11-14-2005 08:57 AM
my get new threads from the forums perl program dmx9595 Programming 0 09-06-2003 11:28 AM
how to write a program can have 2 threads running at the same time?? man9 Programming 3 10-07-2000 01:43 PM

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

All times are GMT -5. The time now is 07:57 PM.

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