任务:
任务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;
}