如果一段代码有多个函数互不影响,不共享数据,即不会更改同一个变量。那么开启多线程让函数在不同线程中同时进行,理论上是会比在单线程中按顺序运行要快的(想象成早上我在烧水的同时做早饭,要比先做早饭后烧水要快)。所以我写了一个简单的例子进行验证。
开启多线程的方法需要使用C++中的thread库,因此头文件中需要 #include
std::thread Thread1(Function()) 即可将Function() 这个函数独立开启一个线程,需要注意的是,后面需要增加 Thread.join() 这行指令(或 Thread.detach() 两者功能不同,但一定要有其中一个),否则会报 terminate called without an active exception. 大致原理是:该线程函数析构后,检测到没有join或detech,会强制终止线程,这部分报错在网上可以搜到。
关于验证代码,我写了两个累加函数,双线程同时累加后输出运行时间,与单线程按顺序运行两个函数后的时间比较。计算时间采用了time库中的clock函数。
由于我的cmakelists文件中涉及很多其他的test,引用的库也比较杂乱,就不放出来了。需要提醒的是编译器设置-lpthread会报错可能 lpthread 是旧版本的原因,需要用-pthread才能兼容:set(CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS} -O3 -Wall -march=native -pthread")。代码如下。
#include#include #include void accumulate1(){ int a = 0; for(int i = 0; i < 5; i ++){ a ++; //std::cout << "a = " << a << std::endl; sleep(2); } } void accumulate2(){ int b = 0; for(int i = 0; i < 10; i ++){ b ++; //std::cout << "b = " << b << std::endl; sleep(1); } } int main(int argc, char** argv){ //双线程 clock_t thread_begin = clock(); //双线程同时运行两个函数 std::thread Thread1(accumulate1); std::thread Thread2(accumulate2); Thread1.join(); Thread2.join(); clock_t thread_end = clock() - thread_begin; std::cout << "thread using time = " << thread_end << std::endl; //单线程 clock_t normal_begin = clock(); //按顺序运行两个函数 accumulate1(); accumulate2(); clock_t normal_end = clock() - normal_begin; std::cout << "normal using time = " << normal_end << std::endl; return 0; }
OUTPUT: thread using time = 440 normal using time = 363
这时发现了问题,为什么双线程运行需要的时间竟然比单线程的还要大?多次运行后的结果都是这样。查询clock的原理后得知,clock计算的是CPU时间,会将所有线程使用的时间都加上。所以time库的clock函数不能进行这个线程验证。
于是我想到ros中有ros::time这个计时方法,需要在头文件中加上ros,并将rostime初始化。尝试使用:
#include#include #include #include void accumulate1(){ int a = 0; for(int i = 0; i < 5; i ++){ a ++; // std::cout << "a = " << a << std::endl; sleep(2); } } void accumulate2(){ int b = 0; for(int i = 0; i < 10; i ++){ b ++; // std::cout << "b = " << b << std::endl; sleep(1); } } int main(int argc, char** argv){ ros::Time::init(); //初始化rostime //双线程 ros::Time thread_begin = ros::Time::now(); std::thread Thread1(accumulate1); std::thread Thread2(accumulate2); Thread1.join(); Thread2.join(); ros::Duration thread_end = ros::Time::now() - thread_begin; std::cout << "thread using time = " << thread_end << std::endl; //单线程 ros::Time normal_begin = ros::Time::now(); accumulate1(); accumulate2(); ros::Duration normal_end = ros::Time::now() - normal_begin; std::cout << "normal using time = " << normal_end << std::endl; return 0; }
OUTPUT: thread using time = 10.015955069 normal using time = 20.008642445
发现结果非常理想,双线程运行确实能比单线程节省一半的时间。



