Thread: How to make 2 threads run for a certain amount of time

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    11

    How to make 2 threads run for a certain amount of time

    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++);
    
    
    
    
                }
            }
        }
    
    }


  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Use cond_wait and cond_signal (along with a mutex)
    c++ - understanding of pthread_cond_wait() and pthread_cond_signal() - Stack Overflow

    Your two threads need to take a void* parameter as well.

    They should also return NULL if there is nothing to report at the end.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2015
    Posts
    11
    Quote Originally Posted by Salem View Post
    Use cond_wait and cond_signal (along with a mutex)
    c++ - understanding of pthread_cond_wait() and pthread_cond_signal() - Stack Overflow

    Your two threads need to take a void* parameter as well.

    They should also return NULL if there is nothing to report at the end.
    Thanks for your answer.
    I took a look at the link you provided, but how do I implement this for my program?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to exit out of scanf after a certain amount of time
    By Digital Android in forum C Programming
    Replies: 6
    Last Post: 10-27-2014, 06:24 PM
  2. Replies: 3
    Last Post: 03-09-2014, 01:44 AM
  3. Replies: 17
    Last Post: 07-20-2007, 09:25 PM
  4. Replies: 2
    Last Post: 09-04-2001, 02:12 PM

Tags for this Thread