Giter Site home page Giter Site logo

pool's Introduction

Pool

A thread-pooled, asynchronous job library with an easy-to-use API


Get Started: Create a Thread Pool

// Instantiates a pool and gives you an IThreadPool interface
// In this case, we spin up 2 threads per CPU core
IThreadPool *ppool1 = pool::IThreadPool::Create(2, 0);

// In this case, we spin up 1 thread per CPU core, but reduce the core count
// used to compute the thread count by 3
IThreadPool *ppool2 = pool::IThreadPool::Create(1, -3);

// In this case, we spin up 3 threads total
IThreadPool *ppool3 = pool::IThreadPool::Create(3);

// You can also create a "pool" with 0 threads, add tasks from multiple threads, then execute them all on a single thread later
// by calling Flush. This is useful for graphics tasks, for example, where you may want to load texture or geometry data
// asynchronously but then upload to GPU memory in the main render thread.
IThreadPool *pGraphicsTasks = pool::IThreadPool::Create(0);

A Simple Example: Asynchronous Processing

First, write your task callback(s)...

pool::IThreadPool::TASK_RETURN __cdecl SimpleTask1(void *param0, void *param1, size_t task_number)
{
  // do a thing - like Sleep(10)
  Sleep(10);

  return pool::IThreadPool::TASK_RETURN::TR_OK;
}

pool::IThreadPool::TASK_RETURN __cdecl SimpleTask2(void *param0, void *param1, size_t task_number)
{
  // do a different thing - like Sleep(50)
  Sleep(50);

  return pool::IThreadPool::TASK_RETURN::TR_OK;
}

Then, somewhere in your code, run some tasks...

for (int i = 0; i < 100; i++)
  ppool1->RunTask(SimpleTask1);

for (int i = 0; i < 10; i++)
  ppool1->RunTask(SimpleTask2);

If your program's termination condition is variable and tasks may be left unfinished (and you don't want them to go on if it's time to quit), you can flush the task queue prior to waiting.

ppool1->PurgeAllPendingTasks();

Your tasks will now run, but will finish whenever they do - but, you can wait for them.

ppool1->WaitForAllTasks(INFINITE);

Another Simple Example: Multithreaded Optimization

pool::IThreadPool::TASK_RETURN __cdecl MyTask(void *param0, void *param1, size_t task_number)
{
  // print the task number, but don't really do anything with it
  char s[16];
  sprintf(s, "%d", (int)task_number);

  return pool::IThreadPool::TASK_RETURN::TR_OK;
}

Run a single task many times and wait on the result...

ppool1->RunTask(MyTask, nullptr, nullptr, 1000, true);

Wrapping Up

ppool1->Release();

pool's People

Contributors

keelanstuart avatar

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.