|
下面这个程序希望得到的结果是每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);
}
} |
|