新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

【并行】std::thread入门-创新互联

std::thread入门

参考
https://immortalqx.github.io/2021/12/04/cpp-notes-3/

专注于为中小企业提供成都网站设计、网站制作服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业仪陇免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了1000+企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

对应代码
https://github.com/chunleili/learnStdThread

简介

thread是C++11之后的一个并行STL库。由于是STL的一部分,好处是跨平台。与之相比,pthread.h只支持linux,因此建议用std::thread来代替pthread。

thread无非就是一个C++类。所以创建线程就是类的实例化,而结束则会自动调用析构函数。

thread的构造函数具有变长参数。第一个参数需要的是个函数指针/函数对象,第二个和之后的参数就是这个函数的参数。非常简单。

其中有两个成员函数最为重要:join()和detach()。

简单来说join就是主线程等待子线程的完成,而detach就是让子线程放飞自我。

hello world

下面是个示例程序

#include#includeusing namespace std;

void output(int i)
{cout<< i<< endl;
}

int main()
{	for (uint8_t i = 0; i< 4; i++)
	{thread t(output, i);
		t.join();	
	}
		
	return 0;
}

输出

0
1
2
3

解释下:
thread t(output, i);是创建线程。每次循环都创建一个新的线程。

t.join();然后利用join等待该线程的执行完毕。

在这里执行该线程就是打印一下i。

在这次循环完毕后,由于对象t是在该作用域内实例化的,因此作用域结束时自动析构。

然后再创建一个线程对象,也是t。再执行。然后等待。最后析构。如此往复4次。

构造函数

下面我们详解一下构造函数

第一个参数是函数对象,可以用普通函数、成员函数,functor,lambda等。

假如用lambda(我个人比较喜欢,简洁干净)
#include#includeusing namespace std;

int main()
{	for (int i = 0; i< 4; i++)
	{thread t([i](){	 cout<< i<< endl; 
		});
		t.join();	
	}
		
	return 0;
}
如果用functor(函数对象,也就是重载了operator()的类)

这种方式要注意:不能使用thread t(Task(), i);。尽管这个有可能不出BUG,但是也有可能会出BUG。因为编译器会认为你在进行函数声明。如果你非想要创建一个匿名变量,那么请用{}

#include#includeusing namespace std;

class Task
{public:
	void operator()(int i)
	{cout<< i<< endl;
	}
};


int main()
{for (int i = 0; i< 4; i++)
	{Task task;
		thread t(task, i);
		//do not use this, because it may be viewed as a function declaration
		//thread t(Task(), i);
		//If you want to use this, you should use initializer_list
		//thread t{(Task()), i};
		t.join();	
	}
		
	return 0;
}
如果用成员函数方式

这类方式最麻烦,因为第二个参数还需要传入所对应的对象的指针。

#include#includeusing namespace std;

class Task
{public:
	void do_work(int i)
	{cout<< i<< endl;
	}
};


int main()
{cout<<"member func"<Task task;
		thread t(&Task::do_work, &task, i);
		t.join();	
	}
		
	return 0;
}
detach

上面说的都是join的例子。因为join会阻塞主线程,等待子线程的执行完毕,所以不会出现乱序的BUG。但是如果detach,那么子线程就是放飞自我,主线程也不会等待子线程,导致谁也不知道哪个子线程先执行完毕。

一个有BUG的程序
#include#includeusing namespace std;

void output(int i)
{cout<< i<< endl;
}

int main()
{cout<<"detach thread\n";
	for (uint8_t i = 0; i< 4; i++)
	{thread t(output, i);
		t.detach();	
	}
	
	getchar();

	return 0;
}

输出

detach thread
02
3
1

这显然是个有BUG的程序。因为子线程放飞自我之后,你根本无法预测哪个先结束。你甚至不能保证主线程main函数最后return 0。因为有可能主线程结束了,子线程还在运行着呢。

信不信由你:如果你不加上getchar();你甚至可能什么都打印不出来。因为主线程先结束了,子线程还没结束呢。

另一个有BUG的程序

detach另外一个错误是容易导致悬空指针。

#include#includeusing namespace std;

int main()
{auto fn = [](const int *a)
	{for (int i = 0; i< 10; i++)
		{	cout<< *a<< endl;
		}
	};

	cout<< "detach thread another bug\n";
	[fn]
	{int a = 1010;
		thread t(fn, &a);
		t.detach();
	}();
}

在这个例子中,a的地址被传给子线程的fn函数。但是由于是detach,主线程不需要等待子线程。这就导致很可能主线程已经执行完毕,并且销毁了a变量,导致这个指针是个悬空的指针。

正确的改进方式是把传递指针改成传值。

因此在多线程的程序中,通常都采用传值而非传址。

即使是传递引用也会变成拷贝传值

为了防止上述错误,C++规定:即使是传递引用,也会变成拷贝。

例如

#include#includeusing namespace std;

class Node
{public:
	int a = 1;
	int b = 2;
};

void func(Node &node)//will become copy even if we pass by reference
{node.a = 10;
	node.b = 20;
}

int main()
{Node node;

	thread t(func, node);
	t.join();

	cout<< node.a<< endl ;
	cout<< node.b<< endl ;
}

output

1
2

在上面的例子中,即使我们指定了传递引用,也会变成拷贝。所以func根本没改变node原有的值。所以最后打印出来还是1 2。

获取线程的id

有两种方式获取id

  1. 从子线程内获取this_thread::get_id()
  2. 从外部主线程获取t.get_id()
#include#includeusing namespace std;

void func()
{
	cout<< "In func thread id:"<

输出

In main thread id:27424
In func thread id:27424
-----------------
In main thread id:20068
In func thread id:20068
-----------------
In main thread id:19404
In func thread id:19404
-----------------
In main thread id:21680
In func thread id:21680
-----------------
TODO

mutex
condition_variable

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


网站名称:【并行】std::thread入门-创新互联
网站链接:http://www.wjwzjz.com/article/dipdps.html
在线咨询
服务热线
服务热线:028-86922220
TOP