Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++98 的写法 #6

Open
Jian-Yin-Shine opened this issue May 1, 2021 · 0 comments
Open

C++98 的写法 #6

Jian-Yin-Shine opened this issue May 1, 2021 · 0 comments

Comments

@Jian-Yin-Shine
Copy link

在从任务队列中取出任务时,用了一个while(task)的写法,会不会过于复杂了。可以稍微简化一下。直接在回调函数里面进行。

void* ThreadPool::threadFunc(void* arg)
    {
        pthread_t tid = pthread_self();
        ThreadPool* pool = static_cast<ThreadPool*>(arg);
        while (pool->isRunning_)
        {
            pthread_mutex_lock(&pool->mutex_);
            while (pool->taskQueue_.empty() && pool->isRunning_) {
                pthread_cond_wait(&pool->condition_, &pool->mutex_);
            }

            Task* task = pool->taskQueue_.front();
            pool->taskQueue_.pop_front();

            pthread_mutex_unlock(&pool->mutex_);
            task->run();
        }
        return 0;
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant