Example #1
0
int threadpool_destroy(threadpool_t *pool, int block, int timeout) {
    int ret;
    assert(pool);
    Pthread_mutex_lock(&pool->mutex);
    if (!pool->exit) {
        /* you should call `threadpool_exit' first */
        Pthread_mutex_unlock(&pool->mutex);
        return -1;
    }

    if (pool->threads_num != 0) {
        if (!block) {
            Pthread_mutex_unlock(&pool->mutex);
            return -1;
        } else {
            struct timespec ts;
            struct timeval  tv;
            gettimeofday(&tv, NULL);
            ts.tv_sec = tv.tv_sec + timeout;
            ts.tv_nsec = tv.tv_usec * 1000;

            while (pool->threads_num != 0) {
                if (timeout == 0) {
                    Pthread_cond_wait(&pool->exit_cond, &pool->mutex);
                    goto CONT;
                } else {
                    ret = Pthread_cond_timedwait(&pool->exit_cond, 
                        &pool->mutex, &ts);
                    if (ret == 0) {
                        goto CONT;
                    } else if (ret == ETIMEDOUT) {
                        Pthread_mutex_unlock(&pool->mutex);
                        return -1;
                    }
                }
            }
        }
    }
 
CONT:
    Pthread_mutex_unlock(&pool->mutex);
    heap_destroy(&pool->task_queue);
    Pthread_mutex_destroy(&pool->mutex);
    Pthread_cond_destroy(&pool->cond);
    Pthread_cond_destroy(&pool->exit_cond);
    Pthread_cond_destroy(&pool->task_over_cond);
    free(pool);
    return 0;
}
Example #2
0
	~MyThread() {
		Pthread_cond_destroy(&c);
	}