main里面定义的变量都是属于主线程的
主线程创建的子线程不能访问之
需用全局变量进行线程间通信
则编译不会通过
但可冒充pthread_create的参数来进行父子线程的数据交换,如下
,以至于在子线程中修改了str,主线程中也可得知
也可单向传递,主线程-->子线程,即传值,如下
下面是pthread_create的原型
主线程创建的子线程不能访问之
需用全局变量进行线程间通信
[root@localhost ch12]# cat test4.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
void* thread_function(void *arg);
void *thread_result;
char *str="hello";
int flag=0;
int main()
{
puts(str);
int res;
pthread_t a_thread;
res=pthread_create(&a_thread,NULL,&thread_function,NULL);
if(res!=0){
perror("pthread_create error");
exit(EXIT_FAILURE);
}
printf("already create a thread\n");
res = pthread_join(a_thread, &thread_result);
puts(str);
}
void* thread_function(void *arg)
{
str="hi";
puts(str);
pthread_exit("thanks");
}
[root@localhost ch12]#
[root@localhost ch12]# make test4
cc -D_REENTRANT -lpthread test4.c -o test4
[root@localhost ch12]# ./test4
hello
already create a thread
hi
hi
如果将line 11 ,char *str="hello";放到main里面则编译不会通过
[root@localhost ch12]# make test4
cc -D_REENTRANT -lpthread test4.c -o test4
test4.c: In function ‘thread_function’:
test4.c:31:1: error: ‘str’ undeclared (first use in this function)
test4.c:31:1: note: each undeclared identifier is reported only once for each function it appears in
make: *** [test4] Error 1
局部变量虽然不可以如此明目张胆的通信---被编译器揪出来le 但可冒充pthread_create的参数来进行父子线程的数据交换,如下
[root@localhost ch12]# cat test4.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
void* thread_function(void *arg);
void *thread_result;
int flag=0;
int main()
{
char str[]="hello";
puts(str);
int res;
pthread_t a_thread;
res=pthread_create(&a_thread,NULL,&thread_function,str);
if(res!=0){
perror("pthread_create error");
exit(EXIT_FAILURE);
}
printf("already create a thread\n");
res = pthread_join(a_thread, &thread_result);
puts(str);
}
void* thread_function(void *arg)
{
strcpy(arg, "hi");
puts(arg);
pthread_exit("thanks");
}
[root@localhost ch12]# make test4
cc -D_REENTRANT -lpthread test4.c -o test4
[root@localhost ch12]# ./test4
hello
already create a thread
hi
hi
定义主线程的局部变量str,充当pthread_create的参数传递给子线程,传的是地址,以至于在子线程中修改了str,主线程中也可得知
也可单向传递,主线程-->子线程,即传值,如下
[root@localhost ch12]# cat test4.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
void* thread_function(void *arg);
void *thread_result;
int flag=0;
int main()
{
char *str="hello";
puts(str);
int res;
pthread_t a_thread;
res=pthread_create(&a_thread,NULL,&thread_function,str);
if(res!=0){
perror("pthread_create error");
exit(EXIT_FAILURE);
}
printf("already create a thread\n");
res = pthread_join(a_thread, &thread_result);
puts(str);
}
void* thread_function(void *arg)
{
arg="hi";
puts(arg);
pthread_exit("thanks");
}
[root@localhost ch12]# make test4
cc -D_REENTRANT -lpthread test4.c -o test4
[root@localhost ch12]# ./test4
hello
already create a thread
hi
hello
可见在主线程中第二次打印的str仍然是hello下面是pthread_create的原型
[root@localhost ch12]# man 3 pthread_create
#include <pthread.h>
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);