static void afsql_dd_start_thread(AFSqlDestDriver *self) { self->db_thread_wakeup_cond = g_cond_new(); self->db_thread_mutex = g_mutex_new(); self->db_thread = create_worker_thread(afsql_dd_database_thread, self, TRUE, NULL); }
// //初始化工作池 struct zhw_worker_pool_t *zhw_worker_pool_init(void) { struct zhw_worker_pool_t *p = calloc(1, sizeof(struct zhw_worker_pool_t)); if(NULL == p) { ZHW_LOG_ERR("CALLOC FAIL"); return p; } p->cache_task = zhw_cache_create(32, sizeof(struct zhw_worker_pool_task_t)); const long cpu_num = sysconf(_SC_NPROCESSORS_ONLN); const long worker_num = cpu_num > 32 ? (cpu_num + 1) : 32; p->workers = calloc(worker_num, sizeof(struct zhw_worker_t)); if(NULL == p->workers) { ZHW_LOG_ERR("CALLOC FAIL"); free(p); p = NULL; return p; } ZHW_LOG_INFO("cpu_num: %ld, worker_num: %ld", cpu_num, worker_num); long i; pthread_mutex_init(&p->init_lock, NULL); pthread_cond_init(&p->init_cond, NULL); pthread_mutex_init(&p->tasks.lock, NULL); pthread_cond_init(&p->tasks.cond, NULL); p->worker_num = worker_num; for(i = 0; i < worker_num; i++) { p->workers[i].id = i; p->workers[i].pool = p; create_worker_thread(worker_thread_loop, &p->workers[i]); } wait_for_thread_registration(p); return p; }
/** * inti the task thread and create worker thread * @param nthreads: number threads to be init * @param base: event_base * @param ttm: task thread manager * @see ************************************************************/ void task_thread_init(int nthreads, struct event_base *base, task_threads_manager_t *ttm) { if( ttm == NULL ){ LM_ERR( " in task_thread_init ttm is null \n"); return; } int i; task_thread_t *threads; pthread_mutex_init(&ttm->init_lock, NULL); pthread_cond_init(&ttm->init_cond, NULL); pthread_mutex_init(&ttm->task_freelist_lock, NULL); ttm->task_freelist = NULL; ttm->threads = (task_thread_t*)calloc(nthreads, sizeof(task_thread_t)); threads = ttm->threads; if (! threads) { LM_ERR("Can't allocate thread descriptors"); exit(1); } for (i = 0; i < nthreads; i++) { int fds[2]; if (pipe(fds)) { LM_ERR("Can't create notify pipe"); exit(1); } threads[i].notify_receive_fd = fds[0]; threads[i].notify_send_fd = fds[1]; LM_DBG("Thread pipe fds [%d], [%d]\n", fds[0], fds[1]); threads[i].ttm = ttm; setup_task_thread(&threads[i]); } LM_DBG("Pipe of threads create successfully.\n"); /* Create threads after we've done all the libevent setup. */ for (i = 0; i < nthreads; i++) { create_worker_thread(worker_thread, &threads[i]); } /* Wait for all the threads to set themselves up before returning. */ pthread_mutex_lock(&ttm->init_lock); while (ttm->init_count < nthreads) { pthread_cond_wait(&ttm->init_cond, &ttm->init_lock); } pthread_mutex_unlock(&ttm->init_lock); LM_DBG("Worker thread start up finished.\n"); }
int main() { init_task(&task); signal(SIGPIPE,SIG_IGN); signal(SIGINT,signal_quit); create_listen_thread(); create_worker_thread(); while(1) { } }
static void afamqp_dd_start_thread(AMQPDestDriver *self) { self->writer_thread = create_worker_thread(afamqp_worker_thread, self, TRUE, NULL); }
static void afmongodb_dd_start_thread (MongoDBDestDriver *self) { self->writer_thread = create_worker_thread(afmongodb_worker_thread, self, TRUE, NULL); }
static void afsql_dd_start_thread(AFSqlDestDriver *self) { self->db_thread = create_worker_thread(afsql_dd_database_thread, self, TRUE, NULL); }