Code Monkey home page Code Monkey logo

threadweaver's Introduction

ThreadWeaver

Helper for multithreaded programming

Introduction

ThreadWeaver is a helper for multithreaded programming. It uses a job-based interface to queue tasks and execute them in an efficient way.

You simply divide the workload into jobs, state the dependencies between the jobs and ThreadWeaver will work out the most efficient way of dividing the work between threads within a set of resource limits.

See the information on [use cases](@ref usecases) and [why multithreading can help](@ref multithreading), as well as the usage section below, for more detailed information.

Usage

If you are using CMake, you need to have

find_package(KF5ThreadWeaver NO_MODULE)

(or similar) in your CMakeLists.txt file, and you need to link to KF5::ThreadWeaver.

ThreadWeaver is a Job queue. It executes jobs in threads it internally manages. The minimum and maximum number of threads provided by a Weaver is set by the user. Jobs are regular QObjects, which allows users to connect to the done() signal to be notified when the Job has been executed. The Weaver class provides objects that handle a number of threads called the inventory. Users usually acquire a reference to a WeaverInterface object.

Jobs may depend on other jobs. A job will only execute if all jobs it depends on are already finished. In this, dependencies reorder job execution. If no dependencies are declared, jobs are executed in queueing order. Multiple dependencies are possible, which allows the creation of complex flow graphs that are automatically executed by the Weaver. It is important, though, to avoid circular dependencies. Two jobs that depend on each other in both directions will simply never be executed, since the dependencies will never resolve.

Threads are created on demand and do not exit until the containing weaver is deleted. Threads have an eager policy in trying to execute jobs out of the queue. The managing Weaver blocks them if no jobs are available.

WeaverObservers are used to receive more informative events about the thread states and job execution. They can be used to provide progress or debugging information or to implement GUIs to show the thread activity. Observers can be attached to Weavers and will disconnect automatically when they are deleted.

Job Execution

In general, jobs are executed in the order they are queued, if they have no unresolved dependencies. This behaviour can be used to balance I/O, network and CPU load. The SMIV example shows how this can be done.

Emitting Signals from Jobs

To notify the application's GUI of progress or other events, it may be desirable to emit signals from the Job objects that can be connected to the main thread. Since the job will be executed in another thread, such signals are delivered asynchronously.

The Job class in the ThreadWeaver library itself contains such a helper class that can be used as a reference for this approach.

threadweaver's People

Contributors

agateau avatar aleixpol avatar arichardson avatar cordlandwehr avatar cornelius avatar dfaure avatar er-vin avatar jpwhiting avatar jriddell avatar kensington avatar krf avatar krop avatar maddimax avatar milianw avatar montel avatar nicolas17 avatar ochurlaud avatar randomguy3 avatar steckdenis avatar vkrause avatar winterz avatar

Watchers

 avatar  avatar  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.