volatile的问题

来源:互联网 发布:gis软件平台 编辑:程序博客网 时间:2024/06/05 08:56
#include <pthread.h>#include "encryption_chip.h"#define GPIO_NUM PAD_GPIO20#define GPIO_DEV "/dev/gpioctl"#define SERIAL_DEV "/dev/ttyS4"int efd;volatile int flag = 0;//些处要用volatile限定,以防被优化void *thr(void *arg){    int i = 0;    char c;    while(1) {        if (flag == 1) {                        //被优化后,些处永远执行不到,一直为假            read(efd, &c, 1);            printf("receive data[%d]: 0x%x\n", i++, c);        }    }}int main(void){    //char data[] = {0x80, 0x08, 0x00, 0x00, 0x05, 0x01, 0x11, 0x22, 0x33, 0x44};    int gpio_num = GPIO_NUM;    pthread_t tid_rx;    if( 0 != pthread_create(&tid_rx, NULL, thr, NULL))        printf("can't create receive thread");    efd = encryption_chip_open(SERIAL_DEV, GPIO_DEV, gpio_num);    flag = 1;    pthread_join(tid_rx, NULL);    encryption_chip_close(efd);    return 0;}


原创粉丝点击