iothreads_t iothreads_start( uint8_t nthreads, uint8_t immediately ) { uint8_t i = 0; struct iothreads * iothreads = NULL; iothreads = calloc( 1, sizeof(struct iothreads) ); if ( iothreads == NULL ) { return NULL; } iothreads->threads = calloc( nthreads, sizeof(struct iothread) ); if ( iothreads->threads == NULL ) { free( iothreads ); return NULL; } iothreads->context = iothreads; iothreads->processor = _base_processor; iothreads->immediately = immediately; iothreads->nthreads = nthreads; pthread_cond_init( &iothreads->cond, NULL ); pthread_mutex_init( &iothreads->lock, NULL ); // 开启网络线程 iothreads->runflags = 1; iothreads->nrunthreads = nthreads; for ( i = 0; i < nthreads; ++i ) { iothread_start( iothreads->threads+i, i, iothreads ); } return iothreads; }
// 创建网络线程组 // nthreads - 网络线程组中的线程数 // method - 任务处理函数 iothreads_t iothreads_start( uint8_t nthreads, void (*method)(void *, uint8_t, int16_t, void *), void * context ) { uint8_t i = 0; struct iothreads * iothreads = NULL; iothreads = calloc( 1, sizeof(struct iothreads) ); if ( iothreads == NULL ) { return NULL; } iothreads->threadgroup = calloc( nthreads, sizeof(struct iothread) ); if ( iothreads->threadgroup == NULL ) { free( iothreads ); iothreads = NULL; } iothreads->method = method; iothreads->context = context; iothreads->nthreads = nthreads; pthread_cond_init( &iothreads->cond, NULL ); pthread_mutex_init( &iothreads->lock, NULL ); // 开启网络线程 iothreads->runflags = 1; iothreads->nrunthreads = nthreads; for ( i = 0; i < nthreads; ++i ) { iothread_start( iothreads->threadgroup+i, i, iothreads ); } return iothreads; }