Staredit Network > Forums > Technology & Computers > Topic: Threads in C++
Threads in C++
May 6 2008, 6:28 pm
By: Falkoner  

May 6 2008, 6:28 pm Falkoner Post #1



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.

May 6 2008, 7:37 pm AntiSleep Post #2



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.

May 6 2008, 7:43 pm Clokr_ Post #3



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.



?????

May 6 2008, 10:14 pm Falkoner Post #4



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.



None.

May 6 2008, 11:30 pm Clokr_ Post #5



Search it on MSDN or something. It takes one API call to create a thread. CreateThread? I never remember which one...



?????

May 7 2008, 5:27 am ShadowFlare Post #6



I think there may be a C library function available for creating a thread.



None.

May 8 2008, 1:42 am Falkoner Post #7



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.

May 8 2008, 11:52 pm Doodle77 Post #8



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



None.

May 9 2008, 4:27 am Falkoner Post #9



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.

May 16 2008, 2:06 am Doodle77 Post #10



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;
}


Post has been edited 1 time(s), last time on May 16 2008, 2:12 am by Doodle77.



None.

May 16 2008, 12:27 pm Clokr_ Post #11



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);




?????

May 16 2008, 9:39 pm Doodle77 Post #12



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.



None.

May 17 2008, 1:17 pm Clokr_ Post #13



Quote from Doodle77
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 ^^



?????

May 21 2008, 12:31 am Heimdal Post #14



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).

Post has been edited 2 time(s), last time on May 21 2008, 5:14 pm by Heimdal.



None.

Options
  Back to forum
Please log in to reply to this topic or to report it.
Members in this topic: None.
[02:26 pm]
UndeadStar -- Vrael, since the ad messages get removed, you look like a total madman for someone that come late
[2024-5-02. : 1:19 pm]
Vrael -- IM GONNA MANUFACTURE SOME SPORTBALL EQUIPMENT WHERE THE SUN DONT SHINE BOY
[2024-5-02. : 1:35 am]
Ultraviolet -- Vrael
Vrael shouted: NEED SOME SPORTBALL> WE GOT YOUR SPORTBALL EQUIPMENT MANUFACTURING
Gonna put deez sportballs in your mouth
[2024-5-01. : 1:24 pm]
Vrael -- NEED SOME SPORTBALL> WE GOT YOUR SPORTBALL EQUIPMENT MANUFACTURING
[2024-4-30. : 5:08 pm]
Oh_Man -- https://youtu.be/lGxUOgfmUCQ
[2024-4-30. : 7:43 am]
NudeRaider -- Vrael
Vrael shouted: if you're gonna link that shit at least link some quality shit: https://www.youtube.com/watch?v=uUV3KvnvT-w
Yeah I'm not a big fan of Westernhagen either, Fanta vier much better! But they didn't drop the lyrics that fit the situation. Farty: Ich bin wieder hier; nobody: in meinem Revier; Me: war nie wirklich weg
[2024-4-29. : 6:36 pm]
RIVE -- Nah, I'm still on Orange Box.
[2024-4-29. : 4:36 pm]
Oh_Man -- anyone play Outside the Box yet? it was a fun time
[2024-4-29. : 12:52 pm]
Vrael -- if you're gonna link that shit at least link some quality shit: https://www.youtube.com/watch?v=uUV3KvnvT-w
[2024-4-29. : 11:17 am]
Zycorax -- :wob:
Please log in to shout.


Members Online: Roy