目录
1. 共享内存
先回顾一下前面几章中用到的,一个存在资源竞争的例子:
#include<iostream>
#include<thread>
void ThreadFunc() {
for (int i = 1; i <= 8; ++i)
std::cout << "thread function: " << i << "\n";
}
int main()
{
std::thread thr(&ThreadFunc);
for (int i = 1; i <= 8; ++i)
std::cout << "main thread: " << i << "\n";
thr.join();
return 0;
}
运行结果为:
main thread: 1
main thread: 2
main thread: 3
main thread: 4
main thread: 5
main thread: 6
main thread: 7
thread function: main thread: 8
1
thread function: 2
thread function: 3
thread function: 4
thread function: 5
thread function: 6
thread function: 7
thread function: 8
可以看到,结果是无序的。
2.使用互斥量mutex
下面使用互斥对线程间进行同步。
#include<iostream>
#include<thread>
#include<string>
#include<mutex>
std::mutex mu;
void PrintFunc(std::string msg, int id) {
mu.lock();
std::cout<<msg<<":"<<id<<std::endl;
mu.unlock();
}
void ThreadFunc() {
for(int i=1;i<=8;++i)
PrintFunc("thread function", i);
}
int main()
{
std::thread thr(&ThreadFunc);
for(int i=1;i<=8;++i)
PrintFunc("main thread", i);
thr.join();
return 0;
}
运行结果为:
main thread:1
thread function:1
thread function:2
thread function:3
main thread:2
main thread:3
main thread:4
thread function:4
thread function:5
thread function:6
main thread:5
main thread:6
main thread:7
main thread:8
thread function:7
thread function:8
3.共享数据问题
多线程之间共享数据的问题,很大程度上源自对数据的修改顺序。
如果共享数据是只读的,则不会存在问题,因为一个线程读数据不会影响到另一个线程读取相同数据。但是,但一个线程或多个线程可以修改数据时,问题就出现了。
下面介绍的互斥就是用于解决这种问题的。
4.互斥量mutex释放
下面例子中多个线程同时访问相同的list
#include<iostream>
#include<thread>
#include<list>
#include<algorithm>
std::list<int> myList;
void AddToList(int max, int interval) {
for(int i=0;i<max;++i) {
if(i%interval==0)
myList.push_back(i);
}
}
void PrintList() {
for(auto &iter: myList)
std::cout<<iter<<",";
}
int main() {
int max = 15;
std::thread t1(AddToList, max, 1);
std::thread t2(AddToList, max, 2);
std::thread t3(PrintList);
t1.join();
t2.join();
t3.join();
return 0;
}
运行结果为:
0,2,4,6,8,10,12,14,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,
或者
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,2,4,6,8,10,12,14,
在对list的操作过程中,t1会插入0到9的整数,而t2会插入每个2的倍数。同时,t3会打印list元素。上面的运行结果显示,打印完全处于无序状态。
互斥可以说是C++的数据保护中使用最广泛的技术。它可以保护数据,避免资源竞争,但同时也会带来死锁,或者保护了太多的或太少的数据(互斥范围选择),等问题。
C++中可以使用std::mutex()创建实例,调用lock()来锁定资源,unlock()释放资源。但是,可能存在unLock()没有被调用进行释放。
例如,lock()之后并且unlock()之前,抛出了一个异常。这样导致资源一直处于被锁状态。所以,在标准C++库中,提供了std::lock_guard类模板。它在构造时会锁定指定的mutex, 析构时解锁,确保一个锁定的资源最后都能解锁。
#include<iostream>
#include<thread>
#include<list>
#include<algorithm>
#include<mutex>
std::list<int> myList;
std::mutex myMutex;
void AddToList(int max, int interval) {
std::lock_guard<std::mutex> guard(myMutex);
for(int i=0;i<max;++i) {
if(i%interval==0)
myList.push_back(i);
}
}
void PrintList() {
std::lock_guard<std::mutex> guard(myMutex);
for(auto &iter: myList)
std::cout<<iter<<",";
}
int main() {
int max = 15;
std::thread t1(AddToList, max, 1);
std::thread t2(AddToList, max, 2);
std::thread t3(PrintList);
t1.join();
t2.join();
t3.join();
return 0;
}
运行的其中一种结果为:
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,2,4,6,8,10,12,14,
从上看到,2的整数倍的数是连续的。加了互斥机制后,保证了多线程的顺序性。