注:
对于报错:
我用的DEV C++,为什么报错说我没定义? [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 我就是把博主你的task01 02换成了t1 t2其他的都没变,不换也是这样的
解决方案:
对于报错:
#include <iostream>
#include <thread>
using namespace std;
void hello()
{
cout<<"hello kitty"<<endl;
}
int main()
{
std::thread t(hello);
t.join();
return 0;
}
会出现编译错误,显示thread未在当前域中声明,也就是这个版本的编译器不支持了。
解决方案:
下载最新版本的带gcc编译器的CodeBlocks就成功了!!
配置自带的编译器就可以。
CodeBlocks自带的都是TDM-GCC,没用过,但是已经支持std::thread了。
环境说明:
测试时间:2016年6月24日
操作系统:Windows 7 64bits OS
CodeBlocks版本:16.01
安装包:codeblocks-16.01mingw-setup.exe
下载链接:http://www.codeblocks.org/downloads/26
CodeBlocks配置:
Settings -》 Compiler
设置编译器位置:(注意每次更换编译器安装路径后,下面的程序文件路径都要重新选择一遍,默认还是在上一次配置的编译器的子目录,为确保准确,请从根目录重新选择。)
勾选c++支持:
对上面的程序进行测试:
正文:
C++11 中引入了一个用于多线程操作的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");
}
两个子线程并行执行,join函数会阻塞主线程,所以子线程都执行完成之后才继续执行主线程。可以使用detach将子线程从主线程中分离,独立运行,不会阻塞主线程。
#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");
}
带参子线程
在绑定的时候也可以同时给带参数的线程传入参数。
#include <iostream>
#include <thread>
#include <Windows.h>
using namespace std;
//定义带参数子线程
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); //带参数子线程
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");
}
多线程数据竞争
多个线程同时对同一个变量进行操作的时候,如果不对变量做一些保护处理,有可能导致处理结果异常:
#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; //线程互斥对象
int totalNum = 100;
void thread01()
{
while (totalNum > 0)
{
mu.lock(); //同步数据锁
cout << totalNum << endl;
totalNum--;
Sleep(100);
mu.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");
}