A simple thread pool implementation---need to be further improved

Source: Internet
Author: User

1. Define a task structure body, and a thread pool structure

struct task{
void * (*p) (void*);//functions that need to be implemented;
Parameters with void *arg;//function
struct task *next;
};
struct pthread_pool{
pthread_mutex_t mutex;//Line Lock
pthread_cond_t cond;//Condition Variable
pthread_t *tids;//Thread ID
int thread_nums;//The number of threads to be created
struct task *head; task Header node
The int cur_queue_size;//is marked as a blocking wait for the control condition variable
};

2. Add Task----Initialize task list, add node

Pool_add_task (void* (*pro) (void *), void *arg)
{
struct Task *new = malloc (sizeof (struct task));//Initialize the task structure body
New->p = Pro;
New->arg = arg;
New->next = NULL;
Pthread_mutex_lock (& (Pool->mutex));//When working with public resources, remember to always lock and unlock
struct Task *lastnode = pool->head;
if (lastnode = = NULL)//Two cases, determine if the head node is empty
{
Pool->head = new;
}

//????? If you change to Lastnode = new in the previous sentence, a segment error occurs. No solution
Else
{
while (Lastnode->next!=null)//The first node is not empty
{
Lastnode = lastnode->next;
}
Lastnode->next = new;
}
pool->cur_queue_size++;//Each node is added, Mark bit +1.
printf ("%d\n", * (int *) (new->arg));
printf ("%d\n", pool->cur_queue_size);
Pthread_mutex_unlock (& (Pool->mutex));
Pthread_cond_signal (&pool->cond);//Send wake-up signal to a thread that is in a blocking state
}

3. Thread initialization----Create thread (function function)

void Pool_init (int thread_num)
{

Initialize the thread pool struct body
Pool = malloc (sizeof (struct pthread_pool));
Pthread_mutex_init (& (Pool->mutex), NULL);
Pthread_cond_init (& (Pool->cond), NULL);
Pool->thread_nums = Thread_num;
Pool->head = NULL;
pool->cur_queue_size = 0;
Pool->tids = malloc (thread_num*sizeof (pthread_t));
int i = 0;
for (i=0;i<thread_num;i++)
{
Pthread_create (& (Pool->tids[i]), null,thread_r,null);
printf ("Pool init\n");
}
}

4. Task function (from the head of the task list to the node, execute the function in the structure)

void* Thread_r (void*p)
{
while (1)
{

Pthread_mutex_lock (& (Pool->mutex));
while (pool->cur_queue_size = = 0)
printf ("wait\n"); Ok
Pthread_cond_wait (& (Pool->cond),& (Pool->mutex));

printf ("wait!\n");
struct Task *q = pool->head;
printf ("Q jiedian\n");
Pool->head = q->next;
printf ("pool->next\n");
Q->next = NULL;
printf ("Put in error\n");
pool->cur_queue_size--;
Pthread_mutex_unlock (&pool->mutex);
printf ("pthred_r\n");
(* (Q->P)) (q->arg);//Perform a function in a struct
Free (q);
Q = NULL;
if (pool->cur_queue_size ==0)
Break
}
}

5. Function pointers in function functions.

void *f1 (void *arg)

{

int a = * (int*) arg;

Sleep (1);

printf ("%d\n", a);

}

Main function: (How to destroy thread pool, need to be solved)

int main ()
{
Pool_init (3);
printf ("Pthread pool is start\n");
Sleep (1);
int a=12;
Pool_add_task (F1, (void*) &a);
Sleep (2);
printf ("pool_add_task\n");
Pool_add_task (F1, (void*) &a);
Pool_add_task (F1, (void*) &a);
Sleep (5);
Pthread_join (Pool->tids[0],null);
Pthread_join (Pool->tids[1],null);
Pthread_join (Pool->tids[2],null);

}

A simple thread pool implementation---need to be further improved

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.