how do i perform 2 actions at same time in c++? c++ only seems to run codes one by one sequentially.
Printable View
how do i perform 2 actions at same time in c++? c++ only seems to run codes one by one sequentially.
You need to look into multithreading. What OS/compiler are you using?
multi-threading or multi-process solutions. Its OS and sometimes compiler specific so we'll need more info before helping you.
Dang foiled again by XSquared.
Geez. Thantos. Work on your typing speed. :p
The fingers are doing their thing just fine. Its the brain telling them what to type is the slow one today.
o/s = operating system im using? im using windows xp and dev c++, the beta one
Thantos: Just today? :p
Chobo: Here is an example of threading.
does this require knowledge of window programming?
Nope.
oooooooo this is interesting stuff but a bit advanced for me >.< damn programming hard...
Why do you think that your program needs multithreading?
@XSquared: Mainly today ;)
@Chobo: Like all things learning the basics of programming is the hardest part. Multithreading and multiprocess solutions are not a giant step in difficultly once you have those skills. Be patient and you'll get it :)
If you are a novice in C++, I doubt the program you're making would benifit from threading. There are cases where threads can lower the performance of the program, and you can create serious issues if you do not have experiance with them. Just learn as much as you can first, then decide to use them later.
ok -.- i just wanted to add special feature to my program
this program just basically counts down from 20:00 to 0:00. but wat if i want the program to accept an input from the user without interupting the count down? if i use cin, it would stop the count down until the input has been received.Code:#include <windows.h>
#include <iostream>
#include <conio.h>
using namespace std;
void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
int main()
{
int count;
int i, ii;
gotoxy(3,0);
cout << ":";
for (ii = 20; ii >= 0; ii--) {
if (ii >= 10) {
gotoxy(1,0);
cout << ii;
}
else {
gotoxy(1,0);
cout << " ";
gotoxy(2,0);
cout << ii;
}
for (i = 60; i >= 0; i--) {
if (i >= 10) {
gotoxy(4,0);
cout << i;
Sleep(50);
}
else {
gotoxy(4,0);
cout << "0";
gotoxy(5,0);
cout << i;
Sleep(50);
}
}
}
getch();
return 0;
}
lookup kbhit(), it will tell you when the user hits a key and using getche() you can read it in one by one.