linux c 多线程入门级问题

Guassfans 2011-10-31 12:19:19
我的程序中有两个比较耗时的过程,想用多线程的方法减少程序总体的运行时间,但如下的多线程方法并没有达到这个目的;
使用多线程与不使用多线程耗时一样,为两过程耗时长之和,而非我想像中的为单一过程耗时长者,求解!

void Fun_1()
{
/* do something:1 */
}

void Fun_2()
{
/* do something:2 */
}

void *threadFun_1(void)
{
/* do something:1 */
}

void *threadFun_2(void)
{
/* do something:2 */
}

/*使用多线程*/
int main()
{
pthread_t pthread_id_1;
pthread_t pthread_id_2;

pthread_create(&pthread_id_1, NULL, &threadFun_1, NULL);
pthread_create(&pthread_id_2, NULL, &threadFun_2, NULL);

pthread_join(pthread_id_1, NULL);
pthread_join(pthread_id_2, NULL);

}

/* 不使用多线程*/
int main()
{
Fun_1();
Fun_2();
}
...全文
438 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
jcback 2011-11-01
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 guassfans 的回复:]
引用 18 楼 luciferisnotsatan 的回复:

引用 17 楼 guassfans 的回复:
结果:

amadeuzou@ubuntu:~/Projects/samples$ time ./thread_demo
thread function-1 run time: 5.00009
thread function-2 run time: 5.00024
tota……
[/Quote]
lz,那我是否可以认为你的2个threadFun同时在做sleep(5)(举例大家可以try)
如果你觉得不是告诉我why?那么你的线程处理函数是否涉及I/O操作之类的?
lz没懂我的意思哦,首先多线程并不肯定优于单线程,这东西用过的人懂,问题
是你的线程处理函数,我让你用sleep(5)try就是让你发现这点,譬如说涉及到
I/O操作,那单线程就和多线程相差无几,时间是耗在I/O上了,所以lz还是分析下
你的线程处理函数吧
Guassfans 2011-11-01
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 luciferisnotsatan 的回复:]

引用 17 楼 guassfans 的回复:
结果:

amadeuzou@ubuntu:~/Projects/samples$ time ./thread_demo
thread function-1 run time: 5.00009
thread function-2 run time: 5.00024
total times in mulit-thread: 5.00038
……
[/Quote]

这个例子是的,但在我的实际工程中就不是啦,汗!
luciferisnotsatan 2011-11-01
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 guassfans 的回复:]
结果:

amadeuzou@ubuntu:~/Projects/samples$ time ./thread_demo
thread function-1 run time: 5.00009
thread function-2 run time: 5.00024
total times in mulit-thread: 5.00038
function-1 run time: 5.00007
function-2 run time: 5.00009
total times without mulit-thread: 10.0003

[/Quote]
total times in mulit-thread: 5.00038
这个结果不就说明节约时间了么?
Guassfans 2011-11-01
  • 打赏
  • 举报
回复


#include <iostream>
#include <ctime>
#include <string.h>
#include <pthread.h>
#include <sys/time.h>
using namespace std;
long long int toddiff(struct timeval *tod1, struct timeval *tod2);


void Fun_1()
{
struct timeval tod1, tod2;
gettimeofday(&tod1, NULL);
/* do something:1 */
sleep(5);
gettimeofday(&tod2, NULL);
cout<<"function-1 run time: "<< toddiff(&tod2, &tod1) / 1000000.0<<endl;
}

void Fun_2()
{
struct timeval tod1, tod2;
gettimeofday(&tod1, NULL);
/* do something:2 */
sleep(5);
gettimeofday(&tod2, NULL);
cout<<"function-2 run time: "<< toddiff(&tod2, &tod1) / 1000000.0<<endl;
}

void *threadFun_1(void*)
{
struct timeval tod1, tod2;
gettimeofday(&tod1, NULL);
/* do something:1 */
sleep(5);
gettimeofday(&tod2, NULL);
cout<<"thread function-1 run time: "<< toddiff(&tod2, &tod1) / 1000000.0<<endl;
}

void *threadFun_2(void*)
{
struct timeval tod1, tod2;
gettimeofday(&tod1, NULL);
/* do something:2 */
sleep(5);
gettimeofday(&tod2, NULL);
cout<<"thread function-2 run time: "<< toddiff(&tod2, &tod1) / 1000000.0<<endl;
}

