C implementation of a simple thread pool

Source: Internet
Author: User
Tags define include printf sleep thread
//threadpool.h #ifndef __threadpool_h__ #define __THREADPOOL_H__ #include <pthread.h>    
typedef void* (*task_fun) (void*);    
    Use linked list to maintain wait task typedef struct THREADTASK {//Task execution function Task_fun tasks;    
    Executes the parameter of the function void* arg;    
Next node struct threadtask* next;    
        
}thread_task;     
void* Thr_fun (void* arg)//function int init_task (void) for each thread;//Initialize wait task List int init_pool (void);//Initialize thread pool//Add Task to Task List    
int Add_task (task_fun task, void* Arg);    
int destroy_poll (void); #endif 
THREADPOOL.C///For the basic error does not occur without detection #include <stdio.h> #include <stdlib.h> #include ;unistd.h> #include <string.h> #include the maximum number of threads allowed in the threadpool.h/thread pool #define Threadpoo    
Lsize 3//The number of currently waiting tasks is static unsigned int taskwaittingnum=0;    
Taskwaittingnum lock pthread_mutex_t g_nummutex= pthread_mutex_initializer;    
Wait for task queue lock pthread_mutex_t G_taskmutex=pthread_mutex_initializer;    
Thread_task* G_ptask=null;    
pthread_t Tid[threadpoolsize];    
Whether to destroy the thread pool static int isshut=0;    
    void* Thr_fun (void* Arg) {Task_fun task;    
    void* Funarg=null;    
            
    Thread_task* Ptmp=null; while (1) {//If you want to destroy the thread pool, jump out while loop if (1==isshut) {break;//jumps out of W    
        Hile cycle} pthread_mutex_lock (&g_nummutex);    
      If there are currently no tasks to schedule, hibernate 5000 subtle if (0==taskwaittingnum) {      Pthread_mutex_unlock (&g_nummutex);    
            Usleep (5000);    
        Continue //There is currently a task scheduler else {//need to write to the linked list, so lock the list pthread_mutex_lock (&am    
            P;g_taskmutex);    
            task=g_ptask->next->task;    
            funarg=g_ptask->next->arg;    
            ptmp=g_ptask->next;    
            g_ptask->next=ptmp->next;    
            taskwaittingnum--;    
            Pthread_mutex_unlock (&g_taskmutex);    
                    
            Pthread_mutex_unlock (&g_nummutex);    
            Free (ptmp);    
            Ptmp=null; (*task)    
        (Funarg);    
} pthread_exit (NULL);    
    }//Initialize task list, first node does not need to store task int init_task (void) {g_ptask= (thread_task*) malloc (sizeof (thread_task));    
        if (null==g_ptask) {printf ("Init_task malloc fails./n");    
    return-1; } memset (G_Ptask, 0, sizeof (thread_task));    
    g_ptask->next=null;    
return 0;    
    }//Initialize thread pool int init_pool (void) {int ret;    
    int i;    
        for (i=0 i<threadpoolsize; i++) {ret=pthread_create (tid+i, NULL, thr_fun, NULL);    
            if (ret!=0) {printf ("Init_pool pthread_create:/n%s/n", Strerror (ret));    
        return-1;       
} return 0;    
    int Add_task (task_fun task, void* Arg) {thread_task* ptmp=null;    
    Pthread_mutex_lock (&g_nummutex);    
    Pthread_mutex_lock (&g_taskmutex);    
    Ptmp=g_ptask;    
    while (ptmp->next!=null) {ptmp=ptmp->next;    
    } ptmp->next= (thread_task*) malloc (sizeof (thread_task));    
        if (null==ptmp->next) {printf ("Add_task malloc fails/n");    
    return-1;    
    } ptmp=ptmp->next;   ptmp->task=task; 
    ptmp->arg=arg;    
    ptmp->next=null;    
    taskwaittingnum++;    
    Pthread_mutex_unlock (&g_nummutex);    
    Pthread_mutex_unlock (&g_taskmutex);    
return 0;    
    int Destroy_pool (void) {isshut=1;    
    Pthread_mutex_destroy (&g_nummutex);    
    Pthread_mutex_destroy (&g_taskmutex);    
return 0; }
Used to test the main.c    
#include    <stdio.h>    
#include    "threadpool.h"    
void* task1 (void* Arg)    
{    
    printf ("Task1 is running!") /n ");    
    Sleep (a);    
    printf ("Task1 run over!/n");    
    return NULL;    
}    
void* Task2 (void* Arg)    
{    
    printf ("Task2 is running!) /n ");    
    Sleep (a);    
    printf ("Task2 run over!") /n ");    
    return NULL;    
}    
void* task3 (void* Arg)    
{    
    printf ("TASK3 is running!) /n ");    
    printf ("Task3 run over!") /n ");    
    return NULL;    
}    
void* task4 (void* Arg)    
{    
    printf ("TASK4 is running!) /n ");    
    printf ("Task4 run over!") /n ");    
    return NULL;    
}    
int main (int argc, char *argv[])    
{    
    init_task ();    
    Init_pool ();    
    Add_task (Task1, NULL);    
    Add_task (Task2, NULL);    
    Add_task (TASK3, NULL);    
    Add_task (TASK4, NULL);    
    Sleep (a);    
    Destroy_pool ();    
    return 0;    
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.