Staredit Newtork
Community
StarCraft
Games
Site
Favourites
Threads in C++, Can I get an explaination?
Creator: Falkoner
Time: May 6 2008, 6:28 pm
 Falkoner May 6 2008, 6:28 pm Post #1
[Avatar]
Taking StarCraft Map Making to the Limit!
 M
 32
 K
 50
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.
This post was edited 1 time, last edit by Falkoner: May 6 2008, 10:12 pm.
 AntiSleep May 6 2008, 7:37 pm Post #2
[Avatar]
 M
 11
 K
 1
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)
(top)
I do not respect your beliefs, and I implore you not to respect mine. To ask respect of beliefs held without evidence, is to burn the books of progress and hope.

- The Village IconoClast
 Clokr_ May 6 2008, 7:43 pm Post #3
[Avatar]
Omg got a title!
 M
 960
 K
 54
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.
(top)
_______________
G T C A A G T C \__________________________
C A G U···/ŻŻŻŻ\ A G T C G A G A T C A G T
··········\____/ T C A G C T C T A G T C A
C A G T T C A G
/ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ

Was anyone missing my DNA signature? :P

 Falkoner May 6 2008, 10:14 pm Post #4
[Avatar]
Taking StarCraft Map Making to the Limit!
 M
 32
 K
 50
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?

Quote
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.
 Clokr_ May 6 2008, 11:30 pm Post #5
[Avatar]
Omg got a title!
 M
 960
 K
 54
Search it on MSDN or something. It takes one API call to create a thread. CreateThread? I never remember which one...
(top)
_______________
G T C A A G T C \__________________________
C A G U···/ŻŻŻŻ\ A G T C G A G A T C A G T
··········\____/ T C A G C T C T A G T C A
C A G T T C A G
/ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ

Was anyone missing my DNA signature? :P

 ShadowFlare May 7 2008, 5:27 am Post #6
[Avatar]
 M
 608
 K
 36
I think there may be a C library function available for creating a thread.
(top)
 Falkoner May 8 2008, 1:42 am Post #7
[Avatar]
Taking StarCraft Map Making to the Limit!
 M
 32
 K
 50
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.
 Doodle77[MM] May 8 2008, 11:52 pm Post #8
[Avatar]
 M
 n/a
 K
 58
Quote from ShadowFlare
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
 Falkoner May 9 2008, 4:27 am Post #9
[Avatar]
Taking StarCraft Map Making to the Limit!
 M
 32
 K
 50
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?
 Doodle77[MM] May 16 2008, 2:06 am Post #10
[Avatar]
 M
 n/a
 K
 58
Starts 3 threads that find prime numbers, and also counts in the main thread:

Code

#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;
}
This post was edited 1 time, last edit by Doodle77: May 16 2008, 2:12 am.
 Clokr_ May 16 2008, 12:27 pm Post #11
[Avatar]
Omg got a title!
 M
 960
 K
 54
http://en.wikipedia.org/wiki/Process.h

I don't think that'll work without including process.h.
Also you forgot the & when calling _beginthread:

Code

_beginthread(&threadfunc,1024,(void*)997);
(top)
_______________
G T C A A G T C \__________________________
C A G U···/ŻŻŻŻ\ A G T C G A G A T C A G T
··········\____/ T C A G C T C T A G T C A
C A G T T C A G
/ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ

Was anyone missing my DNA signature? :P

 Doodle77[MM] May 16 2008, 9:39 pm Post #12
[Avatar]
 M
 n/a
 K
 58
Quote from Clokr_
http://en.wikipedia.org/wiki/Process.h

I don't think that'll work without including process.h.
Also you forgot the & when calling _beginthread:

Code

_beginthread(&threadfunc,1024,(void*)997);
No, threadfunc and &threadfunc are equivalent.
 Clokr_ May 17 2008, 1:17 pm Post #13
[Avatar]
Omg got a title!
 M
 960
 K
 54
Quote from Doodle77[MM]
Quote from Clokr_
http://en.wikipedia.org/wiki/Process.h

I don't think that'll work without including process.h.
Also you forgot the & when calling _beginthread:

Code

_beginthread(&threadfunc,1024,(void*)997);
No, threadfunc and &threadfunc are equivalent.

Heh, didn't know that ^^
(top)
_______________
G T C A A G T C \__________________________
C A G U···/ŻŻŻŻ\ A G T C G A G A T C A G T
··········\____/ T C A G C T C T A G T C A
C A G T T C A G
/ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ

Was anyone missing my DNA signature? :P

 Heimdal May 21 2008, 12:31 am Post #14
[Avatar]
 M
 225
 K
 47
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_tutorial

If 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).
This post was edited 2 times, last edit by Heimdal: May 21 2008, 5:14 pm.
(top)
Users reading this topic: (plus 1 guests)


[06:52 am]
(U)devilesk -- Lol i go to watch the 10:55 one and its sold out so we wait until the midnight show :P
[06:52 am]
(U)devilesk -- GOOD SHIT
[06:52 am]
(U)devilesk -- DUDE I JUST CAME BACK FROM WATCHING THE DARK KNIGHT
[05:59 am]
O)FaRTy1billion[MM] -- Stupid smilies. :-( *
[05:59 am]
O)FaRTy1billion[MM] -- My PS2 wont even let me watch The Matrix! :'(
[05:59 am]
O)FaRTy1billion[MM] -- Where DO you live? :shifty:
[05:58 am]
DT_Battlekruser -- and I live in the fucking Bay Area :(
Login to shout

©2003-2008 Staredit Network.
Starcraft & Starcraft II are trademarks of Blizzard Entertainment.
Site Index   |   Terms of Service   |   Privacy Policy   |   Contributions