作业需求:
任务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