/*使用多线程*/
int main()
{


struct timeval tod1, tod2;
gettimeofday(&tod1, NULL);

pthread_t pthread_id_1;
pthread_t pthread_id_2;

pthread_create(&pthread_id_1, NULL, threadFun_1, NULL);
pthread_create(&pthread_id_2, NULL, threadFun_2, NULL);

pthread_join(pthread_id_1, NULL);
pthread_join(pthread_id_2, NULL);

gettimeofday(&tod2, NULL);
cout<<"total times in mulit-thread: "<< toddiff(&tod2, &tod1) / 1000000.0<<endl;


gettimeofday(&tod1, NULL);

Fun_1();
Fun_2();

gettimeofday(&tod2, NULL);
cout<<"total times without mulit-thread: "<< toddiff(&tod2, &tod1) / 1000000.0<<endl;

return 0;

}



long long int toddiff(struct timeval *tod1, struct timeval *tod2)
{
long long t1, t2;
t1 = tod1->tv_sec * 1000000 + tod1->tv_usec;
t2 = tod2->tv_sec * 1000000 + tod2->tv_usec;
return t1 - t2;
}



结果:

amadeuzou@ubuntu:~/Projects/samples$ time ./thread_demo
thread function-1 run time: 5.00009
thread function-2 run time: 5.00024
total times in mulit-thread: 5.00038
function-1 run time: 5.00007
function-2 run time: 5.00009
total times without mulit-thread: 10.0003

real 0m15.003s
user 0m0.000s
sys 0m0.000s

jcback 2011-11-01
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 guassfans 的回复:]
引用 14 楼 xushaohuaa 的回复:

其实main函数也是一个主线程,你可以只创建一个线程,把function1分配给其完成,另一个则由main函数自己做。建议分离线程,貌似是pthread_detch,这样就不要用join来等待完成,速度快的线程完成以后可以不再跟另一个线程抢资源。还有机子是多核的,线程才会起作用,否则应该是没啥特别的作用的


谢谢回复!
1.“只创建一……
[/Quote]

lz,那我是否可以认为你的2个threadFun同时在做sleep(5)(举例大家可以try)
如果你觉得不是告诉我why?那么你的线程处理函数是否涉及I/O操作之类的?
lengxujun 2011-10-31
  • 打赏
  • 举报
回复
我是路过的...
Toomj 2011-10-31
  • 打赏
  • 举报
回复
这个只是让多个线程“好像”同时进行,其实是让所有线程平均分配可用的处理器资源。例如,如果一个分时系统只有一个CPU可供分配,而程序被分为两个线程,这时,其中一个线程执行一段时间后,另一个线程也将执行一段时间,接着第一个线程又执行,如此反复以实现表面的“同步”,实际消耗时间还是两个线程之和。
昵称很不好取 2011-10-31
  • 打赏
  • 举报
回复
这两件事情有关系吗?
看下多线程函数是否合理
Guassfans 2011-10-31
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 xushaohuaa 的回复:]

其实main函数也是一个主线程,你可以只创建一个线程,把function1分配给其完成,另一个则由main函数自己做。建议分离线程,貌似是pthread_detch,这样就不要用join来等待完成,速度快的线程完成以后可以不再跟另一个线程抢资源。还有机子是多核的,线程才会起作用,否则应该是没啥特别的作用的
[/Quote]

谢谢回复!
1.“只创建一个线程”,但实际上我的程序中有多个类似的Fun 而非两个
2.“建议分离线程,貌似是pthread_detch”,这个我也测试过,最后我是要等待所有进程结果然后进行下一步处理,这样下来,时间还是呈累加型。
xushaohuaa 2011-10-31
  • 打赏
  • 举报
