English 中文(简体)
How do I check if a thread is terminated when using pthread?
原标题:

How do I check if a thread is terminated? In my case, I have my_pthread[5] and I want to check if any of 5 threads has finished its job (is terminated? - I m not sure) then I can give them more work to do.

If I use pthread_join(), then it has to be:

pthread_join(my_pthread[0]);
...
pthread_join(my_pthread[4]);

What if thread[3] finishes before thread[0] and then I have to wait for thread0, 1, 2 to finish? That s not what I want.

问题回答

It sounds like what you want is not to wait for a thread to finish/exit, but for a thread to signal that it s done with work, so you can feed them more work.

What I would do is

  • create 5 work queues, and 1 work result queue

  • The 5 threads loops by fetching work from its work queue and posts results back to the same result queue.

The main thread(sending work to the 5 threads) would do something like this:

for(;;) {
  struct threadmessage msg;
  struct *work_result;
  struct *work;
  thread_queue_get(&result_queue,NULL,&msg);
  work_result = msg->data;
  handle_result(work_result);
  work = get_more_work();
  thread_queue_add(worK_result->queue,work,0); 
  free_work_result(work_result);
}

Each of the 5 worker threads (processing some work, posting the result back to the main thread) would do:

for(;;) {
  struct threadmessage msg;
  struct *work_result;
  struct *work;
  thread_queue_get(my_queue,NULL,&msg);
  work = msg->data;
  process(work_result);
  work_result->queue = my_queue;
  thread_queue_add(&result_queue,work_result,0);
  free_work(work);
}

The code to implement such a queue are here: http://asgaard.homelinux.org/svn/threadqueue/

  1. If they aren t detached, you could wait on them using pthread_join: this method "waits" for a thread to finish. I am not sure that s what you want.

  2. If you just want to assign them some other job (i.e. keep them running), then use a communication channel of some sort e.g. a thread-safe queue. In other words, use a queue to signal when the job is done and push new jobs through a separate queue.

Also, have a look at this thread on SO when it comes to detached thread.

I know that if you are using the wait() function for your parent function, there are signals that are set for the child threads. You can then use some macro s that are defined in wait.h that will let you know what the status of the thread that are finished are. Additionally, if the thread is done and you use the exit() command, what ever the parameter you set there is sent back to the parent thread. Mind you this is all in the C world. Other languages mileage may vary.

Been a long time since I ve worked with pthreads specifically, but with thread programming in general you wait for the thread to complete with a call to join(). Once the call to join completes you know that thread has completed its work.

If I m right you want to know which of your 5 threads finishes his job first. May be it s a good idea to use a semaphore for each of them to signal to the main thread that they are done. Like this:

sem_t signal;

void *working_thread(void *p)
{
   // do somthing useful
  sem_post(&signal);

}

int main()
{
  // create 5 threads and semaphore
  sem_wait(&signal);
  return 0;
}




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签