| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 543 人关注过本帖
标题:一个多线程的问题?请大家指点,指点。
只看楼主 加入收藏
milo0105
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2007-4-1
收藏
 问题点数:0 回复次数:1 
一个多线程的问题?请大家指点,指点。

下面这个程序希望得到的结果是每5秒调用suvA(),suvB()每10秒调用suvC,suvD,就是出现suvA(),suvB()的次数是出现suvC,suvD次数的两倍,不知道为什么不行,应该怎么修改才可以。请高手们指点一下,谢谢。 编译环境linux

出现的结果是5秒suva,suvb,5秒suvc,suvd然后循环!

#include<iostream>
#include <pthread.h>
#include<time.h>

using namespace std;


void *suvA( void *ptr) {
sleep(5);
cout<<"it is suva"<<endl;

}

void *suvB( void *ptr) {
sleep(5);
cout<<"it is suvb"<<endl;

}

void *suvC( void *ptr) {
sleep(10);
cout<<"it is suvc"<<endl;

}

void *suvD( void *ptr) {
sleep(10);
cout<<"it is suvd"<<endl;

}


main()
{


int thread_ret1, thread_ret2, thread_ret3, thread_ret4;
pthread_t thread1, thread2, thread3, thread4;

while(true){

thread_ret1=pthread_create( &thread1, NULL, &suvA, NULL);
thread_ret2=pthread_create( &thread2, NULL, &suvB, NULL);
thread_ret3=pthread_create( &thread3, NULL, &suvC, NULL);
thread_ret4=pthread_create( &thread4, NULL, &suvD, NULL);


pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
pthread_join( thread3, NULL);
pthread_join( thread4, NULL);


}


}

搜索更多相关主题的帖子: 线程 
2007-04-16 21:45
milo0105
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2007-4-1
收藏
得分:0 
问题已解决, 把while(true)加入到每个子线程就行了。
2007-04-16 22:16
快速回复:一个多线程的问题?请大家指点,指点。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011543 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved