c_cpp STD C ++线程

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp STD C ++线程相关的知识,希望对你有一定的参考价值。

#include <iostream>       // std::cout
#include <thread>         // std::thread

void foo()
{
	for (int i = 0; i < 1500000000; i++);
}

void bar(int x)
{
	for (int i = 0; i < 1500000000; i++);
}
using namespace std;
int main()
{
	std::thread first(foo);     // spawn new thread that calls foo()
	std::thread second(bar, 0);  // spawn new thread that calls bar(0)

	std::thread third(foo);
	std::thread fourth(foo);

	std::cout << "main, foo and bar now execute concurrently...\n";

	// synchronize threads:

	first.join();                // pauses until first finishes
	second.join();               // pauses until second finishes
	third.join();
	fourth.join();

	std::cout << "foo and bar completed.\n";

	cout << "Done in " << clock() * 1000 / CLOCKS_PER_SEC << endl;

	getchar();

	return 0;
}

以上是关于c_cpp STD C ++线程的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp [测试]#C ++#多线程

混合 C++11 std::thread 和 C 系统线程(即 pthreads)

c_cpp CPP - 教程016 - C ++线程

C ++ 20中的新线程(jthread)功能

c_cpp RAII确保C ++ 11线程被加入

C++11:基于std::queue和std::mutex构建一个线程安全的队列