Skip to main content
gamedev.net
🔒 Locked

C++ Threads are standard so where is the Mutex class?

Started by EnlightenedOne 31 replies 5k views
Original Post
EnlightenedOne
EnlightenedOne
Hi there I am using what I guess are Posix threads (or about to be using anyway) and I just realised while I can find a CreateThread method and can use a HANDLE container to hold a thread I cannot seem to find a Mutex class in c++ do I have to get an extended library or write my own atomic class for them?

If Mutex's arn't a standard in c++ why on earth are threads constructable?

All the forums I have queried say from 09 at least that threading isn't a part of c++ without a library extension. So please show me the generic mutex class container so I can get threading :) Thanks!!

[Edited by - EnlightenedOne on September 14, 2010 5:04:21 PM]
Rattrap
Rattrap
C++ currently has no concept of threading. Any thread support is provided by native os libraries (posix and win32 for example). If you are looking for something cross-platform and fairly easy to use, I would look at Boost - Threads.
"I can't believe I'm defending logic to a turing machine." - Kent Woolworth [Other Space]
EnlightenedOne
EnlightenedOne
ok thanks very much, I guess I better get on integrating that library into my code :)
empirical2
empirical2
Well CreateThread is a Windows API function. The HANDLE structure is a structure defined by Microsoft as part of the support for this API. Neither is part of the C++ standard.

EnlightenedOne
EnlightenedOne
wait you evaded the question! why is CreateThread a function if theres no concept of threading? and why is there a handle to hold threads?
EnlightenedOne
EnlightenedOne
thanks muchly, im downloading boost now, does that mean there is a standard windows API for threads and mutex's? it would certainly be lighter than the entire boost library just for threads!
Rattrap
Rattrap
Boost basically acts a wrapper for the win32 and posix libraries. Overall, your going have a much easier time maintaining your code using boost libraries.

[edit]
Much of Win32's libraries are C based, so you need to do a lot of manual work, making sure you release everything you create (like CRITICAL_SECTIONS, which are basically mutexes), else you are going to end up deadlocking your program fairly easily.
"I can't believe I'm defending logic to a turing machine." - Kent Woolworth [Other Space]
empirical2
empirical2
Quote:
Original post by EnlightenedOne
wait you evaded the question! why is CreateThread a function if theres no concept of threading? and why is there a handle to hold threads?


How have I evaded the question? CreateThread is a function provided by WINDOWS. Its not a part of the c++ language any more than CreateWindow is.

The HANDLE is defined by windows. Its just a structure.

If I create a structure to hold my Aliens, it doesn't mean c++ has a built concept of my aliens!

I'm not sure how else to explain it.

I can say (AFAIK) Windows has no class for doing threading for you, just the raw functions. Boost is probably best for you right now.
EnlightenedOne
EnlightenedOne
Looking at msdn they appear to speak of it without touching on an actual class
EnlightenedOne
EnlightenedOne
that was a message for rattrap you posted as I was answering him call it a highly ironic lack of a mutex control on this thread :P I am going for the boost resolve thank you both I have upped your ratings :)
rip-off
rip-off
Windows has a CreateMutex function too. The boost mutex classes are great because they are RAII objects, both in terms of managing the OS level mutex handle and also the scoped_locks make it easier to ensure everything gets unlocked properly.
demonkoryu
demonkoryu
Another vote for boost::threads. The good thing about boost libraries is that they're likely to be integrated into the standard (C++0x). Using boost means less porting work once C++0x arrives with std::thread. And it's certainly (as others said already) more portable than using the native system threading facilities.
Antheus
Antheus
Quote:
Original post by EnlightenedOne
wait you evaded the question! why is CreateThread a function if theres no concept of threading?
Because CreateThread is provided by Windows kernel. Windows threads can be called from a C++ application. But same application, while still perfectly valid C++, would not work on any non-windows system.


On Windows, one would use critical sections or mutex.

In addition, C++0x, as partially provided by recent versions of popular compilers, adds standard threading facilities to C++ language.

Alternatively, one can use boost::threads, zthreads, pthreads, TBB, OpenMP or a variety of other cross-platform libraries.
EnlightenedOne
EnlightenedOne
who rated me down for this? I haven't caused any offence with this query have I?
rip-off
rip-off
Ratings are intentionally anonymous. It could have been anything. Don't worry about minor fluctuations in your rating. The trend is what is important.

There was a time that even mentioning your rating would cause it to nosedive, this is no longer an absolute rule but it can happen now and again. Best to keep such thoughts to yourself in future though, just a word of advice.

With time and experience, you can increase your rating by helping others.

Also, gamedev.net isn't particularly fond of "[solved]" - the thread exists for discussion, not just answering the original question. We would ask you to refrain from marking threads as solved in future. In fact, editing the OP and or title (unless it is to remove typos, insert source tags or clarify) is generally frowned upon because it makes the thread harder to follow. A follow up reply makes the thread flow more obvious and helps highlight queries which haven't been answered yet.
Prune
Prune
There's a C++0x compliant threading library here. On Linux, gcc already has most of the threading parts of C++0x so you don't even need a separate library. I've been musing about using just::thread for my Windows stuff, but it's not free for commercial work. I don't know how closely boost follows the standard, but I don't normally use boost due to size and the slow down of compilation speed especially when using Intel's compiler with maxed out optimizations.
"But who prays for Satan? Who, in eighteen centuries, has had the common humanity to pray for the one sinner that needed it most?" --Mark Twain

~~~~~~~~~~~~~~~Looking for a high-performance, easy to use, and lightweight math library? http://www.cmldev.net/ (note: I'm not associated with that project; just a user)
EnlightenedOne
EnlightenedOne
I will remember not to add solved onto any more of my threads but am suprised that hadn't come up sooner! I took it as a sign I had insulted someone in this instance which was not my intent.

I have a disdain for military marshalling of such behaviour, it is a forum and while there are rules and regulations and anonimity is freely a choice there is also a degree of freedom of speech. I frown upon punishment for asking questions to which conduct may well be open ended. You have written your post with a measure of righteousness and power perhaps dictated by your higher rating level or being a member of the GHD, I have contributed to gameDev also and find that section of your post to be mute. Do you speak on behalf of GameDevs moderators?

So while you may judge my action and rate me on it you shall not command me and I have the right to ask if I have offended anyone. By what other purpose or functional measure would my rating stand to show?

Everyone else thankyou for your input :) I have downloaded the BoostLibrary and am just linking it to VS10 now.
rip-off
rip-off
I haven't rated anyone negatively in my time on GD.net. I just rated you up slightly to offset some of the damage you've suffered.

I certainly don't speak for the moderators, but I am echoing what I've read about the style of forums the owners and moderators want to foster.

There aren't rules (AFAIK) on these things, I am merely informing you of the conventions on this forum. You are free to ignore the established conventions.
EnlightenedOne
EnlightenedOne
Ah ok, well that is fair enough to you I corrected the title of the post by removing the solved part and will forgoe doing that in the future.

Got Boost working now!, I am off to build some threads!

Topic Locked

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

Sign in to reply to this topic.