简体   繁体   中英

posix threads with c language

#include <pthread.h>
#define NUM_THREADS 10

void *work(void *i){
      int f = *((int *)(i));
      printf("Hello, world from %i with value %i\n",
             pthread_self(), f);
      pthread_exit(NULL);
}
int main(int argc, char **argv){
      int i;
      pthread_t id[NUM_THREADS];
      for(i = 0; i < NUM_THREADS; ++i){
            if(pthread_create(&id[i], NULL, work, (void *)(&i))){
                  printf("Error creating the thread\n"); exit(19);}
      }
      return 0;
}

the output is supposed to be :

Hello, world from 2 with value 1
Hello, world from 3 with value 2
Hello, world from 6 with value 5
Hello, world from 5 with value 5
Hello, world from 4 with value 4
Hello, world from 8 with value 9
Hello, world from 9 with value 9
Hello, world from 10 with value 9
Hello, world from 7 with value 6
Hello, world from 11 with value 10

this is again not a homework . its some code i run into in some reference but posix is not my field so i just want what's enough to understand this
my questions are :

  • what does this mean int f = *((int *)(i)); ??? i mean pointers written like these i cant understand them
  • what does this mean (void *)(&i))
  • does pthread_create return a zero or non-zero value in success? -in the output let's take line one for example how come there is value 1 !! isn't it supposed to be zero since i is zero
  • does ++i affect this output?

what does this mean int f = *((int *)(i)); ???

It casts i to an int * , dereferences it, and assigns the value to f .

what does this mean (void *)(&i))

It takes a pointer to i and casts it to void * .

does pthread_create return a zero or non-zero value in success?

pthread_create returns zero on success.

in the output let's take line one for example how come there is value 1 !! isn't it supposed to be zero since i is zero

You'd think so, but it isn't. Each thread is executing asynchonously from all the others, and is reading the value of i at the time it runs , not at the time it was launched; as such, the results are actually unpredictable. The program you're running here looks like it's supposed to demonstrate this exact effect.

does ++i affect this output?

Uh... yes? The loop would not terminate without it.

what does this mean int f = *((int *)(i));

int f = *((int *)(i)); means cast i to an int* , and then dereference this pointer to get the int value at the address. You can break it down into two steps:

int *temp = (int*)i;
int f = *temp;


what does this mean (void *)(&i))

(void *)(&i) simply casts the address of i (in main that's an int* ) to a void* .

does pthread_create return a zero or non-zero value in success?

From the man pages:

RETURN VALUE

  On success, pthread_create() returns 0; on error, it returns an error number, and the contents of *thread are undefined. 


in the output let's take line one for example how come there is value 1 !! isn't it supposed to be zero since i is zero

You're passing a pointer to i to the thread function, and since the main thread and this new thread are running in parallel, i can be updated in the for loop ( ++i ) before it's accessed in work .


does ++i affect this output?

Yes, it's the argument being passed to your thread function. It's the number after "...with value" in the output.

Firstly, let's see:

f = *((int *)(i));

Since i is a void * , it can't be legally dereferenced, so it's first casted to an int * , which dereference gives a value of type int , as expeced. asting the address of i to a void * is the reverse of this operation; since a pthread function accepts a void *, but an address of an int is of type int *, this cast is nice to be done.

pthread_create return zero on success: http://pubs.opengroup.org/onlinepubs/007908799/xsh/pthread_create.html

Yes, ++i affects the output, as it increments i by one, thus the next dispatched thread function will be supplied as argument with a value greater than that of the the former call's. That's why 'value' is incrementing during the program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM