![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
C++并发
0penuel0
这个作者很懒,什么都没留下…
展开
-
C++并发-启动线程
#include <iostream>#include <thread>#include <unistd.h>void hello(){while(1){std::cout << "hello world" << std::endl;for(int i=0;i<999999999;i++);}}void h...原创 2019-09-04 19:11:00 · 134 阅读 · 0 评论 -
C++并发-转移线程的所有权
void function();void function1();std::thread t1( function ); //启动一个新线程t1与function关联std::thread t2 = std::move(t1); //将t1限行的转移给t2t1 = std::thread( function1 ); //启动一个临时的对象与function1关联std::t...原创 2019-09-04 19:49:00 · 161 阅读 · 0 评论 -
C++并发-传递参数给线程
1.void f(int i, std::string const& s); std::thread t(f,3,"hello");2.传入的参数是已复制的形式到内部空间,即使使用引用一样如此void update( int w, int & data );void main() { int data = 5; std::thread...原创 2019-09-04 19:22:00 · 559 阅读 · 0 评论 -
C++并发CMAKE添加库
1.因为pthread不是C标准库,需要链接2.在CMakeLists.txt里添加: FIND_PACKAGE(Threads REQUIRED) TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${CMAKE_THREAD_LIBS_INIT})原创 2019-08-11 21:16:00 · 587 阅读 · 0 评论 -
C++并发-互斥元
1.std::mutex类1.构造函数,std::mutex不允许拷贝构造,也不允许 move 拷贝,最初产生的 mutex 对象是处于 unlocked 状态的。2.lock(),调用线程将锁住该互斥量。线程调用该函数会发生下面 3 种情况:①如果该互斥量当前没有被锁住,则调用线程将该互斥量锁住,直到调用 unlock之前,该线程一直拥有该锁。②如果当前互斥量被其他线程锁住,则当前的调用线程...原创 2019-09-05 19:46:28 · 315 阅读 · 0 评论