测试多线程任务

作业需求:

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

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

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

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

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

作业实现过程如下:

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

答:主线和分支都能访问到,过程如下:

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

int a=10;

void *callback(void* arg)
{
	while(1)
	{
		printf("this other a=%d\n",a);
		sleep(1);
	}
}


int main(int argc, const char *argv[])
{
	pthread_t tid;
	if(pthread_create(&tid,NULL,callback,NULL)<0)
	{
		printf("create pthread failed\n");
		return -1;
	}


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

	return 0;
}

结果如下:

ubuntu@ubuntu:05_day$ gcc zuoye.c -pthread
ubuntu@ubuntu:05_day$ ./a.out 
this main a=10
this other a=10
this main a=10
this other a=10
this main a=10
this other a=10
this main a=10
this other a=10
this main a=10
this other a=10
this main a=10
this other a=10
this main a=10

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

答:主线访问到结果是20。

过程如下:

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

int a=10;

void *callback(void* arg)
{
	a=20;
	while(1)
	{
		printf("this other a=%d\n",a);
		sleep(1);
	}
}


int main(int argc, const char *argv[])
{
	pthread_t tid;
	if(pthread_create(&tid,NULL,callback,NULL)<0)
	{
		printf("create pthread failed\n");
		return -1;
	}


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

	return 0;
}

结果如下:

ubuntu@ubuntu:05_day$ gcc zuoye.c -pthread
ubuntu@ubuntu:05_day$ ./a.out 
this main a=10
this other a=20
this main a=20
this other a=20
this main a=20
this other a=20
this main a=20

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

答:不能

过程如下:

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

int a=10;

void *callback(void* arg)
{
	a=20;
	while(1)
	{
		printf("this other a=%d  b=%d\n",a,b);
		sleep(1);
	}
}


int main(int argc, const char *argv[])
{
	int b;
	pthread_t tid;
	if(pthread_create(&tid,NULL,callback,NULL)<0)
	{
		printf("create pthread failed\n");
		return -1;
	}


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

	return 0;
}

结果如下:

ubuntu@ubuntu:05_day$ gcc zuoye.c -pthread
zuoye.c: In function ‘callback’:
zuoye.c:12:38: error: ‘b’ undeclared (first use in this function)
   printf("this other a=%d  b=%d\n",a,b);
                                      ^
zuoye.c:12:38: note: each undeclared identifier is reported only once for each function it appears in
ubuntu@ubuntu:05_day$ 



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

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

int a=10;

void *callback(void* arg)
{
	a=20;
	int c=1;
	while(1)
	{
		printf("this other a=%d  c=%d\n",a,c);
		sleep(1);
	}
}


int main(int argc, const char *argv[])
{
	pthread_t tid;
	if(pthread_create(&tid,NULL,callback,NULL)<0)
	{
		printf("create pthread failed\n");
		return -1;
	}


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

	return 0;
}

结果如下:

ubuntu@ubuntu:05_day$ gcc zuoye.c -pthread
zuoye.c: In function ‘main’:
zuoye.c:32:37: error: ‘c’ undeclared (first use in this function)
   printf("this main a=%d  c=%d\n",a,c);
                                     ^
zuoye.c:32:37: note: each undeclared identifier is reported only once for each function it appears in
ubuntu@ubuntu:05_day$ 


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

实现过程:

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

int a=10;

void *callback(void* p)
{
	a=20;
	int *c=(int*)malloc(sizeof(int));
	int c1=1;
	c=&c1;

	while(1)
	{
		printf("this other a=%d b=%d c=%d\n",a,*(int*)p,*c);
		sleep(1);
	}
	return c;
}


int main(int argc, const char *argv[])
{
    int b=1;
	pthread_t tid;
	if(pthread_create(&tid,NULL,callback,&b)<0)
	{
		printf("create pthread failed\n");
		return -1;
	}


	int *c=(int*)callback(&b);
	if(NULL==c)
	{
		perror("main-c");
		return -1;
	}


	while(1)
	{
	
		printf("this main a=%d b=%d c=%d\n",a,b,*c);
		sleep(1);
	}

	return 0;
}

实现结果:

ubuntu@ubuntu:05_day$ gcc zuoye.c -pthread
ubuntu@ubuntu:05_day$ ./a.out 
this other a=20 b=1 c=1
this other a=20 b=1 c=1
this other a=20 b=1 c=1
this other a=20 b=1 c=1
this other a=20 b=1 c=1

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值