Hello,
I am a beginner here and working with Z-Stack HA sample application, and I want to know if I can use multithreading within the sample app.
I am registering a timer event in "int tcp_send_packet(server_details_t * server_details, uint8_t * buf, int len)" on a separate thread.
I don't want to send the next command unless there is 200ms gap between successive commands(since multiple commands aren't working and after reading throughout the forum I learned about the delay to be introduced) hence I am trying to do it by creating a thread to introduce the delay separately irrespective of execution hierarchy in Gateway sample app.
I have 3 questions.
1. Is multithreading going to work this way in this sample app ?
2. If yes, How/Where do I make changes to compile the code ? (I couldn't identify the spot where (in build_sample_app.c or in makefile) to add/link "-pthread" to compile it. I will take care of semaphores and concurrency once it is confirmed that it will work this way.)
3. Why doesn't it work this way ? What are my options ?
I have written this code
void delayTimerCb (void* arg)
{
delayKill(&delayTimer);
delayFlag = 0;
exitFlag = 1;
}
void* thread_poll_delay (void* arg)
{
delaySetTimer(&delayTimer, 200, false, delayTimerCb, NULL);
delayFd = malloc(sizeof(struct pollfd));
delayFd.fd = delayTimer.fd;
delayFd.events = POLLIN;
while(1)
{
if (!delayFlag)
{
break;
}
if ((poll(delayFd.fd, 1, -1)) > 0)
{
if (delayFd.revents & delayFd.events)
delayFd.timer_handler_cb(delayFd.timer_handler_arg);
if (exitFlag)
{
exitFlag = 0;
break;
}
}
else
{
UI_PRINT_LOG("Delay fd not initialised")
break;
}
}
}
int tcp_send_packet(server_details_t * server_details, uint8_t * buf, int len)
{
FILE *tcp;
pthread_attr_t attr;
int p, s ;
pthread_t threadID;
/*
s = pthread_attr_init(&attr);
if (s != 0)
{
perror("pthread_attr_init");
exit(-1);
}
*/
p = pthread_create(&threadID, NULL, thread_poll_delay, &variables);
while(delayFlag);
if (write(polling_fds[server_details->fd_index].fd, buf, len) != len)
{
return -1;
}
delayFlag = 1;
tcp = fopen("/home/debian/Desktop/tcp.txt", "a+");
fprintf(tcp, "Sending Packet Successful\n");
fclose(tcp);
return 0;
}
Thanks in advance