Linux多线程编程示例

下面的代码是的改进版本, 用多线程实现读卡器的读卡。

#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!\r\n");
    }

while(1)
    {
        if(ReadId(reader_fd,buf,32)==1)
        {
            printf("Read ID=%s\r\n",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 ... \r\n");
            return -1;
     }


     printf("prthread_create is created... \r\n");

rc2=pthread_create(&tidp,NULL,ReaderThread,NULL);
     if(rc2!=0)
     {
            printf("ReaderThread is not created ... \r\n");
            return -1;
     }


     printf("ReaderThread is created... \r\n");

while(1)
     {
         printf("System is Runing...\r\n");
         sleep(1);
     }

return 0;

程序运行效果如下图:

ARM_Linux多线程编程示例

linux

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wyzxgz.html