Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / ARM_Linux多线程编程示例

下面的代码是http://www.linuxidc.com/Linux/2012-02/53901.htm的改进版本, 用多线程实现读卡器的读卡。 #include <pthread.h>
#include"reader.h"void *create(void *arg)
{
    while(1)
    {
    printf("thread is Running ..... ");
    sleep(5);
    }}void *ReaderThread(void *arg)
{
    int reader_fd;
    char buf[32];    if((reader_fd=OpenReader("/dev/tty0"))==-1)
    {
        puts("Open Dev Error! ");
    }    while(1)
    {
        if(ReadId(reader_fd,buf,32)==1)
        {
            printf("Read ID=%s ",buf);
        }
    }    CloseReader(reader_fd);
}int main(int argc, char *argv[])
{     pthread_t tidp;
    int rc1,rc2;     rc1=pthread_create(&tidp,NULL,create,NULL);
     if(rc1!=0)
     {
            printf("pthread_create is not created ... ");
            return -1;
     }
     printf("prthread_create is created... ");     rc2=pthread_create(&tidp,NULL,ReaderThread,NULL);
     if(rc2!=0)
     {
            printf("ReaderThread is not created ... ");
            return -1;
     }
     printf("ReaderThread is created... ");     while(1)
     {
         printf("System is Runing... ");
         sleep(1);
     }    return 0;
} 程序运行效果如下图: