Home > Back-end >  C implementation thread and thread pool
C implementation thread and thread pool

Time:01-30

This Ruo chrysanthemum to bosses to ask, what I use is Dev c + + 5.1. Is actually a simple effect in Python thread class (simulated) :
 
#include
#include
//thread and thread pool class, in the disk C
#include
#include
//or together: # include
using namespace std;

Int inplus ()
{
int a, b;
Cout & lt; <"Enter a, b:";
Cin & gt;> A. & gt;> b;
Int c=a + b;
Cout & lt; <"Answer:" & lt; Return the c;
}

Void sortlist (int len)
{
Int * list=new int [len];
for (int i=0; I & lt; Len. Cin i++) & gt;> The list [I];
Sort (a list, the list + len + 1);
Cout & lt; <"Sorting result:";
for (int i=0; I & lt; Len. I++) cout & lt; Cout & lt; }

Int main ()
{
Thread the t1 (inplus), t2 (sortlist, 5);//define (not ready)
T1. Start ();//start (start ready, waiting for running)
T2. Start ();
T1. Wait ();//waiting for the end of the run
T2. Wait ();
//... Other effects behind said
return 0;
}

The effect to comment me down,

CodePudding user response:

Say first Thread class:
1. The definition (not ready)

The Thread name (function, args... );

Is to define a name is the name of the Thread object, the function of any return values have any parameters is a function, the args is optional/multiple parameters, used to write the function function parameters, such as Thread t1 (sortlist, a number);)

2. Start (ready)

Name. The start ();

Let the name object start

3. Wait for the end

Name. The wait ();

Program can continue to run but remind when object name all the tasks completed the main program, specific zha to remind the user

4. Other

Basic operation: thread sleep (),...

5. An immediate end to the

Such as have a while (1) has been circulating, can use the
Name. The end ()
Monitoring end directly, such as buttons,

CodePudding user 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)

There are:

CodePudding user response:

I am a Windows 10!!!!!!
  • Related