GithubHelp home page GithubHelp logo

taskdistributor's Introduction

Task Distributor

A tiny solution to effectively manage multithreaded queues of tasks.

Main features

  • distribute multiple task to different threads using d.spread(tasks)
  • synchronize queues using d.wait() and tq.wait()
  • lock particular threads to increase program's efficiency using tq.lock()

How to use

#include <dstr/distributor.h>

using namespace dstr;

// Some magic.
int Sum(int x, int y)
{
    return x + y;
}

int main()
{
    // Create Distributor with 3 task queues inside.
    distributor d(3);
    d.start();

    // Declare some pretty stupid tasks.
    task t1 = []() { std::cout << "Time consuming\n"; Sleep(2000); };
    task t2 = []() { std::cout << "Easy and fast\n"; };
    task t3 = []() { std::cout << "Easy and fast\n"; };
    task t4 = []() { std::cout << "Easy and fast\n"; };
    std::vector<task*> easyTasks = { &t2, &t3, &t4 };

    // Lock the queue since a long operation
    // can reduce its throughput. This way we can
    // increase efficiency of our program.
    d.push_task(t1)->lock();
    // In this particular case we prevent pushing
    // two "time consuming" task into the same
    // task queue.

    // Spread (distribute to different task queues) a
    // vector of tasks in order to execute all of them
    // simultaneously.
    d.spread_tasks(easyTasks);
    // Second pushing of the "time consuming" task.
    d.push_task(t1);
    d.wait(); // Wait for all queues to finish their work.

    int x = 75, y = 25;
    int result;
    task getSum = [&]() { result = Sum(x, y); };
    std::cout << "\nCalculating...\n";
    d.push_task(getSum)->wait(); // Wait for the task to complete.
    // The line below will be executed after the task is completed.
    std::cout << "x + y = " << result << "\n";

    d.push_task([]() { std::cout << "\nSeems like time would fail me.\n"; });
    d.stop(); // Stop (freeze) all task queues.
    std::cout << "Feeding unicorns...\n";
    Sleep(3000);
    d.start(); // Resume their work.

    system("pause"); // That's all folks!
    return 0;
}

taskdistributor's People

Contributors

undrcrxwn avatar

Watchers

 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.