If I comment out the sleep_for, the program churns out commas and dots without issues. But when I add the sleep_for, it hangs for a while before suddenly writing all of the commas and dots you would have expected to come evenly during the hang all in one go.
This program was reduced to a minimum working example from something that was much larger and more complex, but fortunately the issue remains.
(Compiled using apt-installed g++ 11.2.0 on Ubuntu 20.04.4 LTS)
#include <chrono>
#include <iostream>
#include <RingBuffer.h>
#include <thread>
void writeCharToBuffer(volatile bool& quit, char, std::chrono::milliseconds);
int main()
{
bool quit { false };
using namespace std::literals;
std::thread dotToBufferThread { writeCharToBuffer, std::ref(quit), '.', 100ms};
std::thread commaToBufferThread { writeCharToBuffer, std::ref(quit), ',', 200ms};
int quitKey;
std::cin >> quitKey;
quit = true;
dotToBufferThread.join();
commaToBufferThread.join();
}
void writeCharToBuffer(volatile bool& quit, char c, std::chrono::milliseconds d)
{
while (!quit)
{
std::cout << c;
std::this_thread::sleep_for(d);
}
}
Output to std::cout is buffered.
It might be that the output without the delay will fill up the buffer quick enough that you won't notice it.
If you want immediate output you need to explicitly flush it:
std::cout << c << std::flush;
Related
I'm new to C++ and am trying to have two threads run:
i) Thread that keeps looping until an atomic bool is flipped.
ii) A thread that polls for input from keyboard and flips the atomic bool.
I seem to be unable to get std::cin.get() to react to an input unless it is assigned its' own thread (like below). Why? Would it not then be set from the parent main thread?
#include <iostream>
#include <iomanip> // To set decimal places.
#include <thread> //std::thread
#include <atomic> //for atomic boolean shared between threads.
#include <math.h>
#define USE_MATH_DEFINES //For PI
std::atomic<bool> keepRunning(false); //set to false to avoid compiler optimising away.
void loop(){
int t = 1;
while(!keepRunning.load(std::memory_order_acquire)) //lower cost than directly polling atomic bool?
{
//Write sine wave output to console.
std::cout << std::setprecision(3) << sin(M_PI * 2 * t/100) << std::endl;
(t<101)? t++ : t = 1;
}
}
//This works, as opposed to stopping in main.
void countSafe(){
int j = 1;
while (j<1E7)
{
j++;
}
keepRunning.store(true, std::memory_order_release); //ends the loop thread.
}
int main(){
std::thread first (loop); //start the loop thread
std::thread second (countSafe); //start the countSafe thread. Without this it doesn't work.
//Why does polling for std::cin.get() here not work?
//std::cin.get(); //wait for key press. puts in buffer..?
//keepRunning.store(true, std::memory_order_release); //Set stop to true.
second.join(); //pause to join.
first.join(); //pause to join
return 0;
}
I'm not quite sure what your problem is, but use of cin.get() might be part of it. Let's simplify with this code:
#include <iostream>
using namespace std;
int main(int, char **) {
cout << "Type something: ";
cin.get();
cout << "Done.\n";
}
Try that code and run it. Then type a single character. Chances are that the code won't recognize it. And you can type all you want until you hit return.
This is complicated, but your program doesn't actually receive the characters until you hit return unless you play other games. Like I said, it's complicated.
It's possible behavior is different on Windows, but this is the behavior on Mac and Linux.
So is it "not working" because you tried typing a space but you really need to use Return?
I'm slowly moving from using Python to using C++ and I don't understand how to run any code. I'm using the g++ compiler, but I get no results from my functions.
// arrays example
#include <iostream>
using namespace std;
int foo [] = {16, 2, 77, 40, 12071};
int n, result=0;
int main ()
{
for ( n=0 ; n<5 ; ++n )
{
result += foo[n];
}
cout << result;
return 0;
}
If I run this example inside VSCode and specify that I want to use g++ compiler it comes back with: Terminal will be reused by tasks, press any key to close it.. If I compile it through cmd and run the task, a new cmd window flashes and nothing is happening.
I found the g++ doc which says how to compile with g++ and it shows the following example:
#include <stdio.h>
void main (){
printf("Hello World\n");
}
But I can't even run the compiler because it says
error: '::main' must return 'int'
void main(){
^
How can I print something in cmd or the ide terminal? I don't understand.
I believe you are using VSCode in a wrong way. You must know that it does not have integrated compiler by default but you need to compile source file in command line and run the executable:
$ g++ hello.cpp
$ ./a.out
Your first example runs with no problem. Check here
Your second example has an error because there is no void main() in C++. Instead, you need to have
int main() {
return 0;
}
UPDATE
If running the executable results in opening and closing the window you can fix that by using one of the following:
shortcut
#include <iostream>
using namespace std;
int main() {
system("pause");
return 0;
}
preferred
#include <iostream>
using namespace std;
int main() {
do {
cout << '\n' << "Press the Enter key to continue.";
} while (cin.get() != '\n');
return 0;
}
Why std::endl is not needed?
Some of the comments are suggesting that changing
cout << result;
to
cout << result << endl;
will fix the issue but, in this case, when the above line is the last line in the main function it really does not matter since program's exit flushes all the buffers currently in use (in this case std::cout).
I'm trying to write a kind of thread pool in C++. The code works fine in OSX, but under Linux I'm experiencing a strange behavior.
After a bit of debugging, I found the problem is due to a call to std::condition_variable::wait_until that I must be doing in a wrong way.
With the code below I expect the loop to be looped once every three seconds:
#include <mutex>
#include <chrono>
#include <iostream>
#include <memory>
#include <condition_variable>
#include <thread>
using namespace std;
typedef std::chrono::steady_clock my_clock;
typedef std::chrono::duration<float, std::ratio<1> > seconds_duration;
typedef std::chrono::time_point<my_clock, seconds_duration> timepoint;
timepoint my_begin = my_clock::now();
float timepointToFloat(timepoint time) {
return time.time_since_epoch().count() - my_begin.time_since_epoch().count();
}
void printNow(std::string mess) {
timepoint now = my_clock::now();
cout << timepointToFloat(now) << " " << mess << endl;;
};
void printNow(std::string mess, timepoint time ) {
timepoint now = my_clock::now();
cout << timepointToFloat(now) << " " << mess << " " << timepointToFloat(time) << endl;;
};
int main() {
mutex _global_mutex;
condition_variable _awake_global_execution;
auto check_predicate = [](){
cout << "predicate called" << endl;
return false;
};
while (true) {
{ // Expected to loop every three seconds
unique_lock<mutex> lock(_global_mutex);
timepoint planned_awake = my_clock::now() + seconds_duration(3);
printNow("wait until", planned_awake);
_awake_global_execution.wait_until(lock, planned_awake, check_predicate);
}
printNow("finish wait, looping");
}
return 0;
}
However, sometimes I get as output:
<X> wait until <X+3>
predicate called
(...hangs here for a long time)
(where X is a number), so it seems the timeout is not scheduled after three seconds. Sometimes instead I get:
<X> wait until <X+3>
predicate called
predicate called
<X> finish wait, looping
<X> wait until <X+3> (another loop)
predicate called
predicate called
<X> finish wait, looping
(...continue looping without waiting)
so it seems the timeout is scheduled after a small fraction of seconds. I think I'm messing up something with the timeout timepoint, but I cannot figure out what I'm doing wrong.
If it may be relevant, this code works fine in OSX, while in Linux (Ubuntu 16.04, gcc 5.4, compiled with "g++ main.cc -std=c++11 -pthread") I'm experiencing the strange behavior.
How can I get it work?
Try to cast your timeout to your clock's duration:
auto planned_awake = my_clock::now() +
std::chrono::duration_cast<my_clock::duration>(seconds_duration(3));
I am using MSVC with Visual Studio 2013. This is the code I am compiling:
#include <iostream>
using namespace std;
void crash()
{
cout << "crash?" << endl;
system("PAUSE");
}
int main(int argc, char *argv[])
{
atexit(crash);
//while(true);
return 0;
}
The way it is right now - it works like a charm. I start the program, it goes inside the crash function, pauses, I press a key and it closes normally. All cool. However, if I uncomment the while loop and use the X button on the console to close it, I get a crash inside the endl function. I was able to determine that the crash is caused by _Ostr.widen()
This is the implementation of the endl function, provided by MSVC:
template<class _Elem,
class _Traits> inline
basic_ostream<_Elem, _Traits>&
__CLRCALL_OR_CDECL endl(basic_ostream<_Elem, _Traits>& _Ostr)
{ // insert newline and flush stream
_Ostr.put(_Ostr.widen('\n'));
_Ostr.flush();
return (_Ostr);
}
Using Ctrl+C to terminate the program causes the same effect. How can I fix this?
Seems like my suspicions turned out to be true. I modified the code like so:
#include <iostream>
using namespace std;
#include <Windows.h>
void crash()
{
printf("%i\n", GetCurrentThreadId());
system("PAUSE");
}
int main()
{
printf("%i\n", GetCurrentThreadId());
atexit(crash);
//while(true);
return 0;
}
When the program exists normally both printf()s display identical thread IDs, however when I press Ctrl+C or the X button the thread IDs are different, which explains the crash and makes a lot of sense when you think about it. Thus, here is a small example how this issue can be tackled:
#include <iostream>
#include <conio.h>
using namespace std;
#include <Windows.h>
volatile bool wantClose = false;
void OnExit()
{
cout << GetCurrentThreadId() << endl;
system("PAUSE");
}
BOOL WINAPI OnConsoleClose(DWORD dwCtrlType)
{
wantClose = true; // set a flag that the console wants us to close
ExitThread(0); // kill this thread immediately so it doesn't make the console stuck
return FALSE;
}
int main()
{
cout << GetCurrentThreadId() << endl;
SetConsoleCtrlHandler(OnConsoleClose, TRUE); // handle close requests from the console
atexit(OnExit);
while(!wantClose); // at some point in our code we will have to check whether the console wants us to close down
return 0;
}
Please note: The usage of system("PAUSE") and busy waiting are only for the sake of keeping the example simple. I do not advise their usage in real code.
How does one "pause" a program in C++ on Win 32, and what libraries must be included?
#include <windows.h>
Sleep(number of milliseconds);
Or if you want to pause your program while waiting for another program, use WaitForSingleObject.
In C++11, you can do this with standard library facilities:
#include <chrono>
#include <thread>
std::this_thread::sleep_for(std::chrono::milliseconds(x));
If you are using boost, you can use the thread::sleep function:
#include <boost/thread/thread.hpp>
boost::system_time time = boost::get_system_time();
time += boost::posix_time::seconds(1);
boost::thread::sleep(time);
Otherwise, you are going to have to use the win32 api:
#include <windows.h>
Sleep(1000);
And, apparently, C++0x includes this:
#include <thread>
std::this_thread::sleep_for(chrono::seconds(1));
Please note that the code above was tested on Code::Blocks 12.11 and Visual Studio 2012
on Windows 7.
For forcing your programme stop or wait, you have several options :
sleep(unsigned int)
The value has to be a positive integer in millisecond.
That means that if you want your programme wait for 2 second, enter 2000.
Here's an example :
#include <iostream> //for using cout
#include <stdlib.h> //for using the function sleep
using namespace std; //for using cout
int main(void)
{
cout << "test" << endl;
sleep(5000); //make the programme waiting for 5 secondes
cout << "test" << endl;
sleep(2000); // wait for 2 secondes before closing
return 0;
}
If you wait too long, that probably means the parameter is in second. So change it like that :
sleep(5);
For those who get error message or problem using sleep try to replace it by _sleep or Sleep especially on Code::Bloks.
And if you still getting probleme, try to add of one this library on the biggining of the code.
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <dos.h>
#include <windows.h>
system("PAUSE")
A simple "Hello world" programme on windows console application would probably close before you can see anything. That the case where you can use system("Pause").
#include <iostream>
using namespace std;
int main(void)
{
cout << "Hello world!" << endl;
system("PAUSE");
return 0;
}
If you get the message "error: 'system' was not declared in this scope" just add
the following line at the biggining of the code :
#include <cstdlib>
cin.ignore()
The same result can be reached by using cin.ignore() :
#include <iostream>
using namespace std;
int main(void)
{
cout << "Hello world!" << endl;
cin.ignore();
return 0;
}
cin.get()
example :
#include <iostream>
using namespace std;
int main(void)
{
cout << "Hello world!" << endl;
cin.get();
return 0;
}
getch()
Just don't forget to add the library conio.h :
#include <iostream>
#include <conio.h> //for using the function getch()
using namespace std;
int main(void)
{
cout << "Hello world!" << endl;
getch();
return 0;
}
You can have message telling you to use _getch() insted of getch
If you wish for the program to stay responsive while "paused", you need to use a timer event.
It depends on what type of program you are writing.
A console app can just call Sleep. A GUI app probably does not want to do this, as all the menus and widgets will go insensitive, and the app won't redraw itself during this period. Instead you need to do something like set yourself up a timer with a callback when it expires.
Dont use a sleep function in your GUI if it is not provided by the framework you are working with. This could create referencing problems to data (specially in a thread that is not the main thread). This could freeze you GUI. Its not just a question of sleeping for a short time , use waitmutexes if you need to do that.