c++ - pthread_create with local variable as parameter -
may cause errors when use pthread_create in following way?
void myfunction(){ thread_t mythread; pthread_create(&mythread,0,myroutine,0); }
i not sure if usage of local variable mythread
can cause errors because ceases exist when myfunction()
exits.
could memory of mythread
still used in kernel when function exits?
i not need thread id stored in mythread
don't want allocate memory , free again.
you can pass pthread_create_detached part of thread attribute when create it. way, dont have worry joining thread later on. this:
pthread_t; int status; pthread_attr_t attr; status = pthread_attr_init(&attr); if (status != 0) { fprintf(stderr, "pthread_attr_init() failed [status: %d]\n", status); return 0; } status = pthread_attr_setdetachstate(&attr, pthread_create_detached); if (status != 0) { fprintf(stderr, "pthread_attr_setdetachstate() failed [status: %d]\n", status); return 0; } status = pthread_create(&t, &attr, myroutine, 0);
Comments
Post a Comment