Tushar
Tushar

Reputation: 499

How can I create a Barrier in c++?

I have 4-threads in my program. I want to execute all of them once and once all of them are executed then only I want to enter the next iteration of execution.

I got a code on stack overflow that implements the boost::barrier function in C++. But it does not seem to work for me. For one iteration it works fine. But for the next iteration, the program execution just hangs.

//Here is my top code:

#include <iostream>
#include <stdio.h>
#include <thread>
#include "abc_func.hpp"
#include <vector>
using namespace std;

int main() {
    abc obj1;
        obj1.num_threads(4);
    std::thread t1([&obj1](){
        for (int i=0; i<5; i++) {
        while (!obj1.abc_write(1));
        };
    });
    std::thread t2([&obj1](){
        for (int i=0; i<5; i++) {
        while (!obj1.abc_read(2));
        };
    });
    std::thread t3([&obj1](){
        for (int i=0; i<5; i++) {
        while (!obj1.abc_write(3));
        };
    });
    std::thread t4([&obj1](){
        for (int i=0; i<5; i++) {
        while (!obj1.abc_read(4));
        };
    });
    t1.join();
    t2.join();
    t3.join();
        t4.join();


//  cout << "done: " << obj1.done << endl;

    // cout << "done: " << obj2.done << endl;

    // cout << "wr_count: " << obj1.wr_count << endl;
    return 0;   
}

// Here is the abc_func.hpp

#include <iostream>
#include <stdio.h>
#include <thread>
#include <barrier.hpp>

using namespace std;
class abc {
    size_t n_threads;

    public:
    abc() : n_threads(0) {};

        void num_threads (size_t l) {
          n_threads = l;
        }

    Barrier task_bar{n_threads};

    bool abc_write (auto id) {
          thread_local int wr_count = 0;
          if (wr_count == 1) {
            std::cout << "write thread waiting" << id << endl;
            task_bar.Wait();
            wr_count = 0;
          };
      std::cout << "write thread running " << id << endl;
          ++wr_count;
      return true;
    }

    bool abc_read (auto id) {
          thread_local int rd_count=0;
          if (rd_count == 1) {
            std::cout << "read thread waiting" << id << endl;
            task_bar.Wait();
            rd_count = 0;
          };
      std::cout << "read thread running " << id << endl;
          ++rd_count;
      return true;
    }

};

// and the barrier class code which I got on stack overflow

#include <thread>
#include <condition_variable>
#include <mutex>
#include <iostream>
#include <stdio.h>

class Barrier {
public:
    explicit Barrier(std::size_t iCount) : 
      mThreshold(iCount), 
      mCount(iCount), 
      mGeneration(0) {
    }

    void Wait() {
        std::unique_lock<std::mutex> lLock{mMutex};
        auto lGen = mGeneration;
        if (!--mCount) {
            mGeneration++;
            mCount = mThreshold;
            mCond.notify_all();
        } else {
            mCond.wait(lLock, [this, lGen] { return lGen != mGeneration; });
        }
    }

private:
    std::mutex mMutex;
    std::condition_variable mCond;
    std::size_t mThreshold;
    std::size_t mCount;
    std::size_t mGeneration;
};

Upvotes: 1

Views: 992

Answers (1)

Olaf Dietsche
Olaf Dietsche

Reputation: 74058

The problem with this code is the member

Barrier task_bar{n_threads};

It is initialized once at the beginning while n_threads is 0. Later, when you call

obj1.num_threads(4);

the barrier object is not updated.

When you update the barrier as well, it works as expected

class Barrier {
public:
// ...
    void num_threads(size_t n) {
        mThreshold = n;
        mCount = n;
    }
// ...
};

and in abc::num_threads()

void num_threads (size_t l) {
    n_threads = l;
    task_bar.num_threads(l);
}

Upvotes: 1

Related Questions