Okay, for a program I am working on I do not require threads, but they would make my program extremely useful.
At the moment, all I really know about them is that they require a class and that they allow the execution of multiple things at once.
So can someone try to explain to me how exactly they work? The help would be greatly appreciated.
I am planning on making this program for Windows, as it is in Console.
Post has been edited 1 time(s), last time on May 6 2008, 10:12 pm by Falkoner.
None.
Implementation depends on OS and dev environment more than it does on language used.
more generally, you usually spawn a thread when you have to wait on something(a keystroke for example) or you want to separate your interface so it remains responsive, but because threads sometimes have to communicate with each other, they act on common variables, so you have to carefully manage access to those variables so the threads don't screw each other up.
http://en.wikipedia.org/wiki/Lock_(computer_science)
None.
Threads are like different processes but they share memory. So they can be executed at the same time and interact with eachother. However locks have to be implement to avoid one thread reading data that the other thread corrupted because it has not finished editing it yet...
EDIT: Btw, they don't need a class, just a main-like function which works as the thread entry point.
?????
Ah, I see, so it does not require classes, that's good. Can anyone tell me like, the basic code in order to make a thread?
when you have to wait on something(a keystroke for example)
I am currently using something exactly like that, but I use kbhit to detect whether a key is currently being hit or not, and so I can continue to program, but it would be much easier to be able to run multiple processes at once.
None.
Search it on MSDN or something. It takes one API call to create a thread. CreateThread? I never remember which one...
?????
I think there may be a C library function available for creating a thread.
None.
I think this provides a good list of stuff:
http://msdn.microsoft.com/en-us/library/ms684847%28VS.85%29.aspx But they do not explain things well for a beginner, so can someone shine a little light in? Perhaps give me some basic flexible code examples?
If I can find an easy way to create and use a thread, I would love to use it.
None.
I think there may be a C library function available for creating a thread.
There is no standard one. You can usually use
_beginthread though
None.
Ugh. It looks almost simple, but I can't exactly see how it works from that page, can someone make a sample program that is like, super simple?
None.
Starts 3 threads that find prime numbers, and also counts in the main thread:
#include <stdio.h>
int threadfunc(void* countfrom) {
int i,k;
for (i=(int)countfrom;;i+=2) {
for (k=3; i%k != 0 && k<i/2;k+=2);
if (i%k != 0) {
printf("%d prime\n",i);
}
}
return 0;
}
int main(int argc, char** argv) {
printf("Go1!");
_beginthread(threadfunc,1024,(void*)3);
//Note: a stack size of 1024 is INCREDIBLY tiny, but since the thread function
// calls almost nothing and only has 2 local vars, it's a good idea here.
printf("Go2!");
_beginthread(threadfunc,1024,(void*)997);
printf("Go3!");
_beginthread(threadfunc,1024,(void*)65539);
threadfunc((void*)6666667); //this thread (main one) shouldn't exit either
return 0;
}
Post has been edited 1 time(s), last time on May 16 2008, 2:12 am by Doodle77.
None.
http://en.wikipedia.org/wiki/Process.hI don't think that'll work without including process.h.
Also you forgot the & when calling _beginthread:
_beginthread(&threadfunc,1024,(void*)997);
?????
No, threadfunc and &threadfunc are equivalent.
None.
Threading is a topic you learn in your 3rd or 4th year of a computer science degree
Threading is completely OS-specific; that is the code to do it is completely different on Windows, Mac, etc.
Here's some general resources on the topic:
http://paulbridger.net/multithreading_tutorialIf you aren't careful with locks and synchronization, things can really blow up. I highly recommend using an RAII scheme for locks as shown here:
http://www.parkscomputing.com/code/threading.aspx (this
does require classes).
Post has been edited 2 time(s), last time on May 21 2008, 5:14 pm by Heimdal.
None.