Esempio n. 1
0
void thpool_destroy(thpool_t* tp_p)
{
	int i ;
	thpool_keepalive = 0;
	
	for(i = 0;i < (tp_p->threadsN); i++)
	{
		if(sem_post(tp_p->jobqueue->queueSem))
		{
			fprintf(stderr, "thpool_destroy(): Could not bypass sem_wait()\n");
		}

	}
	if(sem_post(tp_p->jobqueue->queueSem)!=0)
	{
		fprintf(stderr, "thpool_destroy(): Could not destroy semaphore\n");
	}
	for(i = 0;i < (tp_p->threadsN); i++)
	{
		pthread_join(tp_p->threads[i],NULL);
	}
	thpool_jobqueue_empty(tp_p);
	
	free(tp_p->threads);
	free(tp_p->jobqueue->queueSem);
	free(tp_p->jobqueue);
	free (tp_p);
	
}
Esempio n. 2
0
/* Force kill functionality added by Matt Layher */
void thpool_destroy(thpool_t* tp_p, int force){
	int t;
	
	/* End each thread's infinite loop */
	thpool_keepalive=0; 

	/* Awake idle threads waiting at semaphore */
	for (t=0; t<(tp_p->threadsN); t++){
		if (sem_post(tp_p->jobqueue->queueSem)){
			fprintf(stderr, "thpool_destroy(): Could not bypass sem_wait()\n");
		}
	}

	/* Kill semaphore */
	if (sem_destroy(tp_p->jobqueue->queueSem)!=0){
		fprintf(stderr, "thpool_destroy(): Could not destroy semaphore\n");
	}
	
	/* Wait for threads to finish, or if force is true, cancel them */
	for (t=0; t<(tp_p->threadsN); t++)
	{
		if(force == 1)
			pthread_cancel(tp_p->threads[t]);
		else
			pthread_join(tp_p->threads[t], NULL);
	}
	
	thpool_jobqueue_empty(tp_p);
	
	/* Dealloc */
	free(tp_p->threads);                                                   /* DEALLOC threads             */
	free(tp_p->jobqueue->queueSem);                                        /* DEALLOC job queue semaphore */
	free(tp_p->jobqueue);                                                  /* DEALLOC job queue           */
	free(tp_p);                                                            /* DEALLOC thread pool         */
}
Esempio n. 3
0
/* Destroy the threadpool */
void thpool_destroy(thpool_t* tp_p){
	int t;

	while(tp_p->jobqueue->jobsN > 0) {
	 usleep(50000);
	}	
	/* End each thread's infinite loop */
	thpool_keepalive=0; 

	/* Awake idle threads waiting at semaphore */
	for (t=0; t<(tp_p->threadsN); t++){
		if (sem_post(tp_p->jobqueue->queueSem)){
			fprintf(stderr, "thpool_destroy(): Could not bypass sem_wait()\n");
		}
	}

	/* Kill semaphore */
	if (sem_destroy(tp_p->jobqueue->queueSem)!=0){
		fprintf(stderr, "thpool_destroy(): Could not destroy semaphore\n");
	}
	
	/* Wait for threads to finish */
	for (t=0; t<(tp_p->threadsN); t++){
		pthread_join(tp_p->threads[t], NULL);
	}
	
	thpool_jobqueue_empty(tp_p);
	
	/* Dealloc */
	free(tp_p->threads);                                                   /* DEALLOC threads             */
	free(tp_p->jobqueue->queueSem);                                        /* DEALLOC job queue semaphore */
	free(tp_p->jobqueue);                                                  /* DEALLOC job queue           */
	free(tp_p);                                                            /* DEALLOC thread pool         */
}
Esempio n. 4
0
void thpool_destory (thpool_t *tp_p){
    int    t ;

    thpool_keepalive = 0 ;  //让所有的线程运行的线程都退出循环

    for (t = 0 ; t < (tp_p->threadsN) ; t++ ){

        //sem_post 会使在这个线程上阻塞的线程,不再阻塞
        if (sem_post (tp_p->jobqueue->queueSem) ){
            fprintf (stderr,"thpool_destory () : could not bypass sem_wait ()\n");
        }
        
    }
    if (sem_destroy (tp_p->jobqueue->queueSem)!= 0){
        fprintf (stderr, "thpool_destory () : could not destroy semaphore\n");
    }

    for (t = 0 ; t< (tp_p->threadsN) ; t++)
    {
        pthread_join (tp_p->threads[t], NULL);
    }
    thpool_jobqueue_empty (tp_p);
    free (tp_p->threads);
    free (tp_p->jobqueue->queueSem);
    free (tp_p->jobqueue);
    free (tp_p);



}
Esempio n. 5
0
/* Destroy the threadpool */
void thpool_destroy(thpool_t* tp_p){
	int t;

	/* End each thread's infinite loop */
	thpool_keepalive=0; 

	/* Awake idle threads waiting at semaphore and mutex*/
	for (t=0; t<(tp_p->threadsN); t++){
		if (sem_post(tp_p->jobqueue[t]->queueSem)){
			fprintf(stderr, "thpool_destroy(): Could not bypass sem_wait()\n");
		}
	}

	/* Kill semaphore and mutex */
	for (t = 0; t < tp_p->threadsN; t++){
		if (sem_destroy(tp_p->jobqueue[t]->queueSem) != 0){
		fprintf(stderr, "thpool_destroy(): Could not destroy semaphore\n");
		}

		if (pthread_mutex_destroy(&tp_p->jobqueue[t]->q_tail_lock) ||
			pthread_mutex_destroy(&tp_p->jobqueue[t]->q_head_lock)){
			fprintf(stderr, "thpool_destroy(): Could not destroy q_tail_lock or q_head_lock.\n");
		}
	}

	/* Wait for threads to finish */
	for (t=0; t<(tp_p->threadsN); t++){
		pthread_join(tp_p->threads[t], NULL);
	}

	/* There are 2 reasons that we need to "reflush" job queue:
	 * 1. Awaken threads may add work in job queue.
	 * 2. Job queue won't be real empty, even thread think it's emplty. */
	for (t = 0; t < tp_p->threadsN; t++){
		thpool_jobqueue_empty(tp_p->jobqueue[t]);
	}

	/* Dealloc */
	free(tp_p->threads);

	for (t = 0; t < tp_p->threadsN; t++){
		free(tp_p->jobqueue[t]);
	}

	free(tp_p);
}