回复
其实main函数也是一个主线程,你可以只创建一个线程,把function1分配给其完成,另一个则由main函数自己做。建议分离线程,貌似是pthread_detch,这样就不要用join来等待完成,速度快的线程完成以后可以不再跟另一个线程抢资源。还有机子是多核的,线程才会起作用,否则应该是没啥特别的作用的
hulongchuan 2011-10-31
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 zhao4zhong1 的回复:]
引用 7 楼 hulongchuan 的回复:
lz可以通过开辟进程实现加快速度!两个线程实质上他也是一个进程,cpu分配时间是以进程为单位的,例如这个进程分配到了10ms,这10ms还得分成两半给分别给你的两个线程,这样线程得到的才5ms。而进程就不同了,开辟两个进程分配的就是20ms,提高了4倍。开辟进程的fork()函数,很简单的!较线程的缺点是占资源,不过你就开辟两个,小菜一碟!

……
[/Quote]
进程是资源分配的最小单位!cpu把时间片给的是进程,而不是线程。进程又可以包含许多线程。我之前试验过,做过数码相框,里面有个音乐播放器,如果通过线程调度来播放音乐,音乐就会特别卡,而通过进程调度,则不会出现这样的现象!
Guassfans 2011-10-31
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 luciferisnotsatan 的回复:]

另外,不知道你是怎么得到时间的。
如果你获取的是CPU时间,那当然是两个相加的时间。得看墙上时钟的时间。
[/Quote]

cpu 双核

在使用多线程 的 main() 中 从开始到最后进行计时, t = 3.6..s
在 threadFun_1 threadFun_2 中计时 t1 t2, t1 = 1.69..s t1 = 1.68..s
ligen1123 2011-10-31
  • 打赏
  • 举报
回复
你要测试 单一线程 与多线程哪个更节省时间

应该是这样的
单一线程: 每隔1s输出一个数 当然要sleep(1)

多线程:当一个线程sleep(1)的时候,自然就运行了另外一个线程 所以要节省一半的时间

单一线程不可能同时在多CPU上运行啊,,系统是以线程为最小调度单位的
不可能会有两个CPU运行一个线程
luciferisnotsatan 2011-10-31
  • 打赏
  • 举报
回复
另外,不知道你是怎么得到时间的。
如果你获取的是CPU时间,那当然是两个相加的时间。得看墙上时钟的时间。
luciferisnotsatan 2011-10-31
  • 打赏
  • 举报
回复
测试机器,CPU几核的??
赵4老师 2011-10-31
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 hulongchuan 的回复:]
lz可以通过开辟进程实现加快速度!两个线程实质上他也是一个进程,cpu分配时间是以进程为单位的,例如这个进程分配到了10ms,这10ms还得分成两半给分别给你的两个线程,这样线程得到的才5ms。而进程就不同了,开辟两个进程分配的就是20ms,提高了4倍。开辟进程的fork()函数,很简单的!较线程的缺点是占资源,不过你就开辟两个,小菜一碟!
[/Quote]
但据我所知操作系统分配CPU时间是以线程为最小单位而不是以进程为最小单位的。
hulongchuan 2011-10-31
  • 打赏
  • 举报
回复
lz可以通过开辟进程实现加快速度!两个线程实质上他也是一个进程,cpu分配时间是以进程为单位的,例如这个进程分配到了10ms,这10ms还得分成两半给分别给你的两个线程,这样线程得到的才5ms。而进程就不同了,开辟两个进程分配的就是20ms,提高了4倍。开辟进程的fork()函数,很简单的!较线程的缺点是占资源,不过你就开辟两个,小菜一碟!
nice_cxf 2011-10-31
  • 打赏
  • 举报
回复
没有异步操作的话,如果CPU只有1个,分线程并不能加快速度
Guassfans 2011-10-31
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 toomj 的回复:]

这个只是让多个线程“好像”同时进行,其实是让所有线程平均分配可用的处理器资源。例如,如果一个分时系统只有一个CPU可供分配,而程序被分为两个线程,这时,其中一个线程执行一段时间后,另一个线程也将执行一段时间,接着第一个线程又执行,如此反复以实现表面的“同步”,实际消耗时间还是两个线程之和。
[/Quote]

那该如何达到我的目的呢?
Guassfans 2011-10-31
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 thefirstz 的回复:]

这两件事情有关系吗?
看下多线程函数是否合理
[/Quote]

这两件事情是对同一图像数据做不同处理
图像数据在不两过程中各有份copy
如:
Fun_1:

ImageProcess* imPro = new ImageProcess(image_data);
imPro->Process_1();

Fun_2:

ImageProcess* imPro = new ImageProcess(image_data);
imPro->Process_2();

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