Can we do this in c++

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?

Thanks in Advance. :)
hmmm i dont think something like that can be done but u can try do -while loop which gives similar functionality


study the loops here

http://www.cplusplus.com/doc/tutorial/control/
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.
Last edited on
yeah don't look into multi-threading for something as simple as this it's not at all necessary. A loop will suffice
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. :)

-Albatross
Last edited on
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..

thanks everyone..
and @Albatross
how would u do it with while loop..
can u please provide an example?
Here's a PDCurses documentation link: http://pdcurses.sourceforge.net/doc/PDCurses.txt

Search around and see if you can find the function that detects keypresses. :)

EDIT:
1
2
while (keypressdetector() != code_for_a_certain_key)
    std::cout << "Hey World!\n";


Note that you'll have to make at least two substitutions here. :)

-Albatross
Last edited on
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.
Computergeek01 wrote:
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.


Are you kidding me?!? :P
http://www.boost.org/doc/libs/1_45_0/doc/html/thread.html

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.

-Albatross
Last edited on
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.
You're right though; the Boost library is tricky, and I would not recommend it to a beginner. Still, though, it's a good thing to know!

-Albatross
closed account (3pj6b7Xj)
Well they better get ready for it, becaust its coming with C++0x soon...
Topic archived. No new replies allowed.