C++11引入了多线程, 在编译时, 一般需要添加 "-std=c++11"参数
目录
一、主线程等待子线程运行完继续运行
【子线程需要用到 join() 函数来阻塞主线程执行】
#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); //thead01为函数名
thread task02(thread02);
task01.join();
task02.join();
for (int i = 0; i < 5; i++)
{
cout << "Main thread is working !" << endl;
Sleep(200);
}
return 0;
}
输出结果:
二、主线程和子线程单独分开执行
【子线程需要用到 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;
}
return 0;
}
运行结果(子线程未运行完停止了):
- 主线程结束时间 晚于 子线程结束时间
#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(300); // <<-------
}
return 0;
}
输出结果:
三、带参数子线程
在创建线程对象时,参数依次如下:(函数名,函数参数)
#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, int time)
{
for (int i = 0; i < num; i++)
{
cout << "Thread 02 is working!" << endl;
Sleep(time);
}
}
int main()
{
thread task01(thread01, 5); //带参数子线程
thread task02(thread02, 5, 100);
task01.join();
task02.join();
return 0;
}
输出结果:
四、互斥锁(防止多个线程操作一个变量产生的异常)
【需要包含 mutex 头文件】
- 未加互斥锁
#include <iostream>
#include <thread>
#include <Windows.h>
using namespace std;
int totalNum = 20;
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.join();
task02.join();
return 0;
}
发现输出异常:
- 加入互斥锁
#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");
}
五、参考博客
C++使用thread类多线程:https://blog.youkuaiyun.com/dcrmg/article/details/53912941