1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
#include <iostream>
using namespace std;
#include <time.h>
#include <pthread.h>
void* thread1(void* data){
time_t start = time(0);
// Print every 1 second for at most, 30 seconds
while( (int)difftime(time(0), start) < 30 ){
printf("\n\nI AM THREAD 1!");
printf("\n\n");
sleep(1);
}
}
void* thread2(void* data){
time_t start = time(0);
// Print every 2 seconds for at most, 30 seconds
while( (int)difftime(time(0), start) < 30 ){
printf("\n\nI AM THREAD 2!");
printf("\n\n");
sleep(2);
}
}
void* thread10(void* data){
time_t start = time(0);
// Print every 10 seconds for at most, 30 seconds
while( (int)difftime(time(0), start) < 30 ){
printf("\n\nI AM THREAD 10!");
printf("\n\n");
sleep(10);
}
}
int main(){
// create threads
pthread_t threadOne;
pthread_t threadTwo;
pthread_t threadTen;
int result = 0;
int data = 0;
pthread_create(&threadOne, 0, &thread1, (void*)data);
pthread_create(&threadTwo, 0, &thread2, (void*)data);
pthread_create(&threadTen, 0, &thread10, (void*)data);
pthread_detach(threadOne);
pthread_detach(threadTwo);
pthread_detach(threadTen);
time_t start = time(0);
// Print every 5 seconds for at most, 30 seconds
while( (int)difftime(time(0), start) < 30 ){
printf("\n\nI AM MAIN!");
printf("\n\n");
sleep(5);
}
return 1;
}
|