Skip to main content
GameDev.net gamedev.net
🔒 Locked

C++ Multithreading Tutorial

Started by Sonolin Apr 6, 2008 at 10:39 PM 4 replies 4.2k views
Original Post
Sonolin
Sonolin
Hello! I have been working on my C++ for a while now, and have gotten quite far. Just so you know, I am NOT new to programming.. I have MANY languages under my belt, but most are lightweight scripting languages. The reason I began learning C++ was mostly because a) I want to know a more powerful language then the ones I currently know, and b) I want to program games ;) Anyways, enough of the background mumbo-jumbo, my question is about threading. I used to dabble in Java before I took the jump to C++ (don't try post any Java code on me, that was years ago and I really don't remember much ;)) and remember following a "Space Invaders" game tutorial that featured threading. Threading was pretty easy in Java from what I remember at least, and am fearing that threading is something feared in C++ (I get the impression it isn't too easy). Well, I was just wondering where to start. Are there any good newbie threading tutorials for C++? Are there any good threading libraries? Some things to note: - Portability is a VERY high concern for me. I want to be able to use the same(ish) code on Windows, Linux, other Unixes, and Mac, IF this is possible. - I want to make use of C++'s OOP, so a threading tutorial that utilizes classes would be great I can program in C++ so don't worry about showing me anything you think may be over my head (it most likely is but no hurt in trying ;)). Currently, I am programming a roguelike game to expand my learning, and over a couple weeks (of not much coding) things are starting to look good. I tried searching for some threading tutorials but all I found didn't seem to compile correctly (seemed like there was a missing library.. what do I need to include to have threading functions?). Just so you know, I am developing on a Windows Vista machine using GNU G++ through MinGW. I also have a shell account on a Linux box for testing purposes (not sure of distro). Thank you!
fastcall22
fastcall22
Should take a look at boost::thread. To use it, you'll first have to download the boost libraries, set your IDE's "include" path to reference that directory, so when you #include it will look in /root/bin/sdk/boost or C:/bin/sdk/boost, include the appropriate headers in your project, then specify the appropriate libraries to link your project to them.
Sonolin
Sonolin
Thanks for the quick reply!

After some googling, I found some tutorials for POSIX Threads (pthreads) and was wondering if "boost" would be a better choice to use?

I thought POSIX was limited to just Unixes but compiling on my windows box resulted in everything working fine.
fastcall22
fastcall22
Well, if it ain't broke, don't fix it. :)

I'd still consider obtaining boost. It has a lot of things you don't need, but as time goes on, you'll find here and there extensive applications for the little things in boost.

If you are concerned about being open-source, then I'd use boost.
You mentioned that compiling on your windows machine worked fine, are you using cygwin?
If someone were to compile your code on microsoft visual studio, would it be able to compile POSIX?
Hodgman
Hodgman
Quote:
Original post by Sonolin
After some googling, I found some tutorials for POSIX Threads (pthreads) and was wondering if "boost" would be a better choice to use?

I thought POSIX was limited to just Unixes but compiling on my windows box resulted in everything working fine.

POSIX threads (pthreads) is the standard the most Unix-like OS's use. Windows uses a completely different standard, but there is a library called pthreads-win32 (which I'm guessing is the library which you downloaded) which basically wraps up the windows standard and makes it look just like the Unix standard.
Sonolin
Sonolin
Actually, I am using MinGW, which is sort of like cygwin but it allows you to create native windows apps with it. I assume if somebody tried to compile with Visual Studio, it would not compile, as I think the "pthread-win32" port you mentioned is in the MinGW package.

I will look into Boost, though, as it seems from you that it is more expansive. And, yes, there is a good portion of my projects that will be open source.

Thanks

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.