歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> ARM_Linux多線程編程示例

ARM_Linux多線程編程示例

日期:2017/3/1 10:33:18   编辑: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!\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;
}

程序運行效果如下圖:

Copyright © Linux教程網 All Rights Reserved