looping in background while running other statements c++ - c++

I want to make a program which displays current time that ticks in background then is it possible to initialize cin input processes while it is ticking?

just for fun:
#include <iostream>
#include <chrono>
#include <thread>
#include <atomic>
#include <mutex>
#include <ctime>
std::mutex more_lock;
std::string more_output;
void set_more_output(const std::string& more)
{
std::lock_guard<std::mutex> lock(more_lock);
more_output = more;
}
std::string get_more_output()
{
std::lock_guard<std::mutex> lock(more_lock);
return more_output;
}
std::atomic_bool running {true};
std::thread start_timer_runner()
{
return std::thread([&]{
while(running) {
using namespace std::chrono;
using namespace std::chrono_literals;
auto now = system_clock::to_time_t(system_clock::now());
auto now_str = std::string(std::ctime(&now));
now_str.pop_back(); // ctime adds new line
std::cout << "\r" << now_str << " | " << get_more_output() << std::flush;
std::this_thread::sleep_for(1s);
}
});
}
void parse_decimals()
{
int x;
set_more_output("please enter a decimal: ");
while(std::cin >> x)
{
set_more_output("thanks! you entered " + std::to_string(x) + ", please enter a new decimal: ");
}
}
int main()
{
auto timer = start_timer_runner();
parse_decimals();
running = false;
timer.join();
std::cout << std::endl << "bye bye!" << std::endl;
}

Related

Why does the output file come up empty

I'm working on a program that is supposed to make use of async and futures but i have come across a problem. When an output file is created nothing is in the file. It is just a blank file. I'm not sure if the issue s with reading the original file or write onto a new one I'm also usure if I have properly utilized async so any corrections would be a appreciated.
#include <fstream>
#include <string>
#include <iostream>
#include <string>
#include <chrono>
#include <thread>
#include <future>
#include <vector>
#include <stdexcept>
#include <utility>
#include <sstream>
#include <stdlib.h>
#include <cstdlib>
#include <ctime>
using namespace std;
int prnumber;
static bool bCheckFinished = false;
string guess;
bool process(std::string sFileIn, std::string sFileOut) {
ifstream in(sFileIn);
vector<vector<string>> vsFields;
ofstream outPutFile;
outPutFile.open(sFileOut, std::ofstream::out | std::ofstream::trunc);
if (in) {
string sLine;
while (getline(in, sLine)) {
stringstream sep(sLine);
string sField;
vsFields.push_back(vector<string>());
while (getline(sep, sField, ',')) {
vsFields.front().push_back(sField);
}
}
}
if (outPutFile.is_open()) {
for (auto row : vsFields) {
for (auto field : row) {
outPutFile << field << "," << std::endl;
}
}
}
std::cout << "\n File Successfully Read!" << std::endl;
return true;
}
bool prime(int prnumber) {
std::cout << "Please enter a number" << std::endl;
std::cin >> prnumber;
std::cout << "Processing" << std::endl;
for (int i = 2; i < prnumber; ++i) if (prnumber%i == 0) return false;
return true;
}
int main() {
auto start = std::chrono::high_resolution_clock::now();
std::future<bool> fileProcA = std::async(process, "unigram_freq.cvs", "Word_list.txt");
std::future<bool> fut = std::async(prime, prnumber);
bool pr = fut.get();
bool fileProc = false;
while (!fileProc) {
fileProc = fileProcA.get();
}
auto end = std::chrono::high_resolution_clock::now();
if (pr) std::cout << "The number is prime!" << endl;
else std::cout << "It is not prime";
double elapsed = (std::chrono::duration<double, std::milli>(end - start).count());
std::cout << "The time elapsed is " << elapsed << "ms" << std::endl;
return 0;
}

wait_until timeout causing only one thread to terminate, unable to catch all thread timeouts

I've made a simple thread-safe Buffer implementation, creating 10 threads to work on the buffer queue to push and pop randomly some numbers. My implementation should let threads that are waiting to pop to wait only for 3 seconds and then terminate. When that occurs I print a timeout message.
The problem is that only one timeout message is printed, the main will then join all threads and return. Why?
Here is the code, main.cpp
#include <thread>
#include <vector>
#include <iostream>
#include <sstream>
#include "Buffer.h"
int main() {
std::vector<std::thread> workers;
Buffer<std::string> buffer(3);
srandom(time(NULL));
for (int i = 0; i < 10; i++) {
workers.emplace_back([&buffer]{
long num = random();
if(num%2==0) {
std::stringstream msg;
msg << std::this_thread::get_id() << " pushing " << num << std::endl;
std::cout << msg.str();
buffer.push(std::to_string(num));
} else {
std::stringstream msg1;
msg1 << std::this_thread::get_id() << " waiting to pop" << std::endl;
std::cout << msg1.str();
std::string popped_string = buffer.pop();
std::stringstream msg2;
msg2 << std::this_thread::get_id() << " popped " << popped_string << std::endl;
std::cout << msg2.str();
}
});
}
for (auto &w: workers) {
if (w.joinable()) w.join();
}
return 0;
}
Buffer.h
#ifndef PDS_CPP_BUFFER_H
#define PDS_CPP_BUFFER_H
#include <queue>
#include <mutex>
#include <condition_variable>
template <class T>
class Buffer {
private:
std::queue<T> queue;
std::mutex mutex;
std::condition_variable cv;
std::chrono::seconds sec;
public:
Buffer(int time) : sec(time), queue() {};
void push(T object) {
std::lock_guard lockGuard(mutex);
this->queue.push(object);
this->cv.notify_one();
}
T pop() {
std::unique_lock uniqueLock(mutex);
// this->cv.wait(uniqueLock, [this]{ return !this->queue.empty(); });
if(this->cv.wait_for(uniqueLock, this->sec, [this]{ return !this->queue.empty(); })) {
} else {
std::stringstream msg;
msg << std::this_thread::get_id() << " timeout" << std::endl;
std::cout << msg.str();
}
T object = this->queue.front();
this->queue.pop();
uniqueLock.unlock();
return object;
}
};
#endif //PDS_CPP_BUFFER_H

how to use argument inside of thread? (C++)

i can't use delay argument inside of thread t
void HelloWorldDelay(int Delay)
{
cout << "Hello World";
atomic<bool> abort(false);
thread t([&abort]() {
Sleep(Delay);
abort = true;
});
t.join();
cout << Delay << "Ms ";
}
how to use it inside of thread t?
Sleep(Delay)
void HelloWorldDelay(int Delay) {
std::cout << "Hello World";
std::atomic<bool> abort(false);
std::thread t([&]() {
std::this_thread::sleep_for(std::chrono::seconds(Delay));
abort = true;
});
t.join();
std::cout << Delay << "Ms ";
}
will do the capturing
I think you should invoke like this :
#include <iostream>
#include <fstream>
#include <thread>
#include <atomic>
using namespace std;
void HelloWorldDelay(int Delay)
{
cout << "Hello World";
atomic<bool> abort(false);
thread t([&abort](int delay) {
//sleep(Delay);
std::this_thread::sleep_for(std::chrono::milliseconds(delay));
abort = true;
}, std::ref(Delay));
t.join();
cout << Delay << "Ms ";
}
int main()
{
HelloWorldDelay(3);
std::system("pause");
return 0;
}

std::async uses same thread and my code does not achieve parallelism.

I am using C++11 on Mac OS Xcode 4.3.2
std::async uses same thread and my code does not achieve parallelism. In sample code below I want to create 10 new threads. In each thread I want to calculate square root of input variable and set the result in promise. in main function I want to display the results calculated from threads. I am calling std::async with policy launch::async, So I expect it to create a new thread(10 times).
#include <mutex>
#include <future>
#include <thread>
#include <vector>
#include <cmath>
#include <iostream>
using namespace std;
mutex iomutex;
void foo(int i, promise<double> &&prms)
{
this_thread::sleep_for(chrono::seconds(2));
prms.set_value(sqrt(i));
{
lock_guard<mutex> lg(iomutex);
cout << endl << "thread index=> " << i << ", id=> "<< this_thread::get_id();
}
}
int main()
{
{
lock_guard<mutex> lg(iomutex);
cout << endl << "main thread id=>"<< this_thread::get_id();
}
vector<future<double>> futureVec;
vector<promise<double>> prmsVec;
for (int i = 0; i < 10; ++i) {
promise<double> prms;
future<double> ftr = prms.get_future();
futureVec.push_back(move(ftr));
prmsVec.push_back(move(prms));
async(launch::async, foo, i, move(prmsVec[i]));
}
for (auto iter = futureVec.begin(); iter != futureVec.end(); ++iter) {
cout << endl << iter->get();
}
cout << endl << "done";
return 0;
}
However if I use std::thread, then I can achieve parallelism.
#include <mutex>
#include <future>
#include <thread>
#include <vector>
#include <cmath>
#include <iostream>
using namespace std;
mutex iomutex;
void foo(int i, promise<double> &&prms)
{
this_thread::sleep_for(chrono::seconds(2));
prms.set_value(sqrt(i));
{
lock_guard<mutex> lg(iomutex);
cout << endl << "thread index=> " << i << ", id=> "<< this_thread::get_id();
}
}
int main()
{
{
lock_guard<mutex> lg(iomutex);
cout << endl << "main thread id=>"<< this_thread::get_id();
}
vector<future<double>> futureVec;
vector<promise<double>> prmsVec;
vector<thread> thrdVec;
for (int i = 0; i < 10; ++i) {
promise<double> prms;
future<double> ftr = prms.get_future();
futureVec.push_back(move(ftr));
prmsVec.push_back(move(prms));
thread th(foo, i, move(prmsVec[i]));
thrdVec.push_back(move(th));
}
for (auto iter = futureVec.begin(); iter != futureVec.end(); ++iter) {
cout << endl << iter->get();
}
for (int i = 0; i < 10; ++i) {
thrdVec[i].join();
}
cout << endl << "done";
return 0;
}
async(launch::async, foo, i, move(prmsVec[i]));
This line returns a future but because you do not assign it to anything the future's destructor runs at the end of the statement, which blocks and waits for the result by calling std::future::wait()
Why are you manually calling std::async with a promise, when it returns a future anyway? The point of async is that you don't need to manually use a promise, that's done internally for you.
Rewrite your foo() to return double then call it with async
#include <mutex>
#include <future>
#include <thread>
#include <vector>
#include <cmath>
#include <iostream>
using namespace std;
mutex iomutex;
double foo(int i)
{
this_thread::sleep_for(chrono::seconds(2));
lock_guard<mutex> lg(iomutex);
cout << "\nthread index=> " << i << ", id=> "<< this_thread::get_id();
return sqrt(i);
}
int main()
{
cout << "\nmain thread id=>" << this_thread::get_id();
vector<future<double>> futureVec;
for (int i = 0; i < 10; ++i)
futureVec.push_back(async(launch::async, foo, i));
for (auto& fut : futureVec)
{
auto x = fut.get();
lock_guard<mutex> lg(iomutex);
cout << endl << x;
}
cout << "\ndone\n";
}

C++ background timer

#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <time.h>
using namespace std;
using namespace System;
void wait ( int seconds )
{
clock_t endwait;
endwait = clock() + seconds * CLOCKS_PER_SEC ;
while (clock() < endwait) {}
}
void timer()
{
int n;
printf ("Start\n");
for (n=10; n>0; n--) // n = time
{
cout << n << endl;
wait (1); // interval (in seconds).
}
printf ("DONE.\n");
system("PAUSE");
}
int main ()
{
timer();
cout << "test" << endl; // run rest of code here.}
return 0;
}
I'm trying to create a timer in C++ which would run in the background. So basically if you'd look at the 'main block' I want to run the timer (which is going to count down to 0) and at the same time run the next code, which in this case is 'test'.
As it is now the next line of code won't be run until the timer has finished. How do I make the timer run in the background?
Thanks for your help in advance!
C++11. Should work with VS11 beta.
#include <chrono>
#include <iostream>
#include <future>
void timer() {
std::cout << "Start\n";
for(int i=0;i<10;++i)
{
std::cout << (10-i) << '\n';
std::this_thread::sleep_for(std::chrono::seconds(1));
}
std::cout << "DONE\n";
}
int main ()
{
auto future = std::async(timer);
std::cout << "test\n";
}
If the operation performed in timer() takes significant time you can get better accuracy like this:
void timer() {
std::cout << "Start\n";
auto start = std::chrono::high_resolution_clock::now();
for(int i=0;i<10;++i)
{
std::cout << (10-i) << '\n';
std::this_thread::sleep_until(start + (i+1)*std::chrono::seconds(1));
}
std::cout << "DONE\n";
}