c/c++ linux服务器开发相关视频解析:
后台开发第178讲|【零声学院官方视频】epoll原理剖析以及reactor模型应用|1. epoll原理剖析 2. 单reactor原理以及应用
聊聊线程面试那些事||线程起源||线程模型||进程和线程的联系和区别||为什么需要线程池
c/c++ linux服务器开发免费学习地址:c/c++ linux后台服务器高级架构师
C++11的多线程类thread
C++11之前,C++库中没有提供和线程相关的类或者接口,因此在编写多线程程序时,Windows上需要调用CreateThread创建线程,Linux下需要调用clone或者pthread线程库的接口函数pthread_create来创建线程。但是这样是直接调用了系统相关的API函数,编写的代码,无法做到跨平台编译运行。
C++11之后提供了thread线程类,可以很方便的编写多线程程序(注意:编译器需要支持C++11之后的语法),代码示例如下:
#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(sellTicketTask, "车票窗口二");
std::thread t3(sellTicketTask, "车票窗口三");
// 等待三个线程执行完成
t1.join();
t2.join();
t3.join();
return 0;
}
通过上面的代码可以看到,C++11的mutex和Linux平台下pthread线程库的pthread_mutex_t互斥锁使用几乎是一样的(实际上在Linux平台下mutex就是调用的pthread_mutex_t互斥锁相关的系统函数),mutex也支持trylock活锁机制,可以自己进行测试。