大龙的博客

常用链接

统计

最新评论

gcc version 3.4.6 20060404 + 1字节对齐 有问题 用gcc 4.1没问题,或去掉对齐用gcc 3.4编译,或采用下下策(加锁)

#include <pthread.h>

#pragma pack(1)
struct test_t
{
        short b;
        char c;
        unsigned int a;
};
#pragma pack()
struct test_t test;
unsigned int n = 0xffffffff;

void* set( void* arg )
{
        while (1)
        {
                (&test)->a = 0;
                (&test)->a = n;
        }
}

void* get( void* arg )
{
        while (1)
        {
                unsigned int n1 = 0;
                n1 = (&test)->a;
                if (n1 == 0) continue;
                if (n1 != 0xffffffff )
                printf("ok%x\n", n1);
        }
}

int main( int argc , char **argv )
{
        pthread_t thread_id;
        int i;
        for (i = 0; i < 1; i ++)
        {
                pthread_create( &thread_id , NULL , set , ( void* )NULL );
        }

        for (i = 0; i < 1; i ++)
        {
                pthread_create( &thread_id , NULL , get , ( void* )NULL );
        }
        while(1) sleep(10);
        return 0;
}

posted on 2008-06-25 18:44 大龙 阅读(328) 评论(0)  编辑 收藏 引用


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