954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Threaded Queues of Peopl

I am using
a) Linux
b) Pthreads

I want to write a program, with two pthreads, to cater for two queues of people. People arrive at each queue at a random interval of 1 - 10 seconds.

Each time a person arrives, print out the number of people in each queue to the screen.

Apparently sleep() pauses the whole program, how can a pause one particular thread??

John Linux
Light Poster
28 posts since May 2010
Reputation Points: 30
Solved Threads: 0
 

pthread_cond_timedwait() is a usual solution.

nezachem
Posting Shark
903 posts since Dec 2009
Reputation Points: 719
Solved Threads: 194
 

I have looked at sample code, and cannot seem to implement that, for example it needs a condition. I want something like sleep, that can be used in the thread with no conditions.

Some kind of skeleton example of what I'm trying to do (I can create and join pthreads fine, just have limited knowledge when it comes to mutexes and conditions)

bool run;

int getRandom()
{
    return random number between 1 and 10
}

void thread1()
{
   while(run)
   {
      temp = getRandom();
      pause thread for temp seconds;
   }
}

void thread2()
{
   while(run)
   {
      temp = getRandom();
      pause thread for temp seconds;
   }
}

int main()
{
   create thread to run a timer, after 10 mins, set run to false

   create thread 1;
   create thread 2;
   join thread1;
   join thread2;


}
John Linux
Light Poster
28 posts since May 2010
Reputation Points: 30
Solved Threads: 0
 

In your particular situation the condition is never going to be signalled. Just declare one in the pause() function, something along the lines of

void pause_thread(struct timespec * wakeuptime)
{
    pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
    pthread_mutex_t mx = PTHREAD_MUTEX_INITIALIZER;

    pthread_mutex_lock(&mx);
    while(pthread_cond_timedwait(&cond, &mx, wakeuptime) != ETIMEDOUT)
        ;
    pthread_mutex_unlock(&mx);
    pthread_cond_destroy(&cond);
    pthread_mutex_destroy(&mx);
}


This is a demo. Of course you may want to have cond and mx to be created once and destroyed once.> just have limited knowledge when it comes to mutexes and conditions

That's OK. You want to learn, don't you?

nezachem
Posting Shark
903 posts since Dec 2009
Reputation Points: 719
Solved Threads: 194
 

Thanks for your help.
This looks like what I wanted.

However I can't seem to implement the timespec properly.
I'm looking at :
http://publib.boulder.ibm.com/infocenter/iseries/v5r4/index.jsp?topic=%2Fapis%2Fusers_77.htm

yet I can't seem to use gettimeofday(), and when i use time() instead, I get errors

John Linux
Light Poster
28 posts since May 2010
Reputation Points: 30
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: