C++ Threading Bi-Directional Rendezvous - Chad Salinas ::: Data Scientist
Use Slidify to create motion charts to share with the world on rPubs Chad Salinas or GitHub.
Slidify, rPubs Chad Salinas, Motion Charts, Hans Rosling
1133
post-template-default,single,single-post,postid-1133,single-format-standard,qode-listing-1.0.1,qode-social-login-1.0,qode-news-1.0,qode-quick-links-1.0,qode-restaurant-1.0,ajax_fade,page_not_loaded,,qode-title-hidden,qode_grid_1300,qode-theme-ver-12.0.1,qode-theme-bridge,bridge,wpb-js-composer js-comp-ver-5.4.2,vc_responsive
a

C++ Threading Bi-Directional Rendezvous

Internet Ring Buffer example of Bi-directional Rendezvous
static char internet[8];
static semaphore emptyBuffers[8];
static semaphore fullBuffers[0];
static void writer() {
    for( int i = 0; i < 400; i++) {
        char ch = randomChar();  // calculate the next char to be written to the internet
        // need a slot on the shared internet to write this char
        emptyBuffers.wait();
        internet[ i % 8 ] = ch;        // the exact spot where the next char goes
        fullBuffers.signal();
    }
}
static void reader() {
    for( int i = 0; i < 400; i++) {
        // reader needs to wait for there to be a character to read
        fullBuffers.wait();
        char ch = internet[ i % 8 ];   // from shared internet to a local var
        emptyBuffers.signal();
        processChar(ch);
    }
}
int main(  ) {
    thread w(writer);
    thread r(reader);
    w.join();
    r.join();
    return 0;
}
Myth-Buster example to show polling a bunch of servers to find out how many processes are running on each server. Goal is to surface the least used server.
Sequential Version
short countProcesses(unsigned short num, const set<string> &studentids);
void compileProcessCountMap(map<short, int> &m, const set<string> &studentids) {
    for( unsigned short num = 1; num <= highestServerNumber; num++) {
        short count = countProcesses(num, studentids);
        if(count >= 0) {
            m[num] = count;  // add the new key/value to the map
        }
    }
}
int main() {
    set<string> studentids;
    readStudentids(studentids, “/class/studentids.txt”);
    map<short, int> processCountMap; // map of server ids to the number of processes
    // pass in the map to update and the student’s ids from the class
    compileProcessCountMap(processCountMap, studentids);
    return 0;
}

Chad Salinas working on Plotly

Chad Salinas working on analytics project.

Chad Salinas late nighter
Concurrent Version of Myth-Buster
static set<string> studentids;
static map<short, int> counts;
static mutex m;
static void compileMap() {
    thread threads[highestServerNumber];
    for( size_t i = 0; i < highestServerNumber; i++) {
        threads[i] = thread(
        );
    }
    for(thread &t : threads) {
        t.join();
    }
}
int main() {
    readStudentids(“/class/studentids.txt”);
    compileMap();
    return 0;
}

Let the dataset change your mindset

– Hans Rosling
No Comments

Sorry, the comment form is closed at this time.