volatile的问题

#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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值