Giter Site home page Giter Site logo

optithread's Introduction

optithread

A very lightweight library for C and C++ to easily implement parallelisation.

HOW TO COMPILE:

The POSIX Threads library (pthread) is used for creating and operating on threads.

GCC:

c:

It uses the gcc extensions statement expressions and nested functions.

gcc main.c optithread.c -o test -lpthread

c++:

It uses lambda functions, therefore the c++11 standard is required

g++ main.c optithread.c -o test -lpthread -std=c++11

CLANG/LLVM:

It uses the blocks extension (http://clang.llvm.org/docs/LanguageExtensions.html#blocks) Make sure to have installed the blocksruntime library

It additionally uses statement expressions.

c:

clang main.c optithread.c -fblocks -o test -lpthread -lBlocksRuntime

c++:

clang++ main.c optithread.c -fblocks -o test -lpthread -lBlocksRuntime

HOW TO USE:

sample c source code:

#include "optilang.h"

int main() {

    // start threads
    opti_start_threads();

    // mark variable as block variable. Only needed for clang; has no effect if compiling with gcc. 
    // variables, which are referenced inside a parallel loop need to be marked as block variables. 
    // see clang blocks extension documentation for more information
    __block int a=0;
    
    // store loop identifier in variable c. The REF macro must immediately be followed by a loop macro
    REF(c)
    DO {
        int j;
        for (j=0; j<1000; j++) {
            usleep(1000);
            a++;
	    }
    } CONTINUE // let the other threads do the work. Don't wait for loop to complete.
    
    FOR (k,0,100,1) {
        do_something(a, k);
        return; // equivalent to continue in a normal loop
    } SYNC // wait for loop to complete
    
    // wait for loop identified by c to complete
    opti_wait_for(c);
    
    // tell threads to exit after completing their work
    opti_shutdown_threads();
    
    return 0;
}

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.