我的最小线程实现

1162阅读 0评论2011-12-22 乐百事2
分类:嵌入式

#include
#include
#include
pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
void threadone(void)
{
    while(1)
    {
        printf("i am one!\n");
        sleep(1);    //延时1秒
    }
}
void threadtwo()
{
    while(1)
    {
        printf("i am two!\n");
        sleep(1);
    }
}

int main(void)
{
    pthread_t id[2];
    int i,ret1,ret2;
    ret1=pthread_create(&id[0],NULL,(void *) threadone,NULL);//使id[*]和thread***相对应
    ret2=pthread_create(&id[1],NULL,(void *) threadtwo,NULL);
    pthread_mutex_init(&mut,NULL);
    while(1)
    {
        if(ret1!=0)
        {
            printf ("lesson is error!\n");
            exit(0);
        }
  
        if(ret2!=0)
        {
             printf ("lesson is error!\n");
             exit(0);
        }
        if(id[0] !=0)
        pthread_join(id[0],NULL);
        if(id[1] !=0)
        pthread_join(id[1],NULL);
    }
    return (0);
}
上一篇:windows下配置VisualSVN Server服务器
下一篇:南京银行、中国银行、工商银行、农业银行、建设银行、邮政储蓄银行对比介绍