Need help with threads in C++
Hi I need an example of how threads are coded and implemented in Dev-C++, any help would be greatly appreciated. :)
CodeBoy101
Junior Poster in Training
71 posts since Dec 2007
Reputation Points: 8
Solved Threads: 0
Threads are not natively supported by the c++ language but there are several api functions and libraries that you can use. One of them is win32 api function CreateThread()
Also see this thread
[edit]^^^ Tracey's example is excellent too.[/edit]
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Thanks tracey, but I need help with a few things. First of all, I don't understand the code (I'm new a C++) so could you explain it to me? Second, what s the code supoosed to do? Thirdly, if I wanted to wait on an input from the user for a limited amount of ime, would I have to use threads and how could this be accomplished? Oh, one more thing my library doesn't have XYLock.h, so if I were to get a copy from you and link it to my library, would it work? Thank a lot! for your time! (^ _ ^)
CodeBoy101
Junior Poster in Training
71 posts since Dec 2007
Reputation Points: 8
Solved Threads: 0
Hey doug, thanx for your suggestion, it complied and worked perfectly, but I don't really understand how the program works and I was wondering, how an example like this would work if an input from the user was required.
Correct me if I'm wrong, but based on the output that I'm getting, and looking at the code itself, it seems like the computer is switvching between tasks. Am I right?
(^ _ ^) Thanx!
CodeBoy101
Junior Poster in Training
71 posts since Dec 2007
Reputation Points: 8
Solved Threads: 0
>>it seems like the computer is switvching between tasks. Am I right?
Yes. Unless you have a duel processing computer then the os can only do one thing at the same exact time. It is just an illusion that it looks like two or more things run at the same time. The operating system contains a task switcher that gives only a small amount of CPU time to each program at a time. It has an array of threads that have to be serviced and switches the CPU between them in round-robin fashion. So when you call CreateThread you just add another thread to that list in the task scheduler.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Hey doug, thanks for the code, worked like a charm. Now I know how to use threads! Yeehaw!!
CodeBoy101
Junior Poster in Training
71 posts since Dec 2007
Reputation Points: 8
Solved Threads: 0