const extern static 终极指南(转)
原文:https://www.cnblogs.com/machao/p/5882302.html
参考:
https://blog.csdn.net/peisir/article/details/54799662
参考博文: volatile,让我保持原样
device.c
#include <stdio.h>
 
#include <pthread.h>
 
#include <unistd.h>
 
 
 
 
int g_ready = 0;
 
//int volatile g_ready = 0;
 
 
void* init_device(void* args)
 
{
 
    sleep(5);
 
 
 
    g_ready = 1;
 
 
 
    printf("init_device() - device status : g_ready = %d\n", g_ready);
 
}
 
 
 
void launch_device()
 
{
 
    pthread_t tid = 0;
 
 
 
    pthread_create(&tid, NULL, init_device, NULL);
 
}
mai#include <stdio.h>
 
#include <pthread.h>
 
 
extern  volatile const int g_ready;
//extern  const  volatile int g_ready;
 
 
 
int main()
 
{
 
    launch_device();
 
 
 
    while( g_ready == 0 )
 
    {
 
        sleep(1);
         
 
        printf("main() - launching device : g_ready = %d\n", g_ready);
 
    }
 
     
 
    printf("main() - device status : g_ready = %d\n", g_ready);
 
 
 
    return 0;
 
 
使用带优化选项的编译命令:
gcc -O3 main.c device.c -lpthread -o test.out
观察结论:
1、volatile 和const只在本文件有效
2、将main.c中的
extern volatile const int g_ready改为
extern volatile const char g_ready,依然可以编译通过。参考《 volatile,让我保持原样》中关于编译链接过程的描述,编译只是针对本文件。
posted on 2020-07-06 18:04 lh03061238 阅读(180) 评论(0) 收藏 举报
 
                    
                     
                    
                 
                    
                 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号