C++如何使用thread类多线程编程

命运是项羽英雄末路自刎乌江时的那一声仰天长啸,命运是屈原留在汨罗江畔的那一串串沉痛的叩问,命运是贝多芬在双耳失聪时指尖下所击出的那一曲曲悲壮的交响,命运是奥斯特洛夫斯基双目失明后写下的那一页页辉煌的华章。今天给大家说一下C++的thread编程!

C++11中引入了一个用于多线程操作的thread类

下面进行简单演示如何使用,以及如果进行多线程同步。

thread简单示例

复制代码

#include

#include

#include

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.join();

task02.join();

for (int i = 0; i < 5; i++)

{

cout << "Main thread is working !" << endl;

Sleep(200);

}

system("pause");

}

复制代码

输出:

C++如何使用thread类多线程编程

图1

thread detach不阻塞主线程

两个子线程并行执行,join函数会阻塞主流程,所以子线程都执行完成之后才继续执行主线程。可以使用detach将子线程从主流程中分离,独立运行,不会阻塞主线程:

复制代码

#include

#include

#include

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(200);

}

system("pause");

}

复制代码

输出:

C++如何使用thread类多线程编程

图2

使用detach的主线程和两个子线程并行执行。

大家可以多多评论与转发,感谢大家的支持!

thread带参数子线程

在绑定的时候也可以同时给带参数的线程传入参数:

复制代码

#include

#include

#include

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)

{

for (int i = 0; i < num; i++)

{

cout << "Thread 02 is working !" << endl;

Sleep(200);

}

}

int main()

{

thread task01(thread01, 5); //带参数子线程

thread task02(thread02, 5);

task01.detach();

task02.detach();

for (int i = 0; i < 5; i++)

{

cout << "Main thread is working !" << endl;

Sleep(200);

}

system("pause");

}

复制代码

输出:

C++如何使用thread类多线程编程

图3

多线程同步mutex

多个线程同时对同一变量进行操作的时候,如果不对变量做一些保护处理,有可能导致处理结果异常:

复制代码

#include

#include

#include

using namespace std;

int totalNum = 100;

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.detach();

task02.detach();

system("pause");

}

复制代码

部分输出结果:

C++如何使用thread类多线程编程

图4

有两个问题,一是有很多变量被重复输出了,而有的变量没有被输出;二是正常情况下每个线程输出的数据后应该紧跟一个换行符,但这里大部分却是另一个线程的输出。

这是由于第一个线程对变量操作的过程中,第二个线程也对同一个变量进行各操作,导致第一个线程处理完后的输出有可能是线程二操作的结果。针对这种数据竞争的情况,可以使用线程互斥对象mutex保持数据同步。mutex类的使用需要包含头文件mutex。

复制代码

#include

#include

#include

#include

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");

}

复制代码

多线程中加入mutex互斥对象之后输出正常:

C++如何使用thread类多线程编程

图5

最后你觉得我们的文章对你有帮助,欢迎关注我,可以私信我:久伴,领取学习资料,在评论下方可以关注我的学习群,你可以随时在上面向我们提问,把你在学习前端过程中所遇到的问题发给我们。我们每天都会按时回复大家的每一个问题,希望久伴可以伴随你从入门到专家。


分享到:


相關文章: