Code Monkey home page Code Monkey logo

zl_threadpool's Introduction

zl_threadpool : 三个版本的线程池实现

Build Status

Linux平台下C++(C++98、C++03、C++11)实现的线程池

  • ThreadPoolCpp98

      最古老的做法,只使用了C++98语言规范,采用**面向对象的思路**,每一个任务都是一个子类对象;
    
  • ThreadPoolCpp03

      较新做法,使用C++03语言规范,还有C++0x(特指std::function + std::bind),与上面不同的是采用
      **基于对象的思路**,每一个任务都是一个std::function对象,std::function,std::bind真是好;
    
  • ThreadPoolCpp11

      最新做法,**完全采用C++11**技术,比如std::thread, mutex, condition_variable, atomic组件,
      还有lambda技巧,packaged_task, future等等;
    

zl_threadpool's People

Contributors

lizhenghn123 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

zl_threadpool's Issues

C++98 的写法

在从任务队列中取出任务时,用了一个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;
    }

signal missing question

I have read your code CPP03.
When program wants to stop, you will broadcast condition signal, but what if some thread is busy executing some job, I am afraid your condition signal will miss, and when this thread finish its job, it will still hold in blocking wait and can not exit as you wish.

98版本

` ThreadPool::ThreadPool(int threadNum)
{
threadsNum_ = threadNum;
createThreads();
isRunning_ = true; // <= 这里;是否应该写到前面去.
}

    int ThreadPool::createThreads()
    {
            pthread_mutex_init(&mutex_, NULL);
            pthread_cond_init(&condition_, NULL);
            threads_ = (pthread_t*)malloc(sizeof(pthread_t) * threadsNum_);
            for (int i = 0; i < threadsNum_; i++)
            {
                    pthread_create(&threads_[i], NULL, threadFunc, this); // <= 这里调用了threadFunc
            }
            return 0;
    }
    void* ThreadPool::threadFunc(void* arg)
    {
            pthread_t tid = pthread_self();
            ThreadPool* pool = static_cast<ThreadPool*>(arg);
            while (pool->isRunning_)   <= 这里;isRunning_没有初始化就拿来使用了.
            {
                    Task* task = pool->take();
                    if (!task)
                    {
                            printf("thread %lu will exit\n", tid);
                            break;
                    }

                    assert(task);
                    task->run();
            }
            return 0;
    }

`

#thread stuck (c11)

Is there a way to intervene if the thread is stuck? such as print the stack when the thread is stuck?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.