免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1219 | 回复: 3
打印 上一主题 下一主题

一个多线程的问题?请大家指点,指点。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-04-16 20:17 |只看该作者 |倒序浏览
下面这个程序希望得到的结果是每5秒调用suvA(),suvB()每10秒调用suvC,suvD,就是出现suvA(),suvB()的次数是出现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);
   
   
        }

   
}

论坛徽章:
0
2 [报告]
发表于 2007-04-16 20:45 |只看该作者
又是join!建议开个pthread问题的帖子

论坛徽章:
0
3 [报告]
发表于 2007-04-16 20:59 |只看该作者
这个问题应该怎么解决啊?高手们给我说说啊

论坛徽章:
0
4 [报告]
发表于 2007-04-16 22:20 |只看该作者
问题已解决, 把while(true)加入到每个子线程就行了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP