![]() |
| | #1 |
| Registered User Join Date: Jun 2008
Posts: 62
| thread pool approach Code: #define num_workers 5
pthread_t workers[num_workers];
pthread_cond_t available;;
pthread_mutex_t lock;
void* doing_stuff();
void cleanup_handler(void *arg)
{
pthread_mutex_unlock(&lock);
}
int main(int argc, char *argv[])
{
while(1)
{
pthread_cond_init(&available, NULL);
if (pthread_mutex_init(&lock, NULL) != 0)
{
exit(EXIT_FAILURE);
}
system("clear");
printf("\n\n----- Creating threads -----\n\n");
int x;
for(x =0; x<num_workers; x++)
{
pthread_create(&workers[x], NULL, doing_stuff, NULL);
}
sleep(5);
printf("\n\n----- Cancelling threads -----\n\n");
for(x =0; x<num_workers; x++)
{
printf("Canceling thread %lu.\n", workers[x]);
pthread_cancel(workers[x]);
}
for(x =0; x<num_workers; x++)
{
printf("Joining thread %lu.\n", workers[x]);
pthread_join(workers[x], NULL);
}
pthread_mutex_unlock(&lock);
pthread_cond_destroy(&available);
pthread_mutex_destroy(&lock);
sleep(5);
}
return 0;
}
void *doing_stuff()
{
printf("Thread %lu has started.\n", pthread_self());
pthread_cleanup_push(cleanup_handler, NULL);
while(1)
{
pthread_mutex_lock(&lock);
printf("Thread %lu is at wait condition.\n", pthread_self());
pthread_cond_wait(&available, &lock);
/* Perform the work */
pthread_mutex_unlock(&lock);
sleep(5);
}
pthread_cleanup_pop(0);
return NULL;
}
|
| fguy817817 is offline | |
| | #2 |
| Registered User Join Date: Sep 2004 Location: California
Posts: 2,845
| Well that's a good start, but it is by no means complete. Now you need to implement the functionality to assign jobs to the thread pool.
__________________ bit∙hub [bit-huhb] n. A source and destination for information. |
| bithub is offline | |
| | #3 |
| Registered User Join Date: Jun 2008
Posts: 62
| So this is the proper way to cancel threads and wait for them to finish? I know I have to assign jobs still but I have a better understanding of how that will happen. I just wanted to make sure my handler and cancel methodology was correct. Is it ok if I attempt to join each thread when I cancel to ensure that my main routine waits for all threads to finish? |
| fguy817817 is offline | |
| | #4 |
| Registered User Join Date: Mar 2003
Posts: 3,844
| In addition: - doing_stuff has the wrong signature - main unlocks 'lock' without having locked it - don't forget to use a "predicate", associated with the condition, to handle "spurious wakeups" - see the following: http://www.opengroup.org/onlinepubs/...#tag_03_515_08 - cleanup push/pop isn't used correctly - see the following: http://www.opengroup.org/onlinepubs/...#tag_03_514_06 http://www.opengroup.org/onlinepubs/...l#tag_02_09_05 - and of course, don't skimp on error checking gg |
| Codeplug is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Thread Prog in C language (seg fault) | kumars | C Programming | 22 | 10-09-2008 01:17 PM |
| Thread Pool libraries or examples | Mastadex | Windows Programming | 6 | 08-24-2008 08:58 PM |
| Calling a Thread with a Function Pointer. | ScrollMaster | Windows Programming | 6 | 06-10-2006 08:56 AM |
| pointer to main thread from worker thread? | draegon | C++ Programming | 2 | 10-27-2005 06:35 AM |
| Critical Sections, destroying | Hunter2 | Windows Programming | 4 | 09-02-2003 10:36 PM |