C++多线程thread

 

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

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值