C++线程库的基本使用(初级)

编程入门 行业动态 更新时间:2024-10-27 09:46:21

C++<a href=https://www.elefans.com/category/jswz/34/1771240.html style=线程库的基本使用(初级)"/>

C++线程库的基本使用(初级)

#include<iostream>
#include<thread>
#include<string>
void printHelloWorld(std::string msg)
{std::cout << msg<< std::endl;return;
}
int main()
{std::thread threadl(printHelloWorld,"Hello Thread");//第一个参数是函数名,第二个参数是上一个函数的参数threadl.join();//检查子线程有无return,等子线程结束后,主线程才会继续//threadl.detach();//分离主线程和子线程,主线程和子线程同时进行,主线程运行完之后或子线程结束后释放资源bool isJoin = threadl.joinable();//该函数判断线程能否使用join,detach//join只能使用一次,joinable判断有没有使用过joinreturn 0;
} 
几个需要注意的点:

1.thread参数是引用时:

#include<iostream>
#include<thread>
void foo(int& x)
{x += 1;
}
int main()
{int a = 1;std::thread t(foo, std::ref(a));//使用std::ref()来告诉thread,这个参数是引用t.join();std::cout << a << std::endl;return 0;
} 

2.thread参数是不存在的:

#include<iostream>
#include<thread>
std::thread t;
void foo(int& x)
{x += 1;
}
void test()
{int a = 1;t = std::thread(foo, std::ref(a));
}
int main()
{test();t.join();//a是test函数的局部变量test结束后a已经被释放了//这个线程已经引用不了a了,所以报错return 0;
} 
#include<iostream>
#include<thread>
void foo(int* x)
{std::cout << *x << std::endl;//x现在是野指针
}
int main()
{int* ptr = new int(1);std::thread t(foo, ptr);delete ptr;//此时子线程和主线程同时运行,子线程在运行时,x所指的已经被释放了,子线程运行需要一定的时间t.join();//主线程等待子线程运行结束return 0;
} 

注意:在C++中,std::thread的构造函数要求第一个参数是一个可调用的对象(函数指针、函数名、lambda表达式等),而不是一个类的成员函数。成员函数需要使用类的对象来调用,因此不能直接作为std::thread的参数。(成员函数不是函数名,是因为成员函数是与类对象相关联的函数,它需要一个类的对象来调用。而函数名只是一个标识符,可以与任何类型的函数相关联,包括成员函数和非成员函数。)

#include <iostream>
#include <thread>
class A {
public:void foo(){std::cout << "Hello" << std::endl;}
};
int main() {A a;std::thread t(&A::foo, &a);t.join();return 0;
}

可以用智能指针来防止内存泄漏

#include <iostream>
#include <thread>
class A {
public:void foo(){std::cout << "Hello" << std::endl;}
};
int main() {std::shared_ptr<A> a = std::make_shared<A>();std::thread t(&A::foo, a);t.join();return 0;
}

锁:

#include <iostream>
#include <thread>
#include <mutex>
int a = 0;
std::mutex mtx;
void func()
{for (int i = 0; i < 10000; i++){mtx.lock();a += 1;mtx.unlock();}
}
int main() {std::thread t1(func);std::thread t2(func);t1.join();t2.join();std::cout << a << std::endl;return 0; 
}

更多推荐

C++线程库的基本使用(初级)

本文发布于:2023-11-16 11:51:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1620216.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!