C++最简单的多线程实现

    之前跑实验的时候师兄给我炫了一招,虽然一直知道师兄代码很腻害,所以学到新东西也是要记下来好。这里将一种最简单的C++多线程实现方法,特点就是简单粗暴。代码如下,需要包括"omp.h"头文件。

#include
#include"omp.h"
using namespace std;

int main(int argc, char * argv[])
{
#pragma omp parallel for    //This line of code is the definition of using multi-thread
	for(int i=0; i<5; i++)
	{
		cout << "I am the "<< i<< endl;
	}
}

    在ubuntu环境下可以把上述文件存为test.cpp,可以用命令行也可以直接操作啊,现在的linux环境真是方便= =

    Then compile and run the test.cpp file in Linux

g++ -fopenmp test.cpp    // that -fopenmp is most important
./a.out  

    Then you can see the result

C++最简单的多线程实现_第1张图片


    在linux环境下编译然后用VS运行的结果来忽悠大家?不能忍...请大家海涵......


    And tips:

    The code of Learning Graph Matching in PAMI cannot be make properly is the reason for the second command, there's no -fopenmp in it. 

    Thus you need to add this in the Makefile archive.



你可能感兴趣的:(Basic,Operation)