Linux下Thread使用demo

这里的场景是串口自发自收。

先贴代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
int readRet = 0;
void *CH_Factory_Uart_Selfcheck_Reading_Thread(void *arg) {
SINT32 count = 0;
SINT32 port_id = 0;// use uart 0
SINT32 read_len = 0;
char buf[] = "changhong";
memset(buf,0,sizeof(buf));
while(1)
{
RHAL_UART_ReadUart(port_id,(UINT8 *)buf,&read_len,sizeof(buf));
count++;
if(read_len > 0 && strcmp(buf,"changhong") == 0) {
ALOGD("[%s] [%d]: count = %d, buf = %s, read_len = %d\n",__func__,__LINE__,count,buf,read_len);
ALOGD("[%s] [%d]: OOOOOOOOOOOOOOOOK!\n",__func__,__LINE__);
readRet = 1;
pthread_exit(0);
}
if(count > 20) {
ALOGD("[%s] [%d]: ERRORRRRRRRRRRRR!\n",__func__,__LINE__);
readRet = 0;
pthread_exit(0);
}
}
return NULL;
}
int CSystemControl::CH_Factory_Uart_Check(void)
{
ALOGD("[%s] [%d]\n",__func__,__LINE__);
int ret = 0;
SINT32 read_len = 0;

char buf[] = "changhong";
SINT32 port_id = 0;// use uart 0
pthread_t readThreadId;

system("stop console");

if(RHAL_UART_SelectUart(port_id)!=0)
{
system("start console");
return false;
}

pthread_create(&readThreadId, NULL, CH_Factory_Uart_Selfcheck_Reading_Thread, NULL);

ret = RHAL_UART_WriteUart(port_id,(UINT8 *)buf,sizeof(buf));
if(0 != ret)
{
ALOGD("[%s] [%d]: write uart fail\n",__func__,__LINE__);
pthread_join(readThreadId, NULL);
RHAL_UART_SelectUart(1); //switch back to UART1
system("start console");
return false;
}

pthread_join(readThreadId, NULL);

RHAL_UART_SelectUart(1); //switch back to UART1
system("start console");

if(!readRet)
return false;

return true;
}

以上代码线程的点在于:

  1. 创建一个ID

    pthread_t readThreadId;

  2. 创建了线程,ID与实际的线程函数匹配了

    pthread_create(&readThreadId, NULL, CH_Factory_Uart_Selfcheck_Reading_Thread, NULL);

    CH_Factory_Uart_Selfcheck_Reading_Thread就是被创建的线程,IDreadThreadId

  3. 退出线程

    pthread_join(readThreadId, NULL);

    pthread_join会阻塞主线程,等待CH_Factory_Uart_Selfcheck_Reading_Thread结束。CH_Factory_Uart_Selfcheck_Reading_Thread是一个while(1)循环,当串口读到需要的数据时,通过pthread_exit(0)使线程内部结束,这个时候跳到主线程的pthread_joinpthread_join终于等到了CH_Factory_Uart_Selfcheck_Reading_Thread结束,主线程就可以继续跑下去了。