pthreads - pthread_create argument in c multi thread programming -


pthread_create(&thread,null,childthread,(void *)100);

1) can pass 4th argument of pthread_create shown above? shouldn't pointer variable?

just example (not meant correct way of doing it; serve example code want play it):

#include <stdio.h> #include <sys/types.h> #include <stdlib.h> #include <pthread.h>  void *print_number(void *number) {     printf("thread received parameter value: %d\n", number);     return (void *)number; }  int main(int argc, char *argv[]) {     pthread_t thread;     void *ret;     int pt_stat;     pt_stat = pthread_create(&thread, null, print_number, (void *)100);     if (pt_stat) {         printf("error creating thread\n");         exit(0);     }      pthread_join(thread, &ret);     printf("return value: %d\n", ret);      pthread_exit(null);      return 0; } 

this lead undefined behavior if pointer value greater int can hold. see quote c99:

any pointer type may converted integer type. except specified, result implementation-defined. if result cannot represented in integer type, behavior undefined. result need not in range of values of integer type.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -