I want to make a process with 3 threads. Out of which, I want one thread to work once in every 50ms. So made 2 threads to do my other works and in the third thread I initialised a timer. When I did so the synchronisation between the threads doest seem that good. I cant find the timer codes executing in every 50ms. Its random in nature. The code brief is shown below. Thanks in advance.

void * vUserInterfaceThread()
{
    while(1)
    {
        //***doing my interface code here***********/
    }
}
void * vMornitorThread()
{
    while(1)
    {
        //***doing my monitor code here***********/
    }
}
void * vTimerThread()
{
    vStartTimer(ENABLE); // enabled the timer with 50ms delay with the function 
     while(1);
}
void vTimerFunction()
{
    //******Code to be executed in every  50ms time duration here************//
}
void vStartTimer(unsigned char ucValue)
{
    if(ucValue == ENABLE)
    {
        memset (&sSigActionStruct, 0, sizeof (sSigActionStruct));
        sSigActionStruct.sa_handler = &vTimerHandler;
        sigaction (SIGVTALRM, &sSigActionStruct, NULL);

        iTimerValue.it_value.tv_sec = 0;
        iTimerValue.it_value.tv_usec = TIMERLOADVALUE; //Load value for 50ms

        iTimerValue.it_interval.tv_sec = 0;
        iTimerValue.it_interval.tv_usec = TIMERLOADVALUE; //Load value for 50ms
        setitimer (ITIMER_VIRTUAL/*ITIMER_REAL*/, &iTimerValue, NULL);
    }
}

int main(void)
{
    //***************doing other initialisations***************************//
        pthread_create(&pThreadID1,NULL,vUserInterfaceThread,NULL);
        pthread_create(&pThreadID2,NULL,vMornitorThread,NULL);
        pthread_create(&pThreadID3,NULL,vTimerThread,NULL);
        pthread_join(pThreadID1,NULL);
        pthread_join(pThreadID2,NULL);
        pthread_join(pThreadID3,NULL);
}
有帮助吗?

解决方案

To answer a part of your question:

If you want to adjust your thread priorities you can use pthread_attr_setschedparam

pthread_attr_t thread_attributes;
pthread_attr_init(&thread_attributes);
struct sched_param params = {.sched_priority = 15}; // Set higher/lower priorities in other threads
pthread_attr_setschedparam(&thread_attributes, &params);
pthread_create(&pThreadID1, &thread_attributes, vUserInterfaceThread, NULL);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top