广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >C++中标准线程库的基本使用介绍
  • 362
分享到

C++中标准线程库的基本使用介绍

2024-04-02 19:04:59 362人浏览 八月长安
摘要

目录1.创建线程异步执行2.通过使用互斥锁防止线程冲突3.采用信号量控制线程的运行4.通过promise实现进程间通信总结Qt的封装程度比较高的线程类用多了,发现c++标准库里面的线

Qt的封装程度比较高的线程类用多了,发现c++标准库里面的线程库有些生疏。这里总结一下C++标准库里面的线程相关内容,供大家参考使用。其实标准C++的线程库也是挺好用的。

1.创建线程异步执行

我们可以通过async函数直接异步创建一个线程,这种方法相对来说比较简单,线程执行的结果可以直接用future<T>来进行获取。

#include <iOStream>
#include <future>
 
//线程对应的函数
bool thread_func(int x) {
	return true;
}
int main()
{
	int inputNum = 65547;
	std::future<bool> future = std::async(thread_func, inputNum);
	bool ret = future.get();
	getchar();
}

2.通过使用互斥锁防止线程冲突

线程间同步读取内容的话一般不会出现线程安全问题,但如果线程间同步写同一个内容的话就容易出现冲突。比如每个线程执行一次,就会给全局执行次数累加一次,如果多个线程同时执行操作,在写的时候没有加锁,这就有可能导致执行次数被重复累加的情况。

#include <iostream>
#include <thread>
#include <mutex>
std::mutex mtx; 
 
int count=0;
 
void print_block(int n) {
	mtx.lock();
   count++;
	//do somethings
	mtx.unlock();
}
int main()
{
	std::thread thread1(print_block, 50);
	std::thread thread2(print_block, 50);
 
	thread1.join();
	thread2.join();
	getchar();
	return 0;
}

3.采用信号量控制线程的运行

条件变量(condition_variable)用来控制线程的运行,线程启动的时候如果条件变量等待,会阻塞线程的运行,直到条件变量发送对应的通知线程才能开始运行。通过采用条件变量我们可以控制线程的运行,避免线程空运行消耗计算资源。

#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
 
std::mutex mtx;
std::condition_variable cv;
 
void print_id(int id) {
	std::unique_lock<std::mutex> lck(mtx);
	cv.wait(lck);
	std::cout << "thread " << id << '\n';
}
void Go() {
	std::unique_lock<std::mutex> lck(mtx);
	cv.notify_all();
}
int main()
{
	std::thread threads[10];
	for (int i = 0; i < 10; ++i)
		threads[i] = std::thread(print_id, i);
   std::cout << "start thread run" << std::endl;
	go();
	for (auto& th : threads){th.join();}
	getchar();
	return 0;
}

4.通过promise实现进程间通信

很多时候线程间执行是有先后顺序的,我们需要等待上一个线程执行结束拿到结果之后再执行当前线程,这时候就涉及到线程间的等待和数据传递这时候std::promise<T>就能排上用场了,通过使用该变量我们可以很轻松的实现线程间的等待和数据传递。

#include <iostream>
#include <future>
#include <chrono>
void Thread_Fun1(std::promise<int> &p)
{
	std::this_thread::sleep_for(std::chrono::seconds(5));
	int iVal = 233;
	std::cout << "传入数据(int):" << iVal << std::endl;
	p.set_value(iVal);
}
 
void Thread_Fun2(std::future<int> &f)
{
	//阻塞函数,直到收到相关联的std::promise对象传入的数据
	auto iVal = f.get();
	std::cout << "收到数据(int):" << iVal << std::endl;
}
 
int main()
{
	std::promise<int> pr1;
	std::future<int> fu1 = pr1.get_future();
 
	std::thread t1(Thread_Fun1, std::ref(pr1));
	std::thread t2(Thread_Fun2, std::ref(fu1));
 
	//阻塞至线程结束
	t1.join();
	t2.join();
	return 1;
}

总结

到此这篇关于C++中标准线程库的基本使用介绍的文章就介绍到这了,更多相关C++标准线程库内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: C++中标准线程库的基本使用介绍

本文链接: https://www.lsjlt.com/news/138302.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作