Skip to content

Instantly share code, notes, and snippets.

@ofZach
Created August 24, 2016 00:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ofZach/e944f19b96f861693f1a035ddc588ef9 to your computer and use it in GitHub Desktop.
Save ofZach/e944f19b96f861693f1a035ddc588ef9 to your computer and use it in GitHub Desktop.
non openmp openmp like behavior
#include "ofApp.h"
#include <thread>
#include <algorithm>
#include <vector>
#include <iostream>
#include <functional>
using namespace std;
#ifdef __GNUG__
static __thread int thread_num;
static __thread int num_threads;
#else
static thread_local int thread_num;
static thread_local int num_threads;
#endif
typedef function <void ()> task;
class thread_pool {
private:
vector<thread> the_pool;
public:
thread_pool(unsigned int n_threads, task tbd) {
for(int i = 0; i < n_threads; ++i) {
the_pool.push_back(thread([=] () {
thread_num = i;
num_threads = n_threads;
tbd();
}));
}
}
void join() {
for_each(the_pool.begin(), the_pool.end(),
[] (thread& t) {t.join();});
}
void nowait() {
for_each(the_pool.begin(), the_pool.end(),
[] (thread& t) {t.detach();});
}
};
#define parallel_(N) thread_pool (N, []()
#define parallel parallel_(thread::hardware_concurrency())
#define parallel_end ).join();
#define parallel_end_nowait ).nowait();
#define single if(thread_num==0)
//--------------------------------------------------------------
void ofApp::setup(){
parallel_(8)
{
cout << "I'm thread " << thread_num << " of "
<< num_threads << endl;
single
{
cout << "This region is executed only by thread "
<< thread_num << endl;
}
}
parallel_end
}
//--------------------------------------------------------------
void ofApp::update(){
}
//--------------------------------------------------------------
void ofApp::draw(){
}
//--------------------------------------------------------------
void ofApp::keyPressed(int key){
}
//--------------------------------------------------------------
void ofApp::keyReleased(int key){
}
//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mouseEntered(int x, int y){
}
//--------------------------------------------------------------
void ofApp::mouseExited(int x, int y){
}
//--------------------------------------------------------------
void ofApp::windowResized(int w, int h){
}
//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){
}
//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){
}
@ofZach
Copy link
Author

ofZach commented Aug 24, 2016

@ofZach
Copy link
Author

ofZach commented Aug 24, 2016

how to pass in a value (via lambda capture) so you can alter outside data....

#include "ofApp.h"

#include <thread>
#include <algorithm>
#include <vector>
#include <iostream>
#include <functional>

using namespace std;

#ifdef __GNUG__
static __thread int thread_num;
static __thread int num_threads;
#else
static thread_local int thread_num;
static thread_local int num_threads;
#endif

typedef function <void ()> task;
class thread_pool {
private:
    vector<thread> the_pool;

public:
    thread_pool(unsigned int n_threads, task tbd) {
        for(int i = 0; i < n_threads; ++i) {
            the_pool.push_back(thread([=] () {
                thread_num = i;
                num_threads = n_threads;
                tbd();
            }));
        }
    }

    void join() {
        for_each(the_pool.begin(), the_pool.end(),
                 [] (thread& t) {t.join();});
    }

    void nowait() {
        for_each(the_pool.begin(), the_pool.end(),
                 [] (thread& t) {t.detach();});
    }

};

#define parallel_(N) thread_pool (N, []()
#define parallel parallel_(thread::hardware_concurrency())
#define parallel_end ).join();
#define parallel_end_nowait ).nowait();
#define single if(thread_num==0)
#define parallelCap_(N,N2) thread_pool (N, [N2]()

//--------------------------------------------------------------
void ofApp::setup(){




    vector < int > counts[8];

    parallelCap_(8, &counts)
    {
        int split = 1000/8;
        for (int i = thread_num * split; i < (thread_num+1) * split; i++){
            counts[thread_num].push_back(i);
        }
    }
    parallel_end


    for (int i = 0; i < 8; i++){
        cout << "----------------" << endl;
        for (auto cnt : counts[i]){
            cout << cnt << endl;
        }
    }


}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment