开发者

Break the limit of threading, segmentation fault

use pthread_create to create limited number of threads running concurrently Successfully compile and run

However, after adding function pointer array to run the function, Segmentation fault

Where is wrong?

workserver number: 0 Segmentation fault

void* workserver(void *arg)
{
int status;
while(true)
{
    printf("workserver number: %d\n", (int)arg);
    (* job_queue[(int)arg])();
    sleep(3);

    status = pthread_mutex_lock(&data.mutex);
    if(status != 0)
        printf("%d lock mutex", status);
    data.value = 1;
    status = pthread_cond_signal(&data.cond);
    if(status != 0)
        printf("%d signal condition", status);
    status = pthread_mutex_unlock(&data.mutex);
    if(status != 0)
        printf("%d unlock mutex", status);
}
}


void jobadd(void (*job)())
{   
    for(int i; i<3; i++)
    {
        if(idle[i] == 0)
        {
            job_queue[i] = job;
            job;
            idle[i] = 1;
        }
    }
}
void func1()
{
    printf("func1 run");
}
void func2()
{
    prin开发者_如何学Gotf("func2 run");
}
void func3()
{
    printf("func3 run");
}
void func4()
{
    printf("func4 run");
}

int main(int argc, char* argv[])
{
    jobadd(func1);
    jobadd(func2);
    jobadd(func3);
    jobrun();
    return 0;
}

initialize function pointer array with jobadd function


add more detail in the question

job_queue[i] = job

just assign a function to the function pointer array

as can not assign function pointer array directly to pthread_create


After placing function pointer array (* job_queue[(int)arg])(); at the end it can successfully call function with the following result even if i add a infinite loop and a sleep in func1, it will not block at func1

now i am working on how to replace existing running function with a new function using pthread_cleanup_push and pop

  • workserver number: 0 func1 run
  • workserver number: 2 func3 run
  • workserver number: 1 func2 run
  • workserver number: 0 func1 run
  • workserver number: 2 func3 run
  • workserver number: 1 func2 run
  • workserver number: 2 workserver
  • number: 0 func3 run func1 run
  • workserver number: 1 func2 run
  • workserver number: 0 workserver
  • number: 2 func1 run func3 run
  • workserver number: 1 func2 run
  • workserver number: 2 workserver
  • number: 0 func3 run func1 run
  • workserver number: 1 func2 run
  • workserver number: 0 workserver
  • number: 2 func1 run func3 run
  • workserver number: 1 func2 run

    while(true) { printf("workserver number: %d\n", (int)arg);

    status = pthread_mutex_lock(&data.mutex);
    
    //pthread_cleanup_push(
    
    if(status != 0)
         printf("%d lock mutex", status);
    data.value = 1;
    status = pthread_cond_signal(&data.cond);
    if(status != 0)
        printf("%d signal condition", status);
    status = pthread_mutex_unlock(&data.mutex);
    if(status != 0)
        printf("%d unlock mutex", status);
    
    (* job_queue[(int)arg])();
    sleep(1);
    void (* clean)(void *);
    clean = NULL;
    /*
    for(int i=0; i<4; i++)
    {
        if(idle[i] == 0)
        {
            if(fp[(int)arg] != NULL)
            {
                idle[running_number[(int)arg]] = 0; // replace its running job
                clean = fp[(int)arg];
                fp[(int)arg] = job_queue[i];
                pthread_cleanup_push(clean, NULL);
                (*fp[(int)arg])();
                pthread_cleanup_pop(1);
    
                idle[i] = 1;
            }
            else
            {
                fp[(int)arg] = job_queue[i];
                idle[i] = 1;
                running_number[(int)arg] = i;
                (*fp[(int)arg])();
            }
        }
    }*/
    

    }

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