|
发表于 2022-5-5 10:28:12
|
显示全部楼层
您好,请教一下,关于这个问题,我延申了一下,采用多线程来测试。
环境:ubuntu 18.04, GCC优化等级开到最高,-O3,没有用volatile来申明,但是结果和楼主的现象不一样。
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <pthread.h>
- unsigned long adcyc;
- unsigned int adsum;
- unsigned char st_start;
- static void a20msloop(void)
- {
- adcyc = 0;
- st_start = 1;
- while (st_start == 1)
- {
- usleep(1);
- }
- adsum = +adcyc;
- printf("adsum:%d\r\n", adsum);
- }
- static void* thread_fun1(void *arg)
- {
- while (1)
- {
- a20msloop();
- }
- return NULL;
- }
- static void isr(void)
- {
- if (st_start == 1)
- {
- adcyc = 3;
- st_start = 0;
- }
- }
- void* thread_fun2(void *arg)
- {
- while (1)
- {
- sleep(1);
- isr();
- }
- return NULL;
- }
- int main(int argc, char **argv)
- {
- pthread_t thread1, thread2;
- pthread_create(&thread1, NULL, thread_fun1, NULL);
- pthread_create(&thread2, NULL, thread_fun2, NULL);
- void *ret = NULL;
- pthread_join(thread1, &ret);
- pthread_join(thread2, &ret);
- return 0;
- }
复制代码
运行结果:
adsum:3
adsum:3
adsum:3
adsum:3
adsum:3
|
|