diff options
author | deanm@google.com <deanm@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-09-02 11:15:48 +0000 |
---|---|---|
committer | deanm@google.com <deanm@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-09-02 11:15:48 +0000 |
commit | a89d97fdbea8bf1e246f2adf33819342f684d944 (patch) | |
tree | 8f46b5c03c99276e6f35521127424134f0a79e17 /base/simple_thread.h | |
parent | 5f6eee53a4ba6995b68a8ec6f22d3281529060c8 (diff) | |
download | chromium_src-a89d97fdbea8bf1e246f2adf33819342f684d944.zip chromium_src-a89d97fdbea8bf1e246f2adf33819342f684d944.tar.gz chromium_src-a89d97fdbea8bf1e246f2adf33819342f684d944.tar.bz2 |
Add a simple thread pool to SimpleThread.
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@1634 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/simple_thread.h')
-rw-r--r-- | base/simple_thread.h | 50 |
1 files changed, 49 insertions, 1 deletions
diff --git a/base/simple_thread.h b/base/simple_thread.h index 1c7a82b..edcbfbe 100644 --- a/base/simple_thread.h +++ b/base/simple_thread.h @@ -41,8 +41,11 @@ #define BASE_SIMPLE_THREAD_H_ #include <string> +#include <queue> +#include <vector> #include "base/basictypes.h" +#include "base/lock.h" #include "base/waitable_event.h" #include "base/platform_thread.h" @@ -70,7 +73,7 @@ class SimpleThread : public PlatformThread::Delegate { // configuration involving the thread creation and management. // Every thread has a name, in the form of |name_prefix|/TID, for example // "my_thread/321". The thread will not be created until Start() is called. - SimpleThread(const std::string& name_prefix) + explicit SimpleThread(const std::string& name_prefix) : name_prefix_(name_prefix), name_(name_prefix), thread_(), event_(true, false), tid_(0), joined_(false) { } SimpleThread(const std::string& name_prefix, const Options& options) @@ -136,6 +139,51 @@ class DelegateSimpleThread : public SimpleThread { Delegate* delegate_; }; +// DelegateSimpleThreadPool allows you to start up a fixed number of threads, +// and then add jobs which will be dispatched to the threads. This is +// convenient when you have a lot of small work that you want done +// multi-threaded, but don't want to spawn a thread for each small bit of work. +// +// You just call AddWork() to add a delegate to the list of work to be done. +// JoinAll() will make sure that all outstanding work is processed, and wait +// for everything to finish. You can reuse a pool, so you can call Start() +// again after you've called JoinAll(). +class DelegateSimpleThreadPool : public DelegateSimpleThread::Delegate { + public: + typedef DelegateSimpleThread::Delegate Delegate; + + DelegateSimpleThreadPool(const std::string name_prefix, int num_threads) + : name_prefix_(name_prefix), num_threads_(num_threads), + dry_(true, false) { } + ~DelegateSimpleThreadPool(); + + // Start up all of the underlying threads, and start processing work if we + // have any. + void Start(); + + // Make sure all outstanding work is finished, and wait for and destroy all + // of the underlying threads in the pool. + void JoinAll(); + + // It is safe to AddWork() any time, before or after Start(). + // Delegate* should always be a valid pointer, NULL is reserved internally. + void AddWork(Delegate* work, int repeat_count); + void AddWork(Delegate* work) { + AddWork(work, 1); + } + + // We implement the Delegate interface, for running our internal threads. + virtual void Run(); + + private: + const std::string name_prefix_; + int num_threads_; + std::vector<DelegateSimpleThread*> threads_; + std::queue<Delegate*> delegates_; + Lock lock_; // Locks delegates_ + WaitableEvent dry_; // Not signaled when there is no work to do. +}; + } // namespace base #endif // BASE_SIMPLE_THREAD_H_ |