Design method of two kinds of UNIX network programming thread pool

Source: Internet
Author: User

In the Unp27 chapter 27.12, our sub-threads are using the shared task buffer to get the task, that is, the clifd[] array that is shared between threads, which is actually our task array, which is CONNFD resources.

Our operation of this task array requires a mutex + condition variable to achieve the purpose of synchronization: Each thread is irregular from CLIFD to get the task and then executes. There is no correspondence between tasks and threads. After the thread finishes this task, if there are any tasks in the task array, run the next task again.

In another thread pool model, pthread_create (&temp[i].tid, NULL, Child_work, (void *) &temp[i]), you can know that each thread is running chlid_ The child (struct pthread_node*) function. The parameter of this function is our thread structure struct Pthread_node (representing a thread).

We establish a relationship between the thread structure and the task, and each thread (represented by a thread structure) corresponds to a task.

And then child_work, as long as the work variables in our thread structure (pointing to a task structure) are not NULL, we start to run the program .... (The difference with UNP is that the relationship between our threads and tasks is set up before, but it's not a good thing, just putting the UNP of the thread to the task in the Thread_manger function.)

Essentially, you get the task randomly.

Pthread_create (&thread_manager_tid, NULL, Thread_manager, NULL);

The runtime of this thread is the part that is not in UNP. The main purpose is to get the idle thread structure from the idle thread structure, and to establish a corresponding relationship between the idle thread structure and the task so that the child_work thread does not operate the task pool directly, as UNP does.

The fundamental difference is that we have the structure of the thread pool, and the threading structure. So we have a data structure that monitors idle threads and worker threads, facilitating the expansion of future monitoring operations.

typedef struct PTHREAD_QUEUE
{
int number; /* The number of the thread in this queue. */
struct Pthread_node *head;
struct Pthread_node *rear;
pthread_cond_t cond; /* When no idle thread, the manager is wait for, or if a thread return with idle, signal. */
pthread_mutex_t Mutex;
} pthread_queue_t;

If not for this function, we use the method in UNP to be simpler. Regardless of the design, if the thread is not destroyed, all of our threads are executing. The so-called idle thread is just a blocked thread. The resources for the thread itself are still there.

Design method of two kinds of UNIX network programming thread pool

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.