验证有关线程的操作

任务一:

任务1:定义一个全局变量 int a=10,主线程能否访问到,分支线程能否访问到;    

答:能访问到

代码实现过程:

#include<stdio.h>
#include <pthread.h>
#include <unistd.h>

int global = 10;


void* ch_value(void* p){
	printf("line : %d this is child thread ---> global = %d\n",__LINE__,global);
}


int main(int argc, const char *argv[])
{
	pthread_t tid;
	if(pthread_create(&tid,NULL,ch_value,NULL) != 0){
		fprintf(stderr,"pthread_create failed\n");
		return -1;
	}
	printf("line : %d this is main thread ---> global = %d\n",__LINE__,global);
	sleep(1);
	return 0;
}

代码实现结果:

任务二:

分支线程中修改上述的a = 20, 问主线程中访问该a,是10还是20;

答:是20

代码实现过程:

#include<stdio.h>
#include <pthread.h>
#include <unistd.h>

int global = 10;


void* ch_value(void* p){
	global = 20;
	printf("line : %d this is child thread ---> global = %d\n",__LINE__,global);
}


int main(int argc, const char *argv[])
{
	pthread_t tid;
	if(pthread_create(&tid,NULL,ch_value,NULL) != 0){
		fprintf(stderr,"pthread_create failed\n");
		return -1;
	}
	sleep(1);
	printf("line : %d this is main thread ---> global = %d\n",__LINE__,global);
	return 0;
}

代码实现结果:

任务三:

在主线程定义一个局部变量int b=1,分支线程能否访问到b;

答:不能

追:作何操作访问到b

答:利用void *arg传递参数地址(地址传递)

代码实现过程:

#include<stdio.h>
#include <pthread.h>
#include <unistd.h>

int global_a = 10;


void* ch_value(void* p){
	global_a = 20;
	int local_c = 2;
	printf("line : %d this is child thread ---> global_a = %d\n",__LINE__,global_a);
	printf("line : %d this is child thread ---> local_b = %d\n",__LINE__,*(int*)p);
}


int main(int argc, const char *argv[])
{
	int local_b = 1;
	int *bptr = &local_b;
	pthread_t tid;
	if(pthread_create(&tid,NULL,ch_value,bptr) != 0){
		fprintf(stderr,"pthread_create failed\n");
		return -1;
	}
	//int local_b = 1;
	sleep(1);
	printf("line : %d this is main thread ---> global_a = %d\n",__LINE__,global_a);
	//printf("line : %d this is child thread ---> local_c = %d\n",__LINE__,local_c);
	return 0;
}

代码实现结果:

任务四:

在分支线程定义一个局部变量int c=2,主线程能否访问到c;

答:不能

追:做何操作能访问到c

答:在子线程定义局部变量是,在堆区申请地址记录局部变量的地址,并利用函数返回值void*返回到主函数,在主函数利用该地址访问c

代码实现过程:

#include<stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>

int global_a = 10;


void* ch_value(void* p){
	global_a = 20;
	int *cptr = (int*)malloc(sizeof(int));
	if(NULL == cptr){
		perror("malloc");
		return NULL;
	}
	int local_c = 2;
	cptr = &local_c;
//	printf("line : %d this is child thread ---> global_a = %d\n",__LINE__,global_a);
//	printf("line : %d this is child thread ---> local_b = %d\n",__LINE__,*(int*)p);
	return cptr;
}


int main(int argc, const char *argv[])
{
	int local_b = 1;
	int *bptr = &local_b;
	pthread_t tid;
	if(pthread_create(&tid,NULL,ch_value,bptr) != 0){
		fprintf(stderr,"pthread_create failed\n");
		return -1;
	}
	//int local_b = 1;
	sleep(1);
//	printf("line : %d this is main thread ---> global_a = %d\n",__LINE__,global_a);
	int *cptr = (int*)ch_value(NULL);
	if(NULL == cptr){
		perror("ch_value");
		return -1;
	}
	printf("line : %d this is child thread ---> local_c = %d\n",__LINE__,*cptr);
	return 0;
}

代码实现结果:

任务五:

如果任务34不能访问到,则如何修改代码让对方能够访问到;

见任务三、四

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值