threads 2-局部变量和全局变量

main里面定义的变量都是属于主线程的
主线程创建的子线程不能访问之
需用全局变量进行线程间通信
[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);



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值