C++11 - thread多线程编程,线程互斥和同步通信,死锁问题分析解决

知识的学习在于点滴记录,坚持不懈;知识的学习要有深度和广度,不能只流于表面,坐井观天;知识要善于总结,不仅能够理解,更知道如何表达!

C++11的多线程类thread

C++11之前,C++库中没有提供和线程相关的类或者接口,因此在编写多线程程序时,Windows上需要调用CreateThread创建线程,Linux下需要调用clone或者pthread线程库的接口函数pthread_create来创建线程。但是这样是直接调用了系统相关的API函数,编写的代码,无法做到跨平台编译运行。

C++11之后提供了thread线程类,可以很方便的编写多线程程序(注意:编译器需要支持C++11之后的语法,推荐VS2017,g++4.6版本以上),代码示例如下:

#include <iostream>
#include <thread>
#include <string>
using namespace std;

// 线程1的线程函数
void threadProc1()
{
   
   
	cout << "thread-1 run begin!" << endl;
	// 线程1睡眠2秒
	std::this_thread::sleep_for(std::chrono::seconds(2));
	cout << "thread-1 2秒唤醒,run end!" << endl;
}

// 线程2的线程函数
void threadProc2(int val, string info)
{
   
   
	cout << "thread-2 run begin!" << endl;
	cout << "thread-2 args[val:" << val << ",info:" << info << "]" << endl;
	// 线程2睡眠4秒
	std::this_thread::sleep_for(std::chrono::seconds(4));
	cout << "thread-2 4秒唤醒,run end!" << endl;
}
int main()
{
   
   
	cout << "main thread begin!" << endl;

	// 创建thread线程对象,传入线程函数和参数,线程直接启动运行
	thread t(threadProc1);
	thread t1(threadProc2, 20, "hello world");

	// 等待线程t和t1执行完,main线程再继续运行
	t.join();
	t1.join();

	cout << "main thread end!" << endl;
	return 0;
}

代码运行打印如下

main thread begin!
thread-1 run begin!
thread-2 run begin!
thread-2 args[val:20,info:hello world]
thread-1 2秒唤醒,run end!
thread-2 4秒唤醒,run end!
main thread end!

可以看到,在C++语言层面编写多线程程序,用thread线程类非常简单,定义thread对象,只需要传入相应的线程函数和参数就可以了。

上面同样的代码在Linux平台下面用g++编译:
g++ 源文件名字.cpp -lpthread
【注意】:需要链接pthread线程动态库,所以C++的thread类在Linux环境下使用的就是pthread线程库的相关接口。

然后用strace命令跟踪程序的启动过程:
tony@tony-virtual-machine:~/code$ strace ./a.out

有如下打印输出:
在这里插入图片描述
说明C++ thread线程对象启动线程的调用过程就是 thread->pthread_create->clone,还是Linux pthread线程库使用的那一套,好处就是现在可以跨平台编译运行了,在Windows上当然调用的就是CreateThread系统API创建线程了。

线程互斥

在多线程环境中运行的代码段,需要考虑是否存在竞态条件,如果存在竞态条件,我们就说该代码段不是线程安全的,不能直接运行在多线程环境当中,对于这样的代码段,我们经常称之为临界区资源,对于临界区资源,多线程环境下需要保证它以原子操作执行,要保证临界区的原子操作,就需要用到线程间的互斥操作-锁机制,thread类库还提供了更轻量级的基于CAS操作的原子操作类。

下面用模拟3个窗口同时卖票的场景,用代码示例一下线程间的互斥操作。

thread线程类库的互斥锁mutex

下面这段代码,启动三个线程模拟三个窗口同时卖票,总票数是100张,由于整数的- -操作不是线程安全的操作,因为多线程环境中,需要通过加互斥锁做到线程安全,代码如下示例:

// 车票总数是100张
volatile int tickets = 100; 
// 全局的互斥锁
std::mutex mtx;

// 线程函数
void sellTicketTask(std::string wndName)
{
   
   
	while (tickets > 0)
	{
   
   
		// 获取互斥锁资源
		mtx.lock();
		if (tickets > 0)
		{
   
   
			std::cout << wndName << " 售卖第" << tickets << "张票" << std::endl;
			tickets--;
		}
		// 释放互斥锁资源
		mtx.unlock();

		// 每卖出一张票,睡眠100ms,让每个窗口都有机会卖票
		std::this_thread::sleep_for(std::chrono::milliseconds(100));
	}
}

// 模拟车站窗口卖票,使用C++11 线程互斥锁mutex
int main()
{
   
   
	// 创建三个模拟窗口卖票线程
	std::thread t1(sellTicketTask, "车票窗口一");
	std::thread t2
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值