[etherlab-users] Multi-threading and calling the cyclic task issue

Paul Mulligan mulligan252 at yahoo.ie
Wed Jan 27 20:54:29 CET 2016


Hi ,
I am writing my applications based on the examples that come with the Master installation.
The cyclic_task() is executed every time on reception of a timer signal by calling the pause() blocking function. This works if using just one thread.
When using multi-threading, such that the function which calls the cyclic_task() is running in a separate detached thread from the main thread, the pause function blocks forever and a signal is never received as below. 
Does anyone have a solution for this ?
Also, I tried to use usleep() and sleep() instead of signals to wait for the right time to call the cyclic_task(), but the timing is not right. It seems that the sleeps can not wait longer than 10ms.
Thanks in advance for any help.

/****************************************************************************/
void signal_handler(int signum){    switch (signum)    {        case SIGALRM:            sig_alarms++;            break;    }}
/****************************************************************************//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////This function is assigned to a std::thread and detached
void Ethercat::threadFunc(){    struct sigaction sa;    struct itimerval tv;
    sa.sa_handler = signal_handler;    sigemptyset(&sa.sa_mask);    sa.sa_flags = 0;    if (sigaction(SIGALRM, &sa, 0))    {        fprintf(stderr, "Failed to install signal handler!!\n");    }

    printf("Starting timer...\n");    tv.it_interval.tv_sec = 0;    tv.it_interval.tv_usec = 1000000 / (FREQUENCY * 10);    tv.it_value.tv_sec = 0;    tv.it_value.tv_usec = 1000000 / ( FREQUENCY * 10);    if (setitimer(ITIMER_REAL, &tv, NULL))    {        fprintf(stderr, "Failed to start timer: %s\n", strerror(errno));    }


    printf("Starting cyclical task.\n");
    while (1)    {
#if 0        struct timeval t;        gettimeofday(&t, NULL);        printf("%u.%06u\n", t.tv_sec, t.tv_usec);#endif        pause();        while (sig_alarms != user_alarms)        {            cyclic_task();            user_alarms++;        }
           }
}
/****************************************************************************/

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.etherlab.org/pipermail/etherlab-users/attachments/20160127/3f02d54f/attachment-0002.htm>


More information about the Etherlab-users mailing list