项目作者: zeliard

项目描述 :
G.O.D - Grand Object-bound Dispatcher
高级语言: C++
项目地址: git://github.com/zeliard/Dispatcher.git
创建时间: 2014-02-24T09:15:04Z
项目社区:https://github.com/zeliard/Dispatcher

开源协议:MIT License

下载


G.O.D: Grand Object-bound Dispatcher

G.O.D is a high performance non-blocking task dispatcher which guarantees class member functions’ execution sequence

FEATURES

  • Designed for multi-platform by using C++11 standard
    • Visual Studio Solutions for now, but you can easily use in *NIX environment (Just #include relevant files)
  • Lock-free algorithms (non-blocking)
  • Using custom STL allocator
  • Deferred task execution using Timer
  • Simple code-base for easy to understand (easy to adapt to other projects)

REAL WORLD EXAMPLES

BRANCHES

  • base_version: basic version for educational purposes
  • bind_version: std::bind is adopted instead of a custom Job class interface
  • LB_version: load-balancing among worker-threads (new arrival!)

HOW TO USE

  1. // First, you can attach G.O.D (AsyncExecutable) to an object like this:
  2. class TestObject : public AsyncExecutable
  3. {
  4. public:
  5. void TestFunc(double a, int b)
  6. {
  7. // do something...
  8. // (e.g.) someobj->DoAsync(...);
  9. }
  10. // ... ...
  11. };
  12. // somewhere ...
  13. auto testobj = std::make_shared<TestObject>(); ///< new TestObject;
  14. // And then, make your own worker thread which implements Runnable::Run() like this:
  15. class TestWorkerThread : public Runnable
  16. {
  17. public:
  18. virtual bool Run()
  19. {
  20. // Now, you can call a member function like this:
  21. testobj->DoAsync(&TestObject::TestFunc, 100.123, 456);
  22. // or, deferred execution 1000ms later like this:
  23. testobj->DoAsyncAfter(1000, &TestObject::TestFunc, 100.123, 456);
  24. // ... ...
  25. }
  26. };
  27. // Lastly, run worker-threads at the main() function
  28. JobDispatcher<TestWorkerThread> workerService(WORKER_COUNT);
  29. workerService.RunWorkerThreads();

For more information, just see self-explaning DispatcherTest.cpp

WARNING!

  1. You should use x64 mode due to std::atomic variable alignment
  2. You should guarantee the lifetime (i.e. AsyncExecutable::mRefCount > 0) of an object inherited from AsyncExecutable
  3. Be careful to use DoAsync(…) in a blockable LOCK (critical section). It can cause deadlock…