Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions concurrent_example/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CC = g++
#---------------------------------------------------------
CPPFLAGS=-I../include
LDFLAGS=-pthread
#---------------------------------------------------------
MAIN="concurrent_example"
#---------------------------------------------------------
all: ${MAIN}

${MAIN}:
${CC} ${MAIN}.cpp -o ${MAIN} ${CPPFLAGS} ${LDFLAGS}

53 changes: 53 additions & 0 deletions concurrent_example/concurrent_example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <iostream>
#include <unistd.h>
#include <thread>
#include "progresscpp/ProgressBar.hpp"

/*
* Function which is thrown as thread
*/
void workerThread(int total, progresscpp::ProgressBar& progressBar) {
for (int i = 0; i < total; i++) {
++progressBar; // record the tick

usleep(200); // simulate work

// display the bar only at certain steps
if (i % 10 == 0)
progressBar.display();
}
}

/* Example usage of ProgressBar by multiple threads */
int main() {
const int partial = 1000;

/*
* Define a progress bar that has a total of 10000,
* a width of 70, shows `#` to indicate completion
* and a dash '-' for incomplete
*/
progresscpp::ProgressBar progressBar(10 * partial, 70, '#', '-');

/*
* Throw ten threads which access the same ProgressBar
* instance concurrently
*/
std::thread workers[10];
for (int i = 0; i < 10; i++) {
workers[i] = std::thread(workerThread, partial, std::ref(progressBar));
}

/*
* Wait for threads to finish the work
*/
for (int i = 0; i < 10; i++) {
workers[i].join();
}

// tell the bar to finish
progressBar.done();

std::cout << "Done!" << std::endl;
return 0;
}
8 changes: 7 additions & 1 deletion include/progresscpp/ProgressBar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

#include <chrono>
#include <iostream>
#include <mutex>

namespace progresscpp {
class ProgressBar {
private:
unsigned int ticks = 0;
mutable std::mutex mtx;

const unsigned int total_ticks;
const unsigned int bar_width;
Expand All @@ -20,7 +22,10 @@ class ProgressBar {

ProgressBar(unsigned int total, unsigned int width) : total_ticks{total}, bar_width{width} {}

unsigned int operator++() { return ++ticks; }
unsigned int operator++() {
std::unique_lock<std::mutex> lck (mtx);
return ++ticks;
}

void display() const {
float progress = (float) ticks / total_ticks;
Expand All @@ -29,6 +34,7 @@ class ProgressBar {
std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
auto time_elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - start_time).count();

std::unique_lock<std::mutex> lck (mtx); // stdout must be accessed in mutual exclusion
std::cout << "[";

for (int i = 0; i < bar_width; ++i) {
Expand Down