Is there a functionality in c++ to run two or more methods\modules simultaneously...
I mean, Assume i want a program which keeps on printing "hello world" until the user presses ESC or any other key for that matter.. When that key is pressed the "hello world" printing should stop and it should display some other text\string.
What I'm basically telling is, can we accept data at the same time when a module to print something is running?
Yes this can be done in C++, this is called multithreading and the limiting factor is not the language but rather the system it is running on. For example in a Windows machine you would use CreateThread(...) to start a new 'thread' for the function that displays "Hello World" and another CreateThread(...) for the function that watches for the ESC key. So far this is pretty complex so I would have the ESC function toggle a global bool variable to false that the "Hello World" function tests for true.
There is a way to do that, yes. However, there isn't a standard C++ library to deal with keypresses. You'll need something else to deal with that... try PDCurses, maybe?
A while loop + a function that gets the state of a certain key would be how I would do it. Sorry, but multithreading is overkill. :)
Yeah sorry about that. I was show boating my new skill, sometimes I can't help but to brag. Albatross is right for something as simple as you described a keypress loop would be the best.
@quirkyusername and im abcd
I'm pretty this can't be done in simple loops (either while or do-while)
If i'm wrong pls provide an example...
and @Computergeek01
Should i learn windows programming to learn multi-threading?
and @Albatross
What is PDCurses?
i googled it but couldn't make much sense of it..
would be grateful if u could post a easy-to-understand link or something..
You don't have to learn WINDOWS programming to learn multithreading, many other operating systems are capable of doing this. The Micorsoft platform just happens to be the one I am most familiar with so that's the example I used.
Multithreading is as much of an OS ability as a programs so I doubt that you will be able to find a 3rd party library that allows you to do this.
EDIT: I just realized that the keypress detection function is listed under X Resources which implies no support on other platforms, so on second thought, maybe I would suggest using the Windows API.
You got me there. If I was sadistic enough to suggest the boost library to a beginner I was unaware of this functionality within it. I'll check it out though thanks.