Thread: sleep() and detached threads

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    Europe/Belgrade
    Posts
    78

    Post sleep() and detached threads

    If I make 2 detached threads which count to 10 and write the message on STDOUT, everything is OK. But, if I put sleep() into the loop of the thread function (line (1)), then only the first thread is executed and the loop within the thread for just one time. Why?

    Code:
    #include <stdio.h>
    #include <pthread.h>
    
    
    /*
    Thread start function, counts to 10 and exits.
    */
    void* f(void* arg)
    {
      char* param = (char*)arg;
      int i;
      
      for (i = 1; i <= 10; i++)
      {
        printf("\n%s %d", param, i);
        sleep(1);/* (1) */
      }
    
      return NULL;
    }
    
    
    int main(void)
    {
      pthread_attr_t attr1;
      pthread_attr_t attr2;
      pthread_t th1;
      pthread_t th2;
      char arg1[10] = "thread1";
      char arg2[10] = "thread2";
      
      pthread_attr_init(&attr1);
      pthread_attr_init(&attr2);
      pthread_attr_setdetachstate(&attr1, PTHREAD_CREATE_DETACHED);
      pthread_attr_setdetachstate(&attr2, PTHREAD_CREATE_DETACHED);
      pthread_create(&th1, &attr1, &f, arg1);
      pthread_create(&th2, &attr2, &f, arg2);
      
      printf("\n");
      return 0;
    }
    Also, everything works fine if I use joinable thread with pthread_join(), then there's no problem with sleep(). So, why detached thread makes the problem?
    Thanks in advance.

  2. #2
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    sleep() works on the entire process, not just one thread, IIRC... I could be wrong.
    Insert obnoxious but pithy remark here

  3. #3
    Registered User
    Join Date
    Jan 2006
    Location
    Europe/Belgrade
    Posts
    78
    No, each thread has its own sleep(). The problem is in the main(): finishes before threads have a time to finish. But if I put sleep(30) in the main(), both threads will have time to finish.
    Thanks any way.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Call pthread_join() in your main thread to wait for the other threads to finish.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Location
    Europe/Belgrade
    Posts
    78
    With joinable threads it's OK, but what about detached threads? Does pthread_join() have effect?

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    Quote Originally Posted by karas
    No, each thread has its own sleep(). The problem is in the main(): finishes before threads have a time to finish. But if I put sleep(30) in the main(), both threads will have time to finish.
    Thanks any way.
    This is incorrect, for posix at least. sleep does effect the entire process, in order to give up time on the cpu, you must instead use sched_yield() (check the man pages). as for detached threads I don't see any thing in the man pages for going from detached to joinable, only joinable to detached, so I might suggest either having them joinable, or making a boolean flag of sorts to single that the thread is done with its work.

    Also, to show that sleep DOES effect every thread (since they are part of the parent process) here is some demo code

    Code:
    #include <stdio.h>
    #include <pthread.h>
    #include <sched.h>
    #define array_size 400
    #define no_threads 10
    
    int a[array_size];
    int global_index = 0;
    pthread_mutex_t mutex1;
    
    void *worker(void *notused) {
      int local_index;
      do {
    
        pthread_mutex_lock(&mutex1);
        local_index = global_index;
        global_index++;
        pthread_mutex_unlock(&mutex1);
        printf("I am a thread\n");
        sched_yield();
      } while (local_index < array_size);
    
      return ;
    }
    
    int main() {
      int i;
      pthread_t thread[no_threads];
      pthread_mutex_init(&mutex1,NULL);
    
      for (i=0; i< array_size; i++)
        a[i] = i+1;
    
      for (i=0; i < no_threads; i++)
        if (pthread_create(&thread[i], NULL, worker, NULL) != 0)
          perror("Pthread_create failed");
    
      for (i=0; i < no_threads; i++)
        if (pthread_detach(thread[i])!= 0)
          perror("Pthread_detach failed");
    
      while(i < array_size)
      {
       pthread_mutex_lock(&mutex1);
       printf("The index is currently %d\n", global_index);
       i = global_index;
       if (global_index >= 300) sleep(1);
       pthread_mutex_unlock(&mutex1);
       sched_yield();
      }
    }
    I know its not exactly the cleanest code, but it demonstrates my point, sleep effects the entire process, child threads included.

    EDIT: I had updated the code as I was making the post, forgot to drop in the updated code.
    Last edited by Xipher; 02-11-2006 at 09:33 PM.
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  7. #7
    Registered User
    Join Date
    Jan 2006
    Location
    Europe/Belgrade
    Posts
    78
    Thanks, I was not sure about sleep().

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    No problem, I'm rarely sure of any thing, only knew about this factoid since I'm currently in an operating systems class, and we went over this fact in class.
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

Popular pages Recent additions subscribe to a feed