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 );
}

  

posted @ 2011-08-10 14:08  lxgeek  阅读(365)  评论(0编辑  收藏  举报