1

I use pthread_create to create 10 child threads, passes an integer to the thread_func

#define THREAD_NUM 10

void *thread_func(void *arg)
{
    int v = (int)arg;

    printf("v = %d\n", v);

    return (void*)0;
}

int main(int argc, const char *argv[])
{
    pthread_t pids[THREAD_NUM];
    int rv;
    int i;

    for (i = 0; i < THREAD_NUM; i++) {
        rv = pthread_create(&pids[i], NULL, thread_func, (void*)i);
        if (rv != 0) {
           perror("failed to create child thread");
           return 1;
        }
    }
    return 0;
}

I was wondering why it outputs different result everytime not just v = 1 v = 2 ... v = 9

BenMorel
  • 34,448
  • 50
  • 182
  • 322
bigpotato
  • 211
  • 2
  • 3
  • 13

4 Answers4

1

You have to wait for all the threads to complete in the main using pthread_join, only then u can see all of them display some value

#include <stdio.h>
#include <pthread.h>

#define THREAD_NUM 10

void *thread_func(void *arg)
{
    int v = (int)arg;

    printf("v = %d\n", v);

    return (void*)0;
}

int main(int argc, const char *argv[])
{
    pthread_t pids[THREAD_NUM];
    int rv;
    int i;

    for (i = 0; i < THREAD_NUM; i++) {
        rv = pthread_create(&pids[i], NULL, thread_func, (void*)i);
        if (rv != 0) {
           perror("failed to create child thread");
           return 1;
        }
    }
    for (i = 0; i < THREAD_NUM; i++) {
        pthread_join(pids[i], NULL);
    }
    return 0;
}

Sample run output:

[root@fc ~]# ./a.out
v = 0
v = 2
v = 4
v = 6
v = 7
v = 8
v = 9
v = 5
v = 3
v = 1
Sakthi Kumar
  • 3,047
  • 15
  • 28
  • thanks, but I have another question,when I use pthread_join to wait for the child threads complete in the main thread, it did output 10 lines although(deal to the kernel schedule staff),but if don't do this,why it seems stop executing in the half way,I mean not 10 line output? – bigpotato Feb 24 '14 at 07:39
  • That is because your `main` has exited and eventually your threads also exit upon exit of program. – Sakthi Kumar Feb 24 '14 at 08:22
  • You mean when the parent thread exit,the remaining child threads will be forced to quit before they finish their execution? – bigpotato Feb 24 '14 at 13:13
  • yes it does. see a detailed discussion here http://stackoverflow.com/questions/11875956/main-thread-exit-does-other-exit-too – Sakthi Kumar Feb 24 '14 at 13:18
0

I think your question is why it is not printing them in order like v=1 v = 2 ... v = 9. That is because the kernel schedules the threads and they can be scheduled in any order. If you want synchronized output you need to use locks and condition variables.

user376507
  • 2,004
  • 1
  • 19
  • 31
0

In my case I am having proper out put. I have done some modification. Can you give a try to it?

#include <stdio.h>
#include <pthread.h>

#define THREAD_NUM 10

void *thread_func(void *arg)
{
  int v = *(int *)arg;

  printf("v = %d\n", v);

  return (void*)0;
}

int main(int argc, const char *argv[])
{
  pthread_t pids[THREAD_NUM];
  int rv;
  int i;

  for (i = 0; i < THREAD_NUM; i++) {
    rv = pthread_create(&pids[i], NULL, thread_func, (void*)&i);
    if (rv != 0) {
      perror("failed to create child thread");
      return 1;
    }
  }
  return 0;
}
sutirtha
  • 375
  • 3
  • 21
0

Briefly, echo of the threads in your program is an independent scheduling unit, which means they can actually or virtually run parallelly in an order determined by the scheduler in your operating system kernel, and this order changes run to run according to your system circumstances at that time.

Lee Duhem
  • 14,695
  • 3
  • 29
  • 47