lizhenghn123 / zl_threadpool Goto Github PK
View Code? Open in Web Editor NEWLinux平台下C++(C++98、C++03、C++11)实现的线程池
License: MIT License
Linux平台下C++(C++98、C++03、C++11)实现的线程池
License: MIT License
在从任务队列中取出任务时,用了一个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;
}
Is there a way to intervene if the thread is stuck? such as print the stack when the thread is stuck?
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.
` 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;
}
`
In file ThreadPoolCpp11/ThreadPool.h, line 105.
cond_.wait will release the associated unique_lock before the pop action.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.