alex

a sample to explain how volatile works

//the code also can be a simple sample to implement timer


#include <stdio.h>
#include <signal.h>
#include <sched.h>
#include <sys/time.h>

#ifndef VOLATILE
#define VOLATILE
#endif

VOLATILE int total = 0;

void handle(int signo)
{
        static int count = 0;

/*      printf("timer expired %d times\n", ++count); */
        total++;
        printf("Total = %d\n", total);
}

int main()
{
        int i,x;
        struct itimerval val;
        struct sigaction sa;

        memset(&sa, 0, sizeof(sa));
        sa.sa_handler = handle;
        sigaction(SIGVTALRM, &sa, NULL);

        val.it_interval.tv_sec = 0;
        val.it_interval.tv_usec = 10000;
        val.it_value.tv_sec = 0;
        val.it_value.tv_usec = 10000;
        setitimer(ITIMER_VIRTUAL, &val, NULL);

        while(total < 500)
        {
                for(i = 0; i < 100; i++)
                {
                        x = x + i;
                }
        }

        printf("X = %d\n", x);
        printf("Total = %d\n", total);
}

 

posted on 2007-08-08 10:48 alex 阅读(127) 评论(0)  编辑 收藏 引用 所属分类: Technique


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理