Home > Back-end >  C implementation thread class and thread pool class of the second building
C implementation thread class and thread pool class of the second building

Time:01-30

http://https://bbs.csdn.net/topics/399031782? content with the first building Page=1 # post - 414118971

CodePudding user response:

Layer 2 reference 1 building xiaoyin35 response:
And then the ThreadPool class:

ThreadPool tp1.//create a thread pool

ThreadPool tp2 (5);//create a thread pool and create five thread

Tp1. CreateThread (n);//new n threads in a thread (the default parameter=1)

And:

# # #

Is the use of thread:

Tp1. GetThread (n);//grab n threads to perform a task in the queue n tasks (default=1) parameters, if n is more than the number of jobs, and the rest of the idle thread, each running a delete a task (tp1. TaskQueue. Pop ())

Tp1. TaskQueue//tp1 Task queue (average queue, for example, define a class Task; Then use queue TaskQueue;//public implementation,)
Tp1. TaskQueue. Push (* (new Task (command, args)));//add a task, function of the command and parameter args
Tp1. TaskQueue. Pop ();//you can delete this function (can't directly access (protected), but can slot in the ThreadPool this Queue class, let the ThreadPool can use pop (),)
Tp1. TaskQueue. The size ();//length, you can set the function: tp1 GetTaskNumber () block,

CodePudding user response:

The first building site have a mistake!


The method of thread pool:

GetThread will automatically start the thread n

Tp1. AllWait ();//waiting for is now running thread end

Tp1. Sleep ()... He said little, but the parameter is: (index,... ) the index refers to is which thread, each thread in the thread pool can label, GetThread will return when their label!

I realized some specific Queue,
  • Related