分段错误使用线程

时间:2013-04-11 08:16:17

标签: c++ multithreading segmentation-fault

我正在尝试构建自己的库来使用和调度线程。

系统正在以make maximum 100 threads的方式工作,使其“准备就绪”。然后有一个调度程序,每个时间段,获取活动线程,将他放入就绪列表,并将第一个等待线程放在就绪列表中。

我的测试人员正在创建500000个线程,每个线程都会创建一个新线程并尝试自行终止。第二个线程(第一个线程创建)做同样的事情。而第三个只是终结。

这是测试人员:

void f3()
{
    printf("f3 before terminate\n");
    uthread_terminate(uthread_get_tid());
    printf("f3 after terminate\n");
}

void f2()
{
    printf("f2 before spawn\n");
    uthread_spawn(f3);
    printf("f2 after spawn\n");
    uthread_terminate(uthread_get_tid());
    printf("f2 after termination\n");
}

void f1()
{
    printf("f1 before spawn\n");
    uthread_spawn(f2);
    printf("f1 after spawn\n");
    uthread_terminate(uthread_get_tid());
    printf("f1 after termination\n");
}



int main(int argc, char **argv)
{
   printf("test8:\n--------------\n");
   cout << "* Output should be:\n";    
   cout << "--------------\n";
   cout << "***1***\n";
   cout << "***2***\n";
   cout << "Output is:\n";
   uthread_init(100);
   printf("***1***\n");
   for (volatile int i=0; i< 50000;++i){
        uthread_spawn(f1);
   }
   printf("***2***\n");

   uthread_terminate(0);
   return 0;
}

当我的程序进入我的调度程序时出现“分段错误”:

static void scheduler(){
DBG(("Schedular "))
nQuantum++;
if (ready.size()!=0){
    if (active != NULL){
        if (active->getState() == Thread::RUNNING){
            active->setState(Thread::READY);
            ready.push_back(active);
        }

        int val = sigsetjmp(*active->getEnv(),1);
        if (val !=0){
            blockTimerSignal(UNBLOCK);
            return;
        }
    }

    // Set new Active
    active = ready.front();
    DBG(("Active thread Id: %d",active->getTid()))
    ready.pop_front();
    DBG(("Doing pop front on ready list"))
    DBG(("Number of threads in ready list is  - %d",(ready.size())))
    active->setQuantums(active->getQuantums()+1);
    active->setState(Thread::RUNNING);
    setTimer();
    blockTimerSignal(UNBLOCK);
    DBG(("UNBLOCKED"))
    siglongjmp(*active->getEnv(),1);
    DBG(("After siglong jmp"))
}else{
    active->setQuantums(active->getQuantums()+1);
}
DBG(("Number of threads in ready list is  - %d",(ready.size())))

blockTimerSignal(UNBLOCK);
 }

执行siglongjmp(*active->getEnv(),1);

仅在主线程(ID为0的程序的第一个线程)中发生。 它也会在程序运行一段时间后发生,这意味着,在程序中间,它可以为活动线程执行siglongjmp,但是当它在一段时间后再次尝试时,它会给出错误。

如果有帮助,请添加我的终止功能:

int uthread_terminate(int tid) {
    DBG(("Terminate - %d", tid))

    if (tid == 0){
        // delete all (threads) - don't think it's needed because using stl!
        // TODO : check if needed - and then delete all lists !
        //Added by Roni - Deleting all lists!
        while(!sleeping.empty()){
            delete (sleeping.front());
            sleeping.pop_front();
        }

        while(!suspended.empty()){
            delete (suspended.front());
            suspended.pop_front();
        }

        while(!ready.empty()){
            delete (ready.front());
            ready.pop_front();
        }


        exit(0);
    }

    pThread t = getThread(tid);
    if (t == NULL){
        errmsgLibrary(THREAD_NOT_FOUND);
        return FAIL;
    }
    Thread::threadState state = t->getState();
    DBG(("Terminate - %d in State %d", tid, state))
    DBG(("Number of threads in ready list is  - %d",(ready.size())))
    blockTimerSignal(BLOCK);
    switch (state){
    case (Thread::RUNNING):
        //DBG(("Running Case"))
        //DBG(("Active thread id is: %d ",active->getTid()))
            delete active;
            active = NULL;
    //DBG(("Finsihed running Case"))
            scheduler();

            break;
    case (Thread::READY):
        //DBG(("ready Case"))
            ready.remove(t);
            delete t;
            //DBG(("Finsihed ready Case"))
        break;
    case (Thread::SLEEP):
            sleeping.remove(t);
            delete t;
        break;
    case (Thread::SUSPENDED):
            suspended.remove(t);
            delete t;
            break;
    default:
        break;
    }
    //DBG(("Number of threads in ready list is  - %d",(ready.size())))
    blockTimerSignal(UNBLOCK);
    return SUCCESS;
}

1 个答案:

答案 0 :(得分:0)

猜测,你在这里使用siglongjmp,并且可能没有改变调用之间的堆栈。

请考虑使用getcontext()swapcontext()makecontext()