简体   繁体   English

C ++ 0x的占位符 <thread> MSVC ++ 2010中的标题

[英]Placeholder for C++0x's <thread> header in MSVC++ 2010

I already use some new features from C++0x in Visual C++ 2010, like regular expressions or lambda functions. 我已经在Visual C ++ 2010中使用了C ++ 0x的一些新功能,比如正则表达式或lambda函数。 But there is one major feature that is missing: the <thread> header . 但是缺少一个主要功能: <thread>标头

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. 目前我正在使用boost的线程,但它与标准不完全相同,它给了我巨大的编译时间。 I also found just::thread, but being an amateur I don't want to spend money on it. 我也发现只是:: thread,但作为一个业余爱好者,我不想花钱。

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. 我认为代码不会太难(虽然我可能错了)但我不知道Win32 API是否足够。

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. 我在性能敏感的生产代码中使用了boost::thread ,它比我们在合理的时间内构建的任何东西都更加复杂和高效。 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. 如果您确实需要帮助,那么编写代码本书 的人在Stack Overflow上处于活动状态会带来额外的好处。

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. 由于您使用的是Windows,因此可以通过合理使用Visual Studio 预编译头来改善编译时间,以避免重复加载Boost头。

I think that boost::thread is your best bet. 我认为boost::thread是你最好的选择。 It's throughly tested, and the std::thread is based on it. 它经过了彻底的测试,而std::thread就是基于它的。 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. 这个线程的设计是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 :: thread体验,保持良好状态,并改变响应此现场体验的已知限制所需的最低限度。

  • boost's non-copyable, one-handle-maps-to-one-os-thread, semantics are retained. boost的不可复制,单句柄映射到一个os线程,语义被保留。 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. 这个提议增加了对boost :: thread的取消,这是一个重要的复杂问题。 This change has a large impact not only on thread but the rest of the C++ threading library as well. 此更改不仅对线程有影响,而且对C ++线程库的其余部分也有很大影响。 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. 线程析构函数现在必须在分离之前调用cancel,以避免在取消父线程时意外泄漏子线程。
    • 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). 线程句柄和线程标识的概念已分为两类(它们在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). 能够创建一个保证比较等于没有其他可连接线程的线程id(boost :: thread没有这个)。 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. 存在一个“后门”来获取本机线程句柄,以便客户端可以根据需要使用底层操作系统来操作线程。

You can use the win32 thread functions. 您可以使用win32线程函数。 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. 快速谷歌搜索应该解决你所有的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM