volatile 的作用


add volatile 0
add volatile 1
monitor change volatile 1
add volatile 2
monitor change volatile 2
add volatile 3
monitor change volatile 3
add volatile 4
monitor change volatile 4
add volatile 5
monitor change volatile 5
 

 

-------------------------

 

#include <sys/time.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#define __USE_GNU
#include <sched.h>
#include <ctype.h>
#include <pthread.h>


volatile int g_ainV = 0;

int *thread_fun1(void *arg)
{
    int i = g_ainV;
    while(g_ainV<5)
    {
        g_ainV = i++;
        printf("add volatile %d\n", g_ainV);
        sleep(5);
    }
    
    return NULL;
}

int *thread_fun2(void *arg)
{
    int j = g_ainV;
    while(j<5)
    {
        if (j != g_ainV)
        {
            j = g_ainV;
            printf("monitor change volatile %d\n", j);
        }
        sleep(1);
    }
    
    return NULL;
}


void setparam(pthread_attr_t * pattr, unsigned int cpuindex)
{
    cpu_set_t cpu_info;
    pthread_attr_init(pattr);
        
    CPU_ZERO(&cpu_info);
    CPU_SET(cpuindex, &cpu_info);
    if (0!=pthread_attr_setaffinity_np(pattr, sizeof(cpu_set_t), &cpu_info))
    {
        printf("set affinity failed");
        return;
    }
}

void main()
{
    cpu_set_t cpu_info;

    pthread_t thid1;
    pthread_attr_t attr1;
    setparam(&attr1, 1);
    (void)pthread_create(&thid1, &attr1,(void *)thread_fun1, NULL);

    pthread_t thid2;
    pthread_attr_t attr2;
    setparam(&attr2, 2);
    (void)pthread_create(&thid2, &attr2,(void *)thread_fun2, NULL);


    while(1)
    {
        sleep(50);
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值