/* * Perform a cleanup for the Threaded Network Server check if there is still * something to do or that the cleanup already took place. */ void cleanup_bnet_thread_server(alist *sockfds, workq_t *client_wq) { int status; s_sockfd *fd_ptr = NULL; if (!sockfds->empty()) { /* * Cleanup open files and pointers to them */ fd_ptr = (s_sockfd *)sockfds->first(); while (fd_ptr) { close(fd_ptr->fd); fd_ptr = (s_sockfd *)sockfds->next(); } sockfds->destroy(); /* * Stop work queue thread */ if ((status = workq_destroy(client_wq)) != 0) { berrno be; be.set_errno(status); Emsg1(M_FATAL, 0, _("Could not destroy client queue: ERR=%s\n"), be.bstrerror()); } } }
int main (int argc, char *argv[]) { pthread_t thread_id; engine_t *engine; int count = 0, calls = 0; int status; status = pthread_key_create (&engine_key, destructor); if (status != 0) dbg_printf("Create key \n"); status = workq_init (&workq, 4, engine_routine); if (status != 0) dbg_printf ( "Init work queue \n"); status = pthread_create (&thread_id, NULL, thread_routine, NULL); if (status != 0) dbg_printf ("Create thread\n"); (void)thread_routine (NULL); status = pthread_join (thread_id, NULL); if (status != 0) dbg_printf ( "Join thread \n"); status = workq_destroy (&workq); /*死等模式将无法退出*/ if (status != 0) dbg_printf ("Destroy work queue \n"); return 0; }
int main(int argc, char** argv) { int i, nworkers = 1; pthread_t threads[32]; consumer_t consumers[32]; workq_t workq; /* Get number of workers from command line */ if (argc > 1) nworkers = atoi(argv[1]); if (nworkers > 32 || nworkers < 1) { fprintf(stderr, "Error: Must have between 1 and 32 workers\n"); return -1; } /* Initialize I/O mutex and work queue */ pthread_mutex_init(&io_lock, NULL); workq_init(&workq); /* Launch worker threads */ for (i = 0; i < nworkers; ++i) { consumers[i].id = i; consumers[i].workq = &workq; pthread_create(&threads[i], NULL, consumer_main, &consumers[i]); lprintf("Create worker %d\n", i); } /* Run producer */ producer_main(&workq, 100); /* Join on worker threads */ for (i = 0; i < nworkers; ++i) { lprintf("Join worker %d\n", i); pthread_join(threads[i], NULL); } /* Free I/O mutex and work queue */ workq_destroy(&workq); pthread_mutex_destroy(&io_lock); return 0; }
int main (int argc, char *argv[]) { pthread_t thread_id; engine_t *engine; int count = 0, calls = 0; int status; status = pthread_key_create (&engine_key, destructor); if (status != 0) err_abort (status, "Create key"); status = workq_init (&workq, 4, engine_routine); if (status != 0) err_abort (status, "Init work queue"); status = pthread_create (&thread_id, NULL, thread_routine, NULL); if (status != 0) err_abort (status, "Create thread"); (void)thread_routine (NULL); status = pthread_join (thread_id, NULL); if (status != 0) err_abort (status, "Join thread"); status = workq_destroy (&workq); if (status != 0) err_abort (status, "Destroy work queue"); /* * By now, all of the engine_t structures have been placed * on the list (by the engine thread destructors), so we * can count and summarize them. */ engine = engine_list_head; while (engine != NULL) { count++; calls += engine->calls; printf ("engine %d: %d calls\n", count, engine->calls); engine = engine->link; } printf ("%d engine threads processed %d calls\n", count, calls); return 0; }
int main(int argc, char** argv) { pthread_t thread_id; engine_t* engine; int count = 0; int calls = 0; int status; status = pthread_key_create(&engine_key, destructor); assert(status == 0); status = workq_init(&workq, 4, engine_routine); assert(status == 0); status = pthread_create(&thread_id, NULL, thread_routine, NULL); assert(status == 0); (void)thread_routine(NULL); status = pthread_join(thread_id, NULL); assert(status == 0); status = workq_destroy(&workq); assert(status == 0); engine = engine_list_head; while (engine != NULL) { count++; calls += engine->calls; printf("engine %d: %d calls\n", count, engine->calls); engine = engine->link; } printf("%d engine threads processed %d calls\n", count, calls); return 0; }
void kill_q(void) { workq_destroy(work_queue); }