当前位置: 首页>>代码示例>>C++>>正文


C++ JobQueue::ThreadState_free方法代码示例

本文整理汇总了C++中JobQueue::ThreadState_free方法的典型用法代码示例。如果您正苦于以下问题:C++ JobQueue::ThreadState_free方法的具体用法?C++ JobQueue::ThreadState_free怎么用?C++ JobQueue::ThreadState_free使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在JobQueue的用法示例。


在下文中一共展示了JobQueue::ThreadState_free方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: DPRINTF


//.........这里部分代码省略.........
    //    struct timespec timeout;
    JobQueue *jq = (JobQueue *) arg;
    Job *job;
    int status;
    void *threadState = NULL;
    if(jq->ThreadState_new != NULL) {
        threadState = jq->ThreadState_new(jq->threadData);
        CHECKMEM(threadState);
    }

    for(;;) {
        //        clock_gettime(CLOCK_REALTIME, &timeout);
        //        timeout.tv_sec += 3;

        status = pthread_mutex_lock(&jq->lock); // LOCK
        if(status)
            ERR(status, "lock");
        else
            DPRINTF(("%s:%s:%d: locked\n", __FILE__, __func__, __LINE__));

        // Wait while the queue is empty and accepting jobs
        while(NULL == jq->todo && jq->acceptingJobs) {
            DPRINTF(("%s:%d:  awaiting work. todo=%p\n",
                     __func__, __LINE__, jq->todo));

            ++jq->idle;
            if(jq->idle == jq->nThreads) {
                status = pthread_cond_signal(&jq->wakeMain);
                if(status)
                    ERR(status, "signal wakeMain");
            }
            //status = pthread_cond_timedwait(&jq->wakeWorker, &jq->lock,
            //                                &timeout);
            status = pthread_cond_wait(&jq->wakeWorker, &jq->lock);
            --jq->idle;
            //if(status == ETIMEDOUT)
            //    continue;
            if(status)
                ERR(status, "wait wakeWorker");
        }

        /*
         * todo accepting
         *   0     0  <- exit
         *   0     1  <- stay in while loop
         *   1     0  <- do work
         *   1     1  <- do work
         */

        if(NULL == jq->todo) {  // shutting down
            assert(!jq->acceptingJobs);
            break;
        } else {                // do job
            DPRINTF(("%s %lu got work\n", __func__,
                     (unsigned long) pthread_self()));

            // remove job from queue
            assert(NULL != jq->todo);
            job = jq->todo;
            jq->todo = jq->todo->next;

#ifdef DPRINTF_ON
            printf("%s:%d:queue:", __func__, __LINE__);
            Job_print(jq->todo);
#endif

            status = pthread_mutex_unlock(&jq->lock);   // UNLOCK
            if(status)
                ERR(status, "unlock");
            else
                DPRINTF(("%s:%s:%d: unlocked\n", __FILE__, __func__,
                         __LINE__));

            DPRINTF(("%s %lu calling jobfun\n", __func__,
                     (unsigned long) pthread_self()));
            job->jobfun(job->param, threadState);
            DPRINTF(("%s %lu back fr jobfun\n", __func__,
                     (unsigned long) pthread_self()));
            free(job);
        }
    }
    // still have lock
    --jq->nThreads;

    status = pthread_cond_signal(&jq->wakeMain);
    if(status)
        ERR(status, "signal wakeMain");

    status = pthread_mutex_unlock(&jq->lock);   // UNLOCK
    if(status)
        ERR(status, "unlock");
    else
        DPRINTF(("%s:%s:%d: unlocked\n", __FILE__, __func__, __LINE__));

    if(threadState)
        jq->ThreadState_free(threadState);

    DPRINTF(("%s %lu exit\n", __func__, (unsigned long) pthread_self()));
    return NULL;
}
开发者ID:alanrogers,项目名称:jobqueue,代码行数:101,代码来源:jobqueue.c


注:本文中的JobQueue::ThreadState_free方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。