开发者

Multitasking how to make worker thread gain control after calling infinite loop function

开发者 https://www.devze.com 2023-01-02 16:36 出处:网络
assume creating 3 worker threads by pthread_create, in these worker thread routine, each call a simple infinite loop function which do not have a return to do counting

assume creating 3 worker threads by pthread_create,

in these worker thread routine, each call a simple infinite loop function which do not have a return to do counting

how to make worker thread gain control af开发者_运维技巧ter calling infinite loop function and save the context of infinite loop function for calling in worker thread again?


Let me rephrase to see if I understood the problem.

You have a master thread which spawns 3 worker threads which each do a long running (infinite) job.

At a certain point you want to interrupt processing, save the state of all threads to resume where they left off at a later time.

I think the best way of doing this is organize your threads work in transactionally bound chunks. When restarting, you check the last completed transaction, and go from there.

But since I suspect this to be a homework assignment in low level thread plumbing, may i suggest a shared boolean which is checked on every time you go through the loop to exit and store the state afterwards. Aternatively "kill" the thread and catch the exception and store the state. The last option is messy.


I think you should clarify your question.

If every worker thread calls an infinite loop then I suppose that your master thread would have to call pthread_cancel() on each of them. From what I gather this might require calls to other pthread_*() functions to set the "cancelability" of the target threads.

Of course this suggestion begs the question. The vastly preferable approach would be to prevent those infinite loops. Write your code so that it has exit conditions ... so that the work is bounded by some sort of input or has some sort of event handling.


want to do the effect of a threadpool, after calling infinite loop function, each worker thread can change other tasks(other infinite loop function) to run

for example 3 worker thread can run 4 tasks(infinite loop functions)

#ifndef JOB_CPP
#define JOB_CPP

#include "job.h"

#define NUM_OF_TASKS 4
#define NUM_OF_WORKERS 3
    void (* job_queue[NUM_OF_TASKS])(void*);
    void (* fp[NUM_OF_WORKERS])(void*); // original running job
    int running_task[NUM_OF_WORKERS];
    int idle[NUM_OF_TASKS];
    int last_running_task[NUM_OF_WORKERS];
    int no_of_tasks_running[NUM_OF_WORKERS];
    my_struct_t data = {PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, 0};

void func1(void *arg)
{
    int count = 0;
    int status;
    while(true)
    {
        //if((count % 100) == 0)
        //printf("func1 run %d\n", count);
        count = count + 1;
        //status = pthread_cond_signal(&data.cv);
    }
}
void func2(void *arg)
{
    int count = 0;
    int status;
    while(true)
    {
        //printf("func2 run %d\n", count);
        count = count + 1;
        //status = pthread_cond_signal(&data.cv);
    }
}
void func3(void *arg)
{   int count = 0;
    int status;
    while(true)
    {
        //printf("func3 run %d\n", count);
        count = count + 1;
        //status = pthread_cond_signal(&data.cv);
    }
}
void func4(void *arg)
{   int count = 0;
    int status;
    while(true)
    {
        //printf("func4 run %d\n", count);
        count = count + 1;
        //status = pthread_cond_signal(&data.done);
    }
}

void jobinit()
{
    for(int i=0; i<NUM_OF_TASKS; i++)
    {
        job_queue[i] = NULL;
        idle[i] = 0;
    }
    for(int i=0; i<NUM_OF_WORKERS; i++)
    {
        fp[i] = NULL;
        running_task[i] = 0;
        last_running_task[i] = 0;
        no_of_tasks_running[i] = 0;
    }
    jobadd(func1);
    jobadd(func2);
    jobadd(func3);
    jobadd(func4);
    jobrun();
}
void jobadd(void (*job)(void*))
{   
    for(int i=0; i<4; i++)
    {
        if(job_queue[i] == NULL)
        {
            job_queue[i] = job;
            return;
        }
    }
}
void* workserver(void *arg);
void* workserver(void *arg)
{
    int status, timedout;

    struct timespec timeout;

    status = pthread_mutex_lock(&data.mutex);
    while(true)
    {
        timedout = 0;   
        clock_gettime(CLOCK_REALTIME, &timeout);
        timeout.tv_sec += 2;

        sleep(1);
        //void (* clean)(void*);

        status = pthread_cond_timedwait(&data.cv, &data.mutex, &timeout);
        if(status == ETIMEDOUT){
            printf("worker wait timed out %d\n", (int)arg);
            timedout = 1;
        }else if(status != 0){
            printf("worker wait failed %d\n", (int)arg);
            status = pthread_mutex_unlock(&data.mutex);
            return NULL;
        }
        printf("workserver number: %d\n", (int)arg);

        status = pthread_mutex_unlock(&data.mutex);     

        printf("function run %d\n", (int)arg);
        (* job_queue[(int)arg])(NULL);

        printf("cond wait start %d\n", (int)arg);
        status = pthread_cond_wait(&data.done, &data.mutex);
        printf("cond wait end\n");

        status = pthread_mutex_lock(&data.mutex);
    }
}
void jobrun() 
{   
    for(int i=0; i<3; i++) {idle[i] = 0;}
    pthread_t r1_threadid[3];

    for(int i=0; i<3; i++)  
    {
        pthread_create(&r1_threadid[i], NULL, workserver, (void*)i);
    }

    int status;
    struct timespec timeout;

    timeout.tv_sec = time (NULL) + 2;
    timeout.tv_nsec = 0;

    while(true)
    {
    status = pthread_mutex_lock(&data.mutex);
    while(data.value == 0)
    {
        status = pthread_cond_timedwait(&data.cond, &data.mutex, &timeout);
    }
    if(data.value != 0)
    {
        //printf("condition was signaled\n");
        data.value = 0;
    }
    status = pthread_mutex_unlock(&data.mutex);
    if(status != 0)
        printf("unlock mutex error");
    }
}
#endif
0

精彩评论

暂无评论...
验证码 换一张
取 消