c-pthreads:pthread_create失败,错误代码为11(EAGAIN)

时间:2017-03-15 06:01:42

标签: c linux pthreads

我们有一个设置,我们使用我们的定制板连续录制视频到SD卡。但从长远来看,我们正面临pthread_create的问题。

我尝试使用pthread_create生成线程时得到EAGAIN。但是,根据我所检查的内容,线程似乎已被正确终止。

未封闭的文件句柄是否有可能在导致此EAGAIN时起作用(即它们共享相同的资源空间)?

我尝试使用属性PTHREAD_CREATE_DETACHED。这是我的代码段。

int alarm_main_loop(int qid, int audQid, SemHandl_t hAlarmSem)
{
    ALARM_MSG_BUF msgbuf;
    int msg_size, ret = 0;
    int event_index, sch_index;
    AlarmParam_t* pEvironment;
    pthread_t thread;
    pthread_attr_t attr;

    pAlarmSysInfo = GetSysInfo();
    if((hGIOSem = MakeSem()) == NULL) {
        return -1;
    }
    /* ADDED TO SEND SCHEDULE INFO TO SCHEDULE MANAGER */
    for (event_index = 0; event_index < MAX_NO_OF_EVENTS; event_index++) {
        for (sch_index = 0; sch_index < MAX_SCHEDULES; sch_index++) {
            Set_Event_Schedule(pAlarmSysInfo>event_details[event_index].event_schedule[sch_index], event_index, sch_index, 0);
        }
    }
    while(1)
    {
        /* Get Message */
        msg_size = msgrcv( qid, &msgbuf, sizeof(msgbuf) - sizeof(long), MSG_TYPE_MSG1, 0);
        if( msg_size < 0 ){
            printf("Alarm Receive msg fail \n");
            ret = -1;
            break;
        } else if(msgbuf.src == MSG_TYPE_MSG1 || msgbuf.src < 0){
            printf("Got Error message\n");
            ret = -1;
            break;
        } else if(msgbuf.event == ALARM_EVENT_QUIT){
            printf("Recieved Quit event\n");
            break;
        } else if(IsRestart()){
            // If already restart flag is set then ignore the messages
            continue;
        } else {
            /* Process alarm event */
            pEvironment =  (AlarmParam_t*)malloc(sizeof(AlarmParam_t));
            if(pEvironment == NULL){
                printf("Error: More memory need for Alarm server\n");
                break;
            }
            pEvironment -> msg_id    = qid;
            pEvironment -> audMsg_id = audQid;
            pEvironment->hAlarmSem   = hAlarmSem;
            memcpy(&pEvironment -> msg_buf, &msgbuf, sizeof(msgbuf));
            pthread_attr_init(&attr);
            pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); 
            if((ret = pthread_create(&thread, &attr, ProcAlarmThread,pEvironment)) != 0){
                free(pEvironment);
                printf("Error: Alarm server creat thread fail with error no %d\n", ret);
                break;
            }
            pthread_attr_destroy(&attr);
            pthread_detach(thread);
        }
    }
    DestroySem(hGIOSem);
    return ret;
}
在线程内部调用

alarm_main_loop()。此功能会持续检查是否收到消息并采取相应的操作。

连续跑了近2天后,我们遇到了这个问题。

有谁能告诉我这背后的原因是什么?

我们如何调试此问题?

我们尝试pthread_join代替pthread_detach(没有任何属性),但仍然遇到了同样的问题。

0 个答案:

没有答案