随笔分类 -  C++11 多线程

摘要:c++11 std::thread 在类的成员函数中、调用其他类的成员函数 阅读全文
posted @ 2021-06-16 17:10 玥茹苟 阅读(832) 评论(0) 推荐(0) 编辑
摘要:std::async 参考: (原创)用C++11的std::async代替线程的创建 - 南哥的天下 - 博客园 (cnblogs.com) 阅读全文
posted @ 2021-06-03 16:40 玥茹苟 阅读(95) 评论(0) 推荐(0) 编辑
摘要:=delete 1. 禁止使用编译器默认生成的函数 class LeafFromTree{ public: LeafFromTree() = default; ~LeafFromTree() = default; LeafFromTree( const LeafFromTree& ) = delet 阅读全文
posted @ 2021-02-24 11:37 玥茹苟 阅读(184) 评论(0) 推荐(0) 编辑
摘要:unique_lock unique_lock独占的是mutex对象,就是对mutex锁的独占。用法:(1)新建一个unique_lock 对象(2)给对象传入一个std::mutex 对象作为参数; std::mutex mymutex; unique_lock lock(mymutex); 因此 阅读全文
posted @ 2021-02-22 15:41 玥茹苟 阅读(3156) 评论(0) 推荐(0) 编辑
摘要:在 Linux 中,有 Pthread; windows 中有CreateThread 与 CloseHandle;但有了 C++11 的 std::thread 以后,你可以在语言层面编写多线程程序了,直接的好处就是多线程程序的可移植性得到了很大的提高,所以作为一名 C++ 程序员,熟悉 C++1 阅读全文
posted @ 2021-01-14 17:37 玥茹苟 阅读(639) 评论(0) 推荐(0) 编辑
摘要:1、for循环嵌套 // 首先确保循环之间没有数据依赖, 尽量在外部使用 int a=0; int b=0; void openmpTest1(int thread_num) { #pragma omp parallel for num_threads(thread_num) for(int i=0 阅读全文
posted @ 2020-06-04 14:19 玥茹苟 阅读(206) 评论(0) 推荐(0) 编辑
摘要:1、unique_ptr //1、使用unique_ptr来管理动态内存,只要unique_ptr指针创建成功,其析构函数都会被调用,无需手动释放。 //2、创建一个unique_ptr实例 unique_ptr<int> pInt(new int(5)); cout << *pInt; //3、移 阅读全文
posted @ 2020-04-24 15:25 玥茹苟 阅读(1703) 评论(0) 推荐(0) 编辑
摘要:在C++的类中,普通成员函数不能作为pthread_create的线程函数,如果要作为pthread_create中的线程函数,必须是static ! 在C语言中,我们使用pthread_create创建线程,线程函数是一个全局函数,所以在C++中,创建线程时,也应该使用一个全局函数。static定 阅读全文
posted @ 2019-12-16 12:00 玥茹苟 阅读(1551) 评论(0) 推荐(0) 编辑
摘要:openMP 处理for循环 阅读全文
posted @ 2019-12-03 12:26 玥茹苟 阅读(720) 评论(0) 推荐(0) 编辑
摘要:在开发window程序是经常会遇到编译好好的程序拿到另一台机器上面无法运行的情况,这一般是由于另一台机器上面没有安装响应的运行时库导致的,那么这个与编译选项MT、MTd、MD、MDd有什么关系呢?这是msdn上面的解释: MT:mutithread,多线程库,编译器会从运行时库里面选择多线程静态连接 阅读全文
posted @ 2019-11-25 10:08 玥茹苟 阅读(702) 评论(0) 推荐(0) 编辑
摘要:#include<mutex> 包含四类锁: 1 std::mutex 最基本也是最常用的互斥类 2 std::recursive_mutex 同一线程内可递归(重入)的互斥类 3 std::timed_mutex 除具备mutex功能外,还提供了带时限请求锁定的能力 4 std::recursiv 阅读全文
posted @ 2019-09-25 10:05 玥茹苟 阅读(10220) 评论(0) 推荐(0) 编辑
摘要:std::ref和std::cref 解释 std::ref 用于包装按引用传递的值。 std::cref 用于包装按const引用传递的值。 为什么需要std::ref和std::cref bind()是一个函数模板,它的原理是根据已有的模板,生成一个函数,但是由于bind()不知道生成的函数执行 阅读全文
posted @ 2019-09-24 18:06 玥茹苟 阅读(1082) 评论(0) 推荐(0) 编辑
摘要:1、等待线程执行完成 join() 方法数会阻塞主线程直到目标线程调用完毕,即join会直接执行该子线程的函数体部分。 2、暂停线程(线程休眠) 使用std::this_thread::sleep_for或std::this_thread::sleep_until。 3、线程终止 一般情况下当线程函 阅读全文
posted @ 2019-09-24 17:30 玥茹苟 阅读(1783) 评论(0) 推荐(0) 编辑
摘要:1、普通函数(线程入口) 2、类成员函数(线程入口) https://www.jianshu.com/u/88ad4f76eb79 阅读全文
posted @ 2019-09-24 16:26 玥茹苟 阅读(1375) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示