#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判断有没有使用过join
return 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;
}