[note] c + + uses thread class to realize multithreading

Posted by newcastle_unite on Wed, 08 Dec 2021 03:49:36 +0100

Note:

For error reporting:

         I use DEV C + +, why do I report an error that I have no definition? [Error] #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options. [Error] 'thread' was not declared in this scope[code=cpp] [Error] 'thread' was not declared in this scope [Error] 't1 'was not declared in this scope [error]' T2 'was not declared in this scope. I changed your task01 02 to t1 t2. Nothing else has changed. It's the same without changing

Solution:

For error reporting:

#include <iostream>
#include <thread>
using namespace std;
 
void hello()
{
    cout<<"hello kitty"<<endl;
}
 
int main()
{
    std::thread t(hello);
    t.join();
    return 0;
}

A compilation error will appear, indicating that the thread is not declared in the current domain, that is, this version of the compiler does not support it.  

Solution:

Download the latest version of CodeBlocks with gcc compiler successfully!!  
Configure the built-in compiler.  
CodeBlocks comes with TDM-GCC, which has never been used, but it already supports std::thread.  
Environmental description:  
Test time: June 24, 2016  
Operating system: Windows 7 64bits OS  
CodeBlocks version: 16.01  
Installation package: codeblocks-16.01mingw-setup.exe  
Download link: http://www.codeblocks.org/downloads/26 
CodeBlocks configuration:  
Settings -> Compiler 
Set the compiler location: (note that after changing the compiler installation path each time, the following program file path must be re selected. By default, it is still in the subdirectory of the last configured compiler. To ensure accuracy, please re select from the root directory.)  

Check c + + support:  

Test the above procedure:  

Text:

A thread class for multithreading operation is introduced in C++11. A simple multithreading example:

Parameterless child thread:

#include <iostream>
#include <thread>
#include <Windows.h>
 
using namespace std;
 
void thread01()
{
	for (int i = 0; i < 5; i++)
	{
		cout << "Thread 01 is working !" << endl;
		Sleep(100);
	}
}
void thread02()
{
	for (int i = 0; i < 5; i++)
	{
		cout << "Thread 02 is working !" << endl;
		Sleep(200);
	}
}
 
int main()
{
	thread task01(thread01);
	thread task02(thread02);
	task01.join();
	task02.join();
 
	for (int i = 0; i < 5; i++)
	{
		cout << "Main thread is working !" << endl;
		Sleep(200);
	}
	system("pause");
}

        The two sub threads execute in parallel, and the join function will block the main thread, so the main thread will not continue to execute until the sub threads are completed. detach can be used to separate sub threads from the main thread and run independently without blocking the main thread.

#include <iostream>
#include <thread>
#include <Windows.h>
 
using namespace std;
 
void thread01()
{
	for (int i = 0; i < 5; i++)
	{
		cout << "Thread 01 is working !" << endl;
		Sleep(100);
	}
}
void thread02()
{
	for (int i = 0; i < 5; i++)
	{
		cout << "Thread 02 is working !" << endl;
		Sleep(200);
	}
}
 
int main()
{
	thread task01(thread01);
	thread task02(thread02);
	task01.detach();
	task02.detach();
 
	for (int i = 0; i < 5; i++)
	{
		cout << "Main thread is working !" << endl;
		Sleep(200);
	}
	system("pause");
}

Child thread with parameters

        When binding, you can also pass parameters to threads with parameters at the same time.

#include <iostream>
#include <thread>
#include <Windows.h>
 
using namespace std;
 
//Define a child thread with parameters
void thread01(int num)
{
	for (int i = 0; i < num; i++)
	{
		cout << "Thread 01 is working !" << endl;
		Sleep(100);
	}
}
void thread02(int num)
{
	for (int i = 0; i < num; i++)
	{
		cout << "Thread 02 is working !" << endl;
		Sleep(200);
	}
}
 
int main()
{
	thread task01(thread01, 5);  //Child thread with parameters
	thread task02(thread02, 5);
	task01.detach();
	task02.detach();
 
	for (int i = 0; i < 5; i++)
	{
		cout << "Main thread is working !" << endl;
		Sleep(200);
	}
	system("pause");
}

Multithreaded data contention

        When multiple threads operate on the same variable at the same time, if some protection processing is not done to the variable, the processing result may be abnormal:

#include <iostream>
#include <thread>
#include <Windows.h>
 
using namespace std;
 
int totalNum = 100;
 
void thread01()
{
	while (totalNum > 0)
	{
		cout << totalNum << endl;
		totalNum--;
		Sleep(100);
	}
}
void thread02()
{
	while (totalNum > 0)
	{
		cout << totalNum << endl;
		totalNum--;
		Sleep(100);
	}
}
 
int main()
{
	thread task01(thread01);
	thread task02(thread02);
	task01.detach();
	task02.detach();
	system("pause");
}

#include <iostream>
#include <thread>
#include <Windows.h>
#include <mutex>
 
using namespace std;
 
mutex mu;  //Thread mutex
 
int totalNum = 100;
 
void thread01()
{
	while (totalNum > 0)
	{
		mu.lock(); //Synchronous data lock
		cout << totalNum << endl;
		totalNum--;
		Sleep(100);
		mu.unlock();  //Unlock
	}
}
void thread02()
{
	while (totalNum > 0)
	{
		mu.lock();
		cout << totalNum << endl;
		totalNum--;
		Sleep(100);
		mu.unlock();
	}
}
 
int main()
{
	thread task01(thread01);
	thread task02(thread02);
	task01.detach();
	task02.detach();
	system("pause");
}

Topics: C++