Is there a possibility to use cin parallel to cout? - c++

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.

Related

Use << operator with own function to "override" cout

I know that there is a lot of topics about overloading << operator, but it seems to always be used in class in order to make it support << operator. Hope I would not be duplicate
What I want to do is (I think) quite different (and probably simpler).
I have a console app with 2 threads, both writing on console. I want to avoid them from smashing to each other in the console, so I use a mutex to prevent a thread from outputing when the other one is, so i've created the function :
void print(string s){
globals::console_mtx.lock();
cout << s << endl;
globals::console_mtx.unlock();
}
I would like it to be usable like this, regardless of the data type :
int i=5;
print << "Some text" << i << endl;
Is << overloading what I need ? what would be the simplest way to achieve it ?
Thanks
Is this what you're thinking about?:
struct print_t {};
static const print_t print = print_t();
print_t& operator<<(print_t& p, const std::string& s) {
globals::console_mtx.lock();
std::cout << s << endl;
globals::console_mtx.unlock();
return p;
}
NB. there are about a dozen ways to make it better, like having print as a function with unique type, etc. to make sure you don't run into static (de)initialization order fiasco (avoid the above in static deinitialization if possible). You might use the definition of cout in std as a cheat sheet.
The problem with just overloading << is that:
print << "Some text" << i << endl;
Actually calls print.operator<<() separately for "Some text", i, and endl (also you need to make sure to have overloads for int and endl). If you lock and unlock for each call, then another thread could get the lock before you are done with all 3 calls. If for instance thread 2 had
print << "Other text" << i+1 << endl;
Then you might end up with:
"Some TextOther Text65\n\n"
The simplest way to deal with it is to output to a stringstream first, and then use a normal print function to print the entire thing protected by a locked mutex:
stringstream ss;
ss <<"Some text" << i << endl;
print(ss.str());
If you insist on using streams directly, then streams handle this kind of thing by only actually writing when it receives an instruction to flush. endl automatically flushes the stream, which is one of the ways it differs from "\n".
You then implement a custom std::stringbuf that locks the mutex and outputs to console on sync(), then you construct an ostream for each thread to use as its personal printing output stream.
Example code:
#include <iostream>
#include <sstream>
#include <mutex>
#include <thread>
#include <string>
// Increase risk of race condition if one can be triggered.
char slow_get_ch(char ch)
{
for (unsigned int i = 0; i < 10000; ++i)
{
for (unsigned int j = 0; j < 10000; ++j)
{
}
}
return ch;
}
class print_buf : public std::stringbuf
{
std::mutex& mtx_;
public:
print_buf(std::mutex& mtx)
:
mtx_(mtx)
{
}
protected:
int sync() final
{
std::unique_lock<std::mutex> lck(mtx_);
std::string val = this->str();
std::cout << val;
this->str("");
return 0;
}
};
void print_worker(std::ostream* print_stream_ptr,char ch)
{
std::ostream& print = *print_stream_ptr;
// Print 5 lines of 20 times ch.
for (unsigned int i = 0; i < 5; ++i) {
for (unsigned int j = 0; j < 20; ++j) {
print << slow_get_ch(ch);
}
print << std::endl;
}
}
int main()
{
std::mutex print_mutex;
print_buf buf1(print_mutex);
print_buf buf2(print_mutex);
print_buf buf3(print_mutex);
std::ostream p1(&buf1);
std::ostream p2(&buf2);
std::ostream p3(&buf3);
std::thread t1(print_worker, &p1, 'a');
std::thread t2(print_worker, &p2, 'b');
std::thread t3(print_worker, &p3, 'c');
t1.join();
t2.join();
t3.join();
return 0;
}

How can I keep separate variable copy for same class object's member function?

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

C++ std::this_thread::get_id() passing to cout

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.

Outputting progress of multiple threads to console

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();
}

Error: terminate called throwing an exceptionAbort trap: 6

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)
}
}