主线程与分支线程的变量访问

任务:

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

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

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

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

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

1.任务1程序代码

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


int a =10;
//线程执行体,该线程执行体中指定了线程应该做什么任务;
void* callBack(void* arg) 	//void* arg = NULL;             //分支线程
{
	while(1)
	{
		printf("this is other function\n");
		printf("a=%d\n",a);
		sleep(1);
	}

	return NULL;
}

int main(int argc, const char *argv[])
{
	//创建一个分支线程
	pthread_t tid;
	if(pthread_create(&tid, NULL, callBack, NULL) != 0)
	{
		fprintf(stderr, "pthread_create failed\n");        //主线程
		return -1;
	}

	while(1)
	{
		printf("this is main function\n");
		printf("a=%d\n",a);
		sleep(1);
	}


	return 0;
}

任务1运行结果:

          定义一个全局变量 int a=10,主线程能访问到,分支线程也i能访问到。

2.任务2程序代码

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


int a =10;
//线程执行体,该线程执行体中指定了线程应该做什么任务;
void* callBack(void* arg) 	//void* arg = NULL;             //分支线程执行
{
	a = 20;                //修改分支线程中的a为20
	while(1)
	{
		printf("this is other function\n");
		printf("a=%d\n",a);
		sleep(1);
	}

	return NULL;
}

int main(int argc, const char *argv[])
{
	//创建一个分支线程
	pthread_t tid;
	if(pthread_create(&tid, NULL, callBack, NULL) != 0)
	{
		fprintf(stderr, "pthread_create failed\n");        
		return -1;
	}

	while(1)                       //主线程执行
	{
		printf("this is main function\n");
		printf("a=%d\n",a);
		sleep(1);
	}


	return 0;
}

 任务2运行结果

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

任务3,4程序代码

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


//线程执行体,该线程执行体中指定了线程应该做什么任务;
void* callBack(void* arg) 	//void* arg = NULL;             //分支线程执行
{
	int c =2;                 //分支线程中定义个变量c=2
	while(1)
	{
		printf("this is other function\n");
		printf("b=%d c=%d\n",b,c);
		sleep(1);
	}

	return NULL;
}

int main(int argc, const char *argv[])
{
	//主线程中定义个变量b=1
	int b=1;
	//创建一个分支线程
	pthread_t tid;
	if(pthread_create(&tid, NULL, callBack, NULL) != 0)
	{
		fprintf(stderr, "pthread_create failed\n");        
		return -1;
	}

	while(1)                       //主线程执行
	{
		printf("this is main function\n");
		printf("b=%d c=%d\n",b,c);
		sleep(1);
	}


	return 0;
}

任务3,4运行结果

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

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

 

任务5程序代码

(1)分支线程访问主线程中的变量

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


void* callBack(void *arg) 	 
{	                 //分支线程执行
	while(1)
	{
		printf("this is other function\n");
		printf("b=%d\n",*(int *)arg);
		sleep(1);
	}

	return NULL;
}


int main(int argc, const char *argv[])
{
	//创建一个分支线程
	pthread_t tid;
	int b=1;
	if(pthread_create(&tid, NULL, callBack,&b) != 0)
	{
		fprintf(stderr, "pthread_create failed\n");
		return -1;
	}

	//主线程执行
	while(1)
	{
		//分支线程执行
		printf("this is main functiion\n");
		printf("b=%d\n",b);
		sleep(1);
	}
	return 0;
}

运行结果

 

 (2)主线程访问分支线程中的变量

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


//线程执行体,该线程执行体中指定了线程应该做什么任务
void* callBack(void* arg) 	//void* arg = NULL;           
{
	//a为局部变量,要用static修饰,不然返回a的地址时候可能空间不存在,会报段错误
	static int	c= 2;	  //分支线程执行

	printf("this is other function\n");
	printf("c=%d\n",c);
	sleep(1);
	return &c;       
}

int main(int argc, const char *argv[])
{
	//创建一个分支线程
	pthread_t tid;
	if(pthread_create(&tid, NULL, callBack, NULL) != 0)
	{
		fprintf(stderr, "pthread_create failed\n");
		return -1;
	}
	int *p = (int *)callBack(NULL);         //将void *类型指针强转成int *类型

	while(1)                                       //主线程执行
	{
		printf("this is main functiion\n");
		printf("c=%d\n",*p);
		sleep(1);
	}


	return 0;
}

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值