I create 2 threads, and main() will pass argc and argv to thread, so I use struct arg
that contains argc
and argv
.
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct arg {
int argc;
char id[9];
char **argv;
};
int test1(struct arg *a) {
int argc;
char **argv;
argc = a->argc;
argv = a->argv;
if (argc > 3) {
printf("arg 1 is %s, arg 2 is %s arg 3 is %s\n\n", *(argv+1), *(argv+2),a->id);
}
else {
exit(0);
}
return 0;
}
int main(int argc, const char *argv[]) {
int i;
void *ret;
struct arg *a = (struct arg *) malloc(sizeof(struct arg *) * atoi(argv[3]));
pthread_t *thread = (pthread_t *) malloc(sizeof(*thread) * atoi(argv[3]));
for(i = 0; i < atoi(argv[3]); ++i) {
a[i].argc = argc;
a[i].argv = argv;
sprintf(a[i].id,"%d",i);
pthread_create( &thread[i], NULL , test1 ,(struct arg *)&(a[i]));
}
for(i = 0; i < atoi(argv[3]); ++i) {
pthread_join( thread[i], &ret);
}
return 0;
}
I exec my program:
./test 1 2 2
The output looks like:
arg 1 is 1, arg 2 is 2 arg 3 is 0
arg 1 is , arg 2 is arg 3 is 1
In thread 1 the argv is correct, but in thread 2 the argv
address is different than before.
I use gdb to print a->argv address
- in main argv address is 0x7fffffffdee8
- in thread 1 argv address is 0x7fffffffdee8
- in thread 2 argv address is 0x7ffff6fef700
[Switching to Thread 0x7ffff6fef700 (LWP 19472)]
argv address is the same as thread address
I check arg[1]->argv before pthread_create
the address is 0x7fffffffdee8.
Will pthread_create
change the address?
Is there something I'm missing?