I'm working on my program where I want thread 1 to run for 2.1 seconds, after 2.1 seconds I want thread 2 to run for 3.4 seconds and then it needs to switch back to thread 1. I've managed to make both threads run, but without the given time. It is required that I use 2 threads.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
int main() {
void* taskOne();
void* taskTwo();
pthread_attr_t tattr;
pthread_attr_init(&tattr);
pthread_t thread1, thread2;
double seconds1 = 2.1;
double seconds2 = 3.4;
pthread_create(&thread1, &tattr, taskOne, NULL);
pthread_create(&thread2, &tattr, taskTwo, NULL);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
return 0;
}
void* taskOne() {
int i, j, m, n;
while (1) {
for (i = 0; i < 5; i++) {
for (j = 1; j <= 8; j++) {
printf("thread: 1 periodnumber: %i\n", j);
for (m = 0; m <= 1250; m++)
for (n = 0; n <= 250000; n++);
}
}
}
}
void* taskTwo() {
int i, j, m, n;
while (1) {
for (i = 0; i < 5; i++) {
for (j = 1; j <= 8; j++) {
printf("thread: 2 periodnumber: %i\n", j);
for (m = 0; m <= 2750; m++)
for (n = 0; n <= 250000; n++);
}
}
}
}