Linux-create a countdown timer thread
Angie An

Linux create a countdown timer thread

Create an independent countdown timer thread, the program ends automatically when the countdown is over.

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
void *check_timeout(void *arg)
{
int second_now = 0;
int count = 0;

//Get current time and second
while (1)
{
count++;
sleep(1);
if(count >= TIMEOUT_CNT)
{
printf("Timeout!\r\n");
// kill specific proccess
system("ps | grep busybox | grep -v grep | awk '{print $1}' | xargs kill -s 9");
exit(0);
}

// if websocket received data, clear count
if((strlen(idata) >= 1))
{
count = 0;
}
}
}

//creat pthread to check time
pthread_t thread_check_time;
pthread_create(&thread_check_time, NULL, &check_timeout, NULL);

 Comments