• a threadpool by c


    #include <stdio.h>
    #include <stdlib.h>
    #include <malloc.h>
    #include <assert.h>
    #include <pthread.h>
    
    #define FALSE 0
    
    pthread_mutex_t thread_mutex = PTHREAD_MUTEX_INITIALIZER;
    pthread_mutex_t task_mutex = PTHREAD_MUTEX_INITIALIZER;
    struct temp
    {
            int name;
    };
    
    #define STACK_TYPE struct temp*
    
    typedef struct STACK_NODE
    {
            STACK_TYPE value;
            struct STACK_NODE *next;
    } StackNode;
    
    static StackNode *stack;
    static int task_num = 0;
    /**
     * create_stack
     */
    void
    create_stack( size_t size )
    {
    
    }
    
    /**
     * Push
     */
    void
    push( int value )
    {
            pthread_mutex_lock( &thread_mutex );
    
            StackNode *new_node;
    
            new_node = malloc( sizeof( StackNode ) );
            assert( new_node != NULL );
            new_node->value = malloc( sizeof( struct temp ) );
            assert( new_node->value != NULL );
            (new_node->value)->name = value;
            new_node->next = stack;
            stack = new_node;
    
            pthread_mutex_unlock( &thread_mutex );
    }
    
    /**
     * is_empty
     */
    int
    is_empty( void )
    {
            return stack == NULL;
    }
    
    /**
     * Pop
     */
    void
    pop( void )
    {
            pthread_mutex_lock( &thread_mutex );
    
            StackNode *first_node;
    
            assert( !is_empty() );
            first_node = stack;
            stack = first_node->next;
            free( first_node );
    
            //printf( "task_num is %d\n", task_num );
            pthread_mutex_unlock( &thread_mutex );
    }
    
    
    /**
     * top
     */
    int
    top( void )
    {
            //assert( !is_empty() );
            pthread_mutex_lock( &thread_mutex );
            assert( !is_empty() );
            int name = stack->value->name;
            pthread_mutex_unlock( &thread_mutex );
            return name;
    
    
    }
    
    /**
     * is_full
     */
    int
    is_full( void )
    {
            return FALSE;
    }
    
    
    /**
     * destroy_stack
     */
    void
    destory( void )
    {
            pthread_mutex_lock( &thread_mutex );
    
            while( !is_empty() )
                    pop();
    
            pthread_mutex_unlock( &thread_mutex );
    }
    
    
    int
    get_task()
    {
            pthread_mutex_lock( &task_mutex );
            int num = task_num;
            pthread_mutex_unlock( &task_mutex );
            return  num;
    }
    
    void
    delete_task()
    {
            pthread_mutex_lock( &task_mutex );
            task_num--;
            pthread_mutex_unlock( &task_mutex );
    }
    
    
    void*
    thread_handle( void* arg )
    {
            int name;
            while( get_task() != 0 )
            {
                    name = top();
                    //printf( "thread id is %uld and value is %d\n", pthread_self(), name );
                    pop();
                    delete_task();
                    printf( "thread id is %uld and value is %d\n", pthread_self(), name );
    
            }
    
    }
    
    
    int
    main( int argc, char** argv )
    {
            int i = 0;
    
            if ( argc != 3 )
            {
                    printf( "./t20 n m\n" );
                    exit( 1 );
            }
    
            int n_thread = atoi( argv[1] );
            int n_task = atoi( argv[2] );
            task_num = n_task;
    
            for ( ; i < n_task; i++ )
                    push( i );
    
            pthread_t *t;
            t = malloc( sizeof( pthread_t ) * n_thread );
            assert( t != NULL );
    
            for ( i = 0; i < n_thread; i++ )
            {
                    pthread_create( (t + i), NULL, thread_handle, NULL );
                    //pthread_join( *(t + i), NULL );
            }
    
            for ( i = 0; i < n_thread; i++ )
            {
                    pthread_join( *(t + i), NULL );
            }
    
            free( t );
            exit( 0 );
    }
    

      

  • 相关阅读:
    Eric 的随机文件名 生成方法
    .NET模板引擎 EFPlatform.CodeGenerator (代码生成器 静态页生成 生成HTML 模板)
    Eric的行政区(省市)下列表控件和标签控件
    下载网页中远程图片的方法
    一起来玩Twister(扭扭乐)
    ASP.NET 安全认证(四)——巧妙实现 Form 表单认证跨站点、跨服务器的单点登录(Single Sign On) .
    DataList的编辑、更新、删除、全选、分页以及 <EditItemTemplate>模版中的DropDownList的数据绑定
    ASP.NET 安全认证(三)—— 用Form 表单认证实现单点登录(Single Sign On) .
    C# 中的委托和事件
    ASP.Net获取当前运行文件的文件名称
  • 原文地址:https://www.cnblogs.com/lxgeek/p/2133639.html
Copyright © 2020-2023  润新知