is there any way to have 2 loops running at the same time?
thanks
Printable View
is there any way to have 2 loops running at the same time?
thanks
Not unless you have multiple processors, but you can fake it a number of ways.
Can you not do it with Multi-Threads in C++ (I assume so as I know that you can with C).
>Can you not do it with Multi-Threads in C++
Yes, that would be one of the ways to fake it.
Cool, so lookup Threads and multithreading DeepFyre. This looks good:
http://www.powerbasic.com/support/te...threading1.asp
dt
It doesnt tell you how to do multi threading, is it somewhere else on the site?
Also, is multi-threading like hyper threading?
I've no idea, just used Google and that looked pretty good. Does anyone else have any more light on the matter?
dt
Hyper threading isnt a programming term, it's a hardware term. Multi-threading means that you have two different threads running concurrently, where the processer switches off between them very quickly.Quote:
Originally Posted by DeepFyre
On windows, take a look at the CreateThread() function.
Where can I find the CreateThread() function?
I'll google it but i dont know if i will find it.
MSDN docs for CreateThread()
http://msdn.microsoft.com/library/de...eatethread.asp
Yea ... I saw that but didnt quite undertand it
But thanks anyway guys, I'll try to figure out the rest on my own sometime.
>>Also, is multi-threading like hyper threading?
hyper threading is Intel's technology that, when you have several threads running side by side, makes them perform better (in a nutshell anyway).
>>didnt quite undertand it
Don't worry about it, CreateThread() is Windows stuff, which you probably don't know yet.
I've heard of a function called _beginthread(), and although I've never used it and have no idea how portable it is or whatnot, it seems much more straightforward to use than CreateThread(). You might want to look that up, if you're still wanting to use multiple threads.
On the other hand, if you want 2 loops running 'at the same time', you can often merge the two loops so that one loop somehow does the job for both, and thus you won't need them running 'at the same time'.
Hope this helps! :)
Thanks Hunter, I'll look that up.
Well i found found this:
...The problem is that I get these errors:Code:_beginthreadex(void);
do{
time();
}while(x != 1);
_endthreadex(void);
syntax error before `)' token (on that first line)
syntax error before `)' token (on that last line)
I dont understand what the problem is and these are the only two errors i get.
(Am I doing it right?)
No, beginthread() and beginthreadex() work by passing a function pointer to the function.
The function documentation can be found here:
http://msdn.microsoft.com/library/de...inthreadex.asp
Judging from the argument, the definition of the function you need should look like this:
Note that your program will continue to run as long as the thread is still running; _endthread() does not terminate the thread from the main(), but can be used within the thread function (myfunction()) to quit the thread, although it gets called automatically anyways when the function returns. So you need to find some way of ensuring that the thread ends when the program does.Code:#include <process.h>
void myfunction(void* parameter)
{
int param = *(int*)parameter; //or whatever type of parameter you wanted to pass
//put your loop in here
}
int main()
{
int parameterToPass = 10;
_beginthread(myfunction, 0, (void*)¶meterToPass);
//Now your thread is running the loop
//Do whatever you want to do in main() while the loop is running
return 0;
}
You also have to make sure that the thread doesn't access any data that main() accesses, to prevent memory corruption. That's what the windows synchronization objects are for (mutex, critical section, etc.).