C++ windows多线程程序到linux下运行失败
博客http://blog.csdn.net/monkey3233/article/details/71467574 中的多线程程序,在VS下运行正常,但是复制到ubuntu,clion运行就失败
报错如下:
undefined reference to `pthread_create'
解决办法:
在CMakeLists.txt中添加-lpthread
原:target_link_libraries(multi_thread)
修改后:target_link_libraries(multi_thread -lpthread)
多线程未能起到优化效果,原因是计时方式有问题
- 在windows下,VS运行时,并行时间约为串行的一半,并行为370ms,串行为600ms;
- 但是在ubuntu下,运行时间相当,并行为581ms,串行为576ms;
- 可以在CMakeLists.txt中开启多线程优化,此时,并行时间为:38ms,串行时间为:35ms;时间差不多,多线程没有起到优化效果,目前不清楚问题出在什么地方;
- 原程序所用的计时方式:clock_t类,是用的CPU运行周期来计时的,多线程下,总的周期和单线程差不多,因此看上去时间相同;现在改用系统时间计时,gettimeofday() 链接:获取系统时间中所用的方法即可实现,更新后的代码附在最后(PS:CMakList.txt相同);
函数原型
#include<sys/time.h>
int gettimeofday(struct timeval*tv,struct timezone *tz )
这个函数会把时间包装为一个结构体返回。包括秒,微妙,时区等信息:
struct timeval{
long tv_sec;/*秒*/
long tv_usec;/*微妙*/
};
struct timezone{
int tz_minuteswest;/*和greenwich时间差*/
int tz_dsttime;
}
多线程时间为329ms,单线程594ms,基本实现了性能的提升;
clock_t计时代码如下:
#include <stdlib.h>
#include <iostream>
#include <thread>
#include <time.h>
using namespace std;
int N = 10000;
void thread_task() //用于比较的函数
{
long int sum2 = 0;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
sum2 += (i + j);
// cout << "thread1: " << i << endl;
}
cout << "sum2: " << sum2 << endl;
}
int main()
{
long int sum1 = 0;
int j = 0;
clock_t clockBegin, clockEnd;
clockBegin = clock(); // 计时开始
thread t(thread_task);// 并行开始
// thread_task(); //串行开始
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
sum1 += (i + j);
// cout << "thread2: " << i << endl;
}
t.join();// 并行结束
cout << "sum1: " << sum1 << endl;
clockEnd = clock(); // 计时结束
cout << "time is " << (clockEnd - clockBegin)/1000 << " ms" << endl;
// system("pause");
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.7)
project(multi_thread)
#set(CMAKE_CXX_STANDARD 11)
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -march=native -O3 -pthread" )
set(SOURCE_FILES main.cpp)
add_executable(multi_thread ${SOURCE_FILES})
target_link_libraries(multi_thread -lpthread)
采用gettimeofday系统时间计时
#include <stdlib.h>
#include <iostream>
#include <thread>
#include <sys/time.h>
using namespace std;
int N = 10000;
void thread_task1()
{
long int sum1 = 0;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
sum1 += (i + j);
}
cout << "sum1: " << sum1 << endl;
}
void thread_task2()
{
long int sum2 = 0;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
sum2 += (i + j);
}
cout << "sum2: " << sum2 << endl;
}
int main()
{
long int sum1 = 0;
int j = 0;
float serial_time = 0 ,parallel_time = 0;
struct timeval start;
struct timeval end;
cout << "Repeat time N is: " << N << endl;
/********************* parallel compute *********************/
gettimeofday(&start, NULL);
thread t1{thread_task1};
thread_task2();
if(t1.joinable())
t1.join();
gettimeofday(&end,NULL);
parallel_time = (end.tv_sec-start.tv_sec)*1000+(end.tv_usec-start.tv_usec)*0.001;
/********************* parallel compute *********************/
/********************* serial compute *********************/
gettimeofday(&start, NULL);
thread_task1();
thread_task2();
gettimeofday(&end,NULL);
serial_time = (end.tv_sec-start.tv_sec)*1000+(end.tv_usec-start.tv_usec)*0.001;
/********************* serial compute *********************/
cout << "parallel compute time is " << parallel_time << " ms" << endl;
cout << "serial compute time is " << serial_time << " ms" << endl;
return 0;
}