Exemple #1
0
// 创建网络通信层
iolayer_t iolayer_create( uint8_t nthreads, uint32_t nclients )
{
    struct iolayer * self = (struct iolayer *)malloc( sizeof(struct iolayer) );
    if ( self == NULL )
    {
        return NULL;
    }

    self->context   = NULL;
    self->transform = NULL;
    self->nthreads  = nthreads;
    self->nclients  = nclients;
    self->localfunc = NULL;
    self->localdata = NULL;
    self->status    = eLayerStatus_Running;

    // 初始化会话管理器
    if ( _new_managers( self ) != 0 )
    {
        iolayer_destroy( self );
        return NULL;
    }

    // 创建网络线程组
    self->group = iothreads_start( self->nthreads, _io_methods, self );
    if ( self->group == NULL )
    {
        iolayer_destroy( self );
        return NULL;
    }

    return self;
}
Exemple #2
0
int32_t main()
{
    uint64_t index = 0;

    uint8_t i = 0;
    uint8_t nthreads = 4;

    iothreads_t threadgroup;
    uint64_t start_time = 0, end_time = 0;

    struct iothread_args * args = NULL;

    srand( (int32_t)time(NULL) );
    signal( SIGINT, signal_handler );

    //
    threadgroup = iothreads_start( nthreads, 0, task_method, NULL );
    if ( threadgroup == NULL )
    {
        printf("iothreads_start() failed \n");
        return -1;
    }

    args = (struct iothread_args *)calloc( nthreads, sizeof(struct iothread_args) );
    if ( args == NULL )
    {
        printf("calloc for 'iothread_args' failed \n");
        return -2;
    }

    //
    runflags = 1;

    //
    start_time = milliseconds();
    while ( runflags )
    {
        uint8_t _index = index&(nthreads-1);
        struct iothread_args * _args = args + _index;

        iothreads_post( threadgroup, _index, 0, _args, 0 );
        ++index;
    }
    end_time = milliseconds();

    //
    iothreads_stop( threadgroup );

    //
    for ( i = 0; i < nthreads; ++i )
    {
        struct iothread_args * _args = args + i;
        float rate = (float)(_args->taskcount)*1000.0f/(float)(end_time-start_time);

        printf("iothread[%d] process tasks: %ld, rate: %6.3f\n", i, _args->taskcount, rate );
    }

    printf("dispatch tasks: %ld, rate: %6.3f\n", index, (float)(index)*1000.0f/(float)(end_time-start_time) );

    return 0;
}