最近做项目要用到多线程的编程,又要用到opencv因此采用cmake生成makefile然后进行编译的方法比较简单。这里给出多线程的简单实例。
主函数非常简单:
#include <iostream>
#include <pthread.h>
using namespace std;
#define NUM_THREADS 2
class Hello
{
public:
static void* say_hello( void* args )
{
cout << "hello...123" << endl;
}
};
int main()
{
pthread_t tids[NUM_THREADS];
for( int i = 0; i < NUM_THREADS; ++i )
{
int ret = pthread_create( &tids[i], NULL, Hello::say_hello, NULL );
if( ret != 0 )
{
cout << "pthread_create error:error_code" << ret << endl;
}
}
pthread_exit( NULL );
}
CmakeList文件如下(这里没有包含opencv 的相关部分)
project(test)
add_executable(test main.cpp)
set(THR