/*================================MAIN FUNCTION====================================*/ int main() { thread_pool_t* thread_pool; common_msg_t common_msg; //syn_queue_t* queue_syn; int i; //log_init("/home/binyang/Program/filesystem/src/common/test", LOG_DEBUG); log_init("", LOG_DEBUG); queue_syn = alloc_syn_queue(10, sizeof(common_msg_t)); thread_pool = alloc_thread_pool(8, queue_syn, resolve_handler); thread_pool->tp_ops->start(thread_pool); for(i = 0; i < 30; i++) { common_msg.operation_code = 1; queue_syn->op->syn_queue_push(queue_syn, &common_msg); common_msg.operation_code = 2; queue_syn->op->syn_queue_push(queue_syn, &common_msg); } destroy_thread_pool(thread_pool); destroy_syn_queue(queue_syn); log_close(); return 0; }
int alloc_thread(ThreadPool *tp, THandler handler, void *arg) { Pthread_t *th; pthread_mutex_lock(&tp->lock); if (!tp->head && !alloc_thread_pool(tp, 10)) { pthread_mutex_unlock(&tp->lock); return -1; } th = tp->head; tp->head = th->next; th->arg = arg; th->handler = handler; pthread_mutex_unlock(&th->lock); pthread_mutex_unlock(&tp->lock); return 0; }