views:

85

answers:

3

I already use some new features from C++0x in Visual C++ 2010, like regular expressions or lambda functions. But there is one major feature that is missing: the <thread> header.

Do you know any code which could act as a replacement?

For the moment I'm using boost's thread, but it's not exactly the same as the standard, and it gives me huge compile times. I also found just::thread, but being an amateur I don't want to spend money on it.

I don't think it would be too difficult to code (though I may be wrong) but I don't know the Win32 API enough to do so.

A: 

You can use the win32 thread functions. There are rather simple to use and only require a few function calls to get working. A quick google search should solve all your problems.

Alexander Rafferty
how is that preferable?
jalf
+4  A: 

I think that boost::thread is your best bet. It's throughly tested, and the std::thread is based on it. You should just keep in mind the different areas and try to deal with them the best you can.

As specified here:


The design of this thread is an evolution from boost::thread. The intent is to reuse as much of the boost::thread experience as possible, keeping the good, and changing the very minimum necessary to respond to the known limitations of this field experience.

  • boost's non-copyable, one-handle-maps-to-one-os-thread, semantics are retained. But this thread is movable to allow returning thread from factory functions and placing into containers.
  • This proposal adds cancellation to the boost::thread, which is a significant complication. This change has a large impact not only on thread but the rest of the C++ threading library as well. It is believed this large change is justifiable because of the benefit.
    • The thread destructor must now call cancel prior to detaching to avoid accidently leaking child threads when parent threads are canceled.
    • An explicit detach member is now required to enable detaching without canceling.
  • The concepts of thread handle and thread identity have been separated into two classes (they are the same class in boost::thread). This is to support easier manipulation and storage of thread identity.
  • The ability to create a thread id which is guaranteed to compare equal to no other joinable thread has been added (boost::thread does not have this). This is handy for code which wants to know if it is being executed by the same thread as a previous call (recursive mutexes are a concrete example).
  • There exists a "back door" to get the native thread handle so that clients can manipulate threads using the underlying OS if desired.
Matteo Italia
+6  A: 

I have used boost::thread in performance-sensitive production code and it's way more sophisticated and performant than anything we could have built ourselves in a sensible amount of time. The reader-writer lock in particular is a thing of beauty imo. You have the added benefit that the person who wrote the code and the book is active on Stack Overflow, if you do need help.

Since you are on Windows you may be able to improve compile-times by judicious use of Visual Studio precompiled headers, to avoid repeated load of the Boost headers.

Steve Townsend
`+1` for "...way more sophisticated and performant than anything we could have built ourselves in a sensible amount of time." People often overlook how much knowledge and experience was put into anything that managed to get past boost's review. Anything a single company's development department can come up with is bound to be inferior. And the problem is you might only find out about this when, years later, someone needs that code ported to platform X, which none of you now have any experience with.
sbi