I have a program doing 'assignments' that are scheduled by boost::threadpool. Each of those assignments take a lot of time (1 min - 5h per assignment). I wan't to monitor progress for each assignment, to understand how long will the process take.
There are a lot more assignments then there are threads, so only NUM_THREADS of progress outputs should be visible.
I have created a simple piece of code that does this, but there 2 problems:
I don't think it's elegant.
Don't know if incrementing prog like this is safe.
What would be the better ways of doing this?
#include <thread>
#include <iostream>
#include <atomic>
#include <vector>
volatile int prog[4]={0,0,0,0};
std::atomic<bool> prog_lock[4];
bool end(){
bool ret=true;
for(int i=0;i<4;i++)
if(prog[i]!=100){
ret=false;
break;
}
return ret;
}
void Func(){
int prog_ind=0;
for(int i=0;i<4;i++){
if(!prog_lock[i].exchange(true)){
prog_ind=i;
break;
}
}
for(int i=0;i<100;i++){
prog[prog_ind]++;
std::this_thread::sleep_for(std::chrono::milliseconds(300));
}
prog_lock[prog_ind].store(false);
}
int main(){
for(int i=0;i<4;i++)
prog_lock[i].store(false);
std::vector<std::thread> thr;
for(int i=0;i<4;i++){
thr.push_back(std::thread(Func));
}
while(!end()){
std::cout << std::string(50,' ') << "\r";
for(int i=0;i<4;i++){
std::cout << "Progress: " << prog[i] << "/100\t";
}
std::cout << "\r" << std::flush;
std::this_thread::sleep_for(std::chrono::seconds(1));
}
for(int i=0;i<4;i++)
thr[i].join();
}
Related
I have posted a code to print the pattern given below . just need to know if there is another efficient way to do it .
#include<iostream>
#include <string>
using namespace std;
int main()
{
for (int i = 0; i < 5; i++)
{
cout<< string (i+1, '*') << '\n';
}
return 0;
}
This code prints a pattern as drawn below:
*
**
***
****
*****
The most efficient way is:
#include <iostream>
int main()
{
std::cout <<
"*\n"
"**\n"
"***\n"
"****\n"
"*****";
}
You could use lower-level stdio-style calls if you want to avoid the overhead of string manipulations and I/O streams:
#include <stdio.h>
int main(int, char **)
{
for (int i = 1; i <= 5; i++)
{
for (int j = 0; j < i; j++) putc('*', stdout);
putc('\n', stdout);
}
return 0;
}
It's hard to imagine why efficiency would matter here, though.
The following is 4 lines.
#include <iostream>
int main()
{
std::cout << std::setfill('*'); // change fill
for (int i=1; i<= 5; ++i)
std::cout << std::setw(i) << '*' << '\n';
std::cout << std::setfill(' ') << endl; // restore fill
}
I will leave the measurement of run-time comparison to the OP.
I'm trying to write a program in C++ which will be responsible for simulating blinkers in cars. I want it to be simple and to compile it in a console window.
Is it possible to create one thread for input which will be always active and second for output that will run simultaneously?
I wanted to use threads to solve this but it doesn't work as I would like. I have a little trouble to understand threads. If anyone could help me to fix this I would be grateful.
int in()
{
int i;
cout<<"press 1 for left blinker or 0 to turn it off: ";
cin>>i;
return i;
}
void leftBlinker()
{
int i;
cout << "<-";
Sleep(1000/3);
cout << " ";
Sleep(1000/3);
}
int main()
{
thread t1 (in);
if (in()==1)
{
for (int i=0; i<100; i++)
{
thread t2(leftBlinker);
if (in()==0)
break;
}
}
system("pause");
return 0;
}
Here is a simple example code:
#include <atomic>
#include <chrono>
#include <iostream>
#include <thread>
int in(std::atomic_int &i) {
while (true) {
std::cout << "press 1 for left blinker or 0 to turn it off: ";
int input;
std::cin >> input;
i = input;
}
}
void leftBlinker(std::atomic_int &i) {
while (true) {
if (i) {
std::cout << "<-" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds{333});
std::cout << " " << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds{333});
}
}
}
int main() {
std::atomic_int i{0};
std::thread t1(in, std::ref(i));
std::thread t2(leftBlinker, std::ref(i));
t1.join();
t2.join();
return 0;
}
A reference to std::atomic_int is passed to both functions for communication. std::atomic_int ensures thread-safe reads and writes. At the end you should join or detach the threads.
I have a class object obj1 and I am trying to call a member function sdf_write from 2-separate-threads.
There is a static variable wr_count inside the member-function.
The issue is: when I run both threads, the wr_count value is being shared between both threads.
For e.g. thread_1 runs 8-times and makes the wr_count=8 but when thread_2 starts it makes the wr_count=9. I want thread_2 to start counting from "1" not from the last value of thread_1.
Here is my code:
#include <iostream>
#include <stdio.h>
#include <thread>
#include "sdf_func.hpp"
#include <vector>
using namespace std;
int main() {
sdf obj1;
std::thread t1([&obj1](){
for (int i=0; i<30; i++) {
while (!obj1.sdf_write(10));
};
});
t1.detach();
std::thread t2([&obj1](){
for (int i=0; i<30; i++) {
while (!obj1.sdf_write(10));
};
});
t2.join();
cout << "done: " << obj1.done << endl;
// cout << "done: " << obj2.done << endl;
// cout << "wr_count: " << obj1.wr_count << endl;
return 0;
}
// This is sdf_func/////////////////
#include <iostream>
#include <stdio.h>
#include <thread>
#include <mutex>
using namespace std;
class sdf {
public:
int done;
std::mutex mutex;
sdf() : done(0){};
void increment() {
std::lock_guard<std::mutex> guard(mutex);
++done;
}
bool sdf_write (auto size) {
static int wr_count = 0;
if (wr_count == size) {
wr_count = 0;
increment();
//cout << "done : " << done;
return false;
}
wr_count++;
cout << wr_count << "--" << std::this_thread::get_id() << endl;
return true;
}
};
This is a perfect job for the thread_local storage duration, which is a keyword introduced from C++11.
thread_local int wr_count;
Essentially, you get a separate static instance of wr_count per thread; each one is initialised to 0.
Reference: http://en.cppreference.com/w/cpp/keyword/thread_local
When using std::this_thread::get_id() on CodeBlocks using the c++11 compiler, the thread numbers start from 2. Every time I run the code bellow it prints threads 2 - 6 instead of 0 - 4. Why?
Could it be possible that some other c++ application that is running in the background is using up thread IDs 1 and 2? What sorcery is this?
#include <iostream>
#include <thread>
#include <mutex>
using namespace std;
std::mutex m;
class TClass
{
public:
void run()
{
m.lock();
cout << "Hello, I'm thread " << std::this_thread::get_id() << endl;
m.unlock();
}
};
int main()
{
TClass tc;
std::thread t[5];
for (int i=0; i<5; i++)
{
t[i] = std::thread(&TClass::run, &tc);
}
for (int i=0; i<5; i++)
{
t[i].join();
}
cout << "All threads terminated." << endl;
}
There are no guarantee about the values returned by std::this_thread::get_id(). You cannot assume that the value will begin from zero or will be sequential. That is unspecified.
I am making a hot potato game that deals with inheritance. I have a potato class, player class and umpire class.
In calling the toss function within my umpire class I KEEP getting this same error and I can't seem to figure out why: terminate called throwing an exceptionAbort trap: 6
I went through and found that within umpire::start(), the call to Players.at(randU)->toss(*m) in my for loop is making this error come up. But everything looks fine to me.
Can anyone help me out? Thanks!
Umpire.cc
#include <iostream>
#include <string>
#include "potato.h"
#include "player.h"
#include "umpire.h"
#include "PRNG.h"
using namespace std;
// Declare globally
int gamecount=1;
// GLOBAL VARIABLES
bool first=false; // False if set has not started
PRNG rplayer;
// UMPIRE CONSTRUCTOR-------------------------------------------------------------------
Umpire::Umpire( Player::PlayerList &players ){
Players=players;
cout<<"\tMashed POTATO will go off after ";
cout.flush();
m=new Mashed(Players.size()-1);
cout << " tosses" << endl;
cout.flush();
cout <<"\tFried POTATO will go off after 5 tosses" << endl;
cout.flush();
f=new Fried(5);}
// UMPIRE DESTRUCTOR-------------------------------------------------------------------
Umpire::~Umpire(){
delete m;
delete f;}
// UMPIRE START------------------------------------------------------------------------
void Umpire::start(){
int randU;
int gameCount=1; // Keeps track of sets
// Check if you are at the end of the list.
if(Players.size()==1){
// Who won?
cout << Players.at(0)->getId() << "wins the Match!" << endl;
cout.flush();}
else{
// Print output for sets----------------------------------------------------------------
// See which potato is being used in the set
if(gameCount%2!=0){
cout << "Set " << gameCount << "-\tUser (mashed) [";
cout.flush();}
else{
cout << "Set " << gameCount << "-\tUser (fried) [";
cout.flush();}
gameCount++; // increase gamecount
// Outputting players left in the set
for(unsigned int i=0;i<Players.size();i++){
cout<<Players.at(i)->getId();}
cout <<"]: ";
cout.flush();
//Start Tossing--------------------------------------------------------------------------
randU=rplayer(Players.size()-1);
// Output A(id) or R(id)
if (randU%2==0){
cout<<"A("<<randU<<"), ";
cout.flush();}
else{
cout<<"R("<<randU<<"), ";
cout.flush();}
if(first==false){
for(unsigned int i=0; i<Players.size(); i++){
if(Players.at(i)->getId()==Players.at(randU)->toss(*m)){
Players.erase(Players.begin()+i);
cout << "Eliminated: "<< i << endl;
cout.flush();}
}
first=true;
f->reset();
start();
}
else{
for(unsigned int i=0; i<Players.size(); i++){
if(Players.at(i)->getId()==Players.at(randU)->toss(*f)){
Players.erase(Players.begin()+i);
cout << "Eliminated: "<< i << endl;
cout.flush();}
}
first=false;
m->reset();
start();
}
}
}
Player.cc
#include <iostream>
#include "player.h"
#include "potato.h"
#include "PRNG.h"
using namespace std;
// GLOBAL DECLARATIONS--------------------------------------------------------------------
PRNG randP;
// PLAYER CONSTRUCTOR---------------------------------------------------------------------
Player::Player(unsigned int id, Player::PlayerList &players){
pid=id;
Players=players;
lrpFLAG=false;}
// getId() returns the player's id--------------------------------------------------------
unsigned int Player::getId(){
return pid;}
// RNPlayer Constructor-------------------------------------------------------------------
RNPlayer::RNPlayer( unsigned int id, Player::PlayerList &players ) : Player(id,players){}
// TOSS FUNCTION--------------------------------------------------------------------------
unsigned int RNPlayer::toss( Potato &potato ){
unsigned int randnum;
if(potato.countdown()){ return getId(); }
for(;;){
randnum=randP(Players.size()-1);
if (randnum%2==0){
cout<<"A("<<randnum<<"), ";
cout.flush();}
else{
cout<<"R("<<randnum<<"), ";
cout.flush();}
// If our randomly selected player is not the current player...
if(Players.at(randnum)->getId()!=getId()){
break;}
}
return Players.at(randnum)->toss(potato);
}
// LRPlayer Constructor-------------------------------------------------------------------
LRPlayer::LRPlayer( unsigned int id, Player::PlayerList &players ) : Player(id,players){}
// TOSS FUNCTION
unsigned int LRPlayer::toss( Potato &potato ){
unsigned int current; // current player
// Find who our current player is
for(unsigned int i=0; i<Players.size(); i++){
if(Players.at(i)->getId()==getId()){
current=i;
cout<<"A("<<i<<"), ";}
}
// if timer hasn't gone off yet...
if(potato.countdown()!=true){
// if this is the FIRST toss, we want to toss left
if(lrpFLAG==false){
if(current==0){
lrpFLAG=true;
(Players.at(Players.size()-1))->toss(potato);}
else{
lrpFLAG=true;
(Players.at(current-1))->toss(potato);}
}
else{
if(current==Players.size()-1){
lrpFLAG=false;
(Players.at(0))->toss(potato);}
else{
lrpFLAG=false;
(Players.at(current+1))->toss(potato);}
}
}
return (Players.at(current))->getId();
}
Main.cc
#include <iostream>
#include <vector>
#include <time.h>
#include "potato.h"
#include "player.h"
#include "umpire.h"
#include "PRNG.h"
using namespace std;
int main(int argc, char *argv[] ){
int p = 5;
int tmp;
unsigned int s;
PRNG prng1;
prng1.seed(1234567890);
// Parse the command line arguments
switch ( argc ) {
case 3:
tmp=atoi(argv[1]);
s=atoi(argv[2]);
prng1.seed(s);
if(tmp<2 || tmp>20){
cout << "Player must be between 2 and 20 inclusive" << endl;
return 0;}
else{
p = atoi(argv[1]);}
}
// Creating list of players.
Player::PlayerList players;
for(int i=0; i<p; i++){
if(i%2==0){
players.push_back(new LRPlayer(i,players));}
else{
players.push_back(new RNPlayer(i,players));}
}
//for (int i=0;i<players.size();i++){
// cout << "Player at " << i << " id: " << players.at(i)->getId() << endl;}
// How many players?----------------------------------------------------------------------
cout << p << " players in the match" << endl;
// Construct an UMPIRE--------------------------------------------------------------------
Umpire u(players);
// Start the game!------------------------------------------------------------------------
u.start();
}
Also note: PRNG.h is a class that generates a random number. so PRNG prng1; int rand=prng1(#) generates a random number between 0 and #.
ALSO:
The problem is occurring when i call my toss function. I'm not out of range because when i try to call Players.at(randU)->getID() i don't get any errors at all. Could it be that i can't reach the toss function for that player?I'm thinking it has to do with something im pointing to or memory. I first make a PlayerList players in my main.cc and push_back players alternating between the two. But each player also takes in a list. Maybe I'm running into errors involved with this?
Thanks! Any help is much appreciated :)
The exception thrown must be std::out_of_range. The random number you are generating must be higher than the number of elements in the vector.
randU must be re-generated after Players.erase() is called. Otherwise it will go out of range.
Or you should break the loop as:
for(unsigned int i=0; i<Players.size(); i++){
if(Players.at(i)->getId()==Players.at(randU)->toss(*f)){
Players.erase(Players.begin()+i);
cout << "Eliminated: "<< i << endl;
cout.flush();
break; // BREAK HERE as randU now can be greater than (Players.size() - 1)
}
}