Greatest C++ Doubt
Hi there again,
I have the greatest doubt.. I doubt any one can solve it.. Who ever solves this, i agree is the greatest programmer..
How do you make a part of the program to kepp running when the program is waiting for an input from the user....
That's it sounds simple, dosent it.. Try
Using threads. Run your background processing in one or more worker thread(s) with your UI in another.
How, depends to a great deal on the OS. Most *NIX systems use the POSIX threading model, (commonly called pthreads), Windows has it's own model, but is actually very similar.
>>> That's it sounds simple, dosent it.. Try
I have. I routinely multithread my programs.
Incidently, threading is one way of doing it, probably the easiest. There are other asynchronous techniques.
Hey! I knew that to! Means i'm a great programmer now!
As Adrian says, how threads are implemented differs from OS to OS. This is why you should take great caution when using treads in your "platform independant project". Threads will in a such project be a portion of the code you must rewrite when switching platform. Of cause there is always a solotion to this to!
Ok i use Windows Platform and use the Turbo C++ 3.0 comiler. So how do i do it here..... Please help me...
Include the windows.h header and look into the functions related to CreateThread().
You should update your compiler to a decent one too. Borland has released its command line compiler for free. not perfect but miles better than turbo c++.
I can't comment on a compiler that old, but certainly with VC and the more recent Borland compilers, CreateThread() can cause problems. Both Borland and MS provide a function _beginthread() which is a better deal. If you need more control, there is _beginthreadex() from MS or _beginthreadNT() from Borland. Remember however, that the more sophisticated version do not release the thread handle, you must do that yourself. If you don't need the extras that they give, use _beginthread().
To do it, use a macro at the top and create a generic name that is defined to windows style if run on windows. Else, have it defined to use the Linux version if it detects it's on a Linux machine.
I remember adrian saying that before so I did a little searching.
This comes from the MS platform SDK
A thread that uses functions from the C run-time libraries should use the _beginthread and _endthread C run-time functions for thread management rather than CreateThread and ExitThread. Failure to do so results in small memory leaks when ExitThread is called.
Perhaps useful info.
I use borland... In this situation how might I go about making a thread?
OK I will admit that multi-threading is the best and most obvious solution but what about..... The third best solution (it certainly cannot be the next best) for someone not ready for multi-threading just yet.
I have not tested the code but with some minor adjustments it should work....
I realize that this cannot replace multi-threading but at least it can allow processes to run while until the user enters a key.
int i = 0;
while( 1 )
while( !kbhit() )
// do this or the other.......
int chVal = getch();
szEntry[i++] = (char)chVal;
szEntry[i] = '\0';
if( strcmp(szEntry, "desired input") == 0)
i = 0;
else if( strcmp(szEntry, "quit") == 0)
vasanth....we already went over this!
check this -->
I AM THE GREATEST C++ PROGRAMMER. BOW DOWN BEFORE ME...
We went over this i agree. Byt that dint answer my question. But threads seems to be my answer.. But there is no _beginthread in my compiler.. And the bad thing about Borland compiler is that it does not have an ide environment.. Well I need more help
Borland does support beginthread(), and their version of the higher control function is called beginthreadNT(). In MS these functions are declared in the process.h header and you have to link with the multithreaded library.
I think one of my books has a Borland example, (I use MS so am not the best help source here!!!), if I find it, I'll contact you.