windows Visual C++ 中的线程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2403536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pthreads in Visual C++
提问by Jacob
I'm experimenting with multithreading in Windows and was wondering whether I should
我正在 Windows 中试验多线程,想知道我是否应该
Learning Pthreads would be useful if I tried to develop such applications on different platforms - but am I losing anything by not learning Win32 API? Or are both similar enough so that learning one allows me to figure out the other easily?
如果我尝试在不同平台上开发此类应用程序,学习 Pthreads 会很有用 - 但是如果不学习 Win32 API,我会失去什么吗?或者两者都足够相似,以至于学习一个可以让我轻松地找出另一个?
采纳答案by tony
Use Boost Threads. When C++0x comes along, we will have std::threads. Boost threads has the closest implementation to std threads.
else use pthreads. Pthreads is second closest to std::threads, and formed the main basis of std threads and boost threads.
else do windows threading directly. You can still learn how threads work, and form a mental model of things. It just tends to use synchronization primitives that are a bit non-standard.
使用 Boost 线程。当 C++0x 出现时,我们将拥有 std::threads。Boost 线程具有与 std 线程最接近的实现。
否则使用pthreads。Pthreads 是第二接近 std::threads 的,并且形成了 std 线程和 boost 线程的主要基础。
否则直接做windows线程。您仍然可以了解线程的工作原理,并形成事物的心理模型。它只是倾向于使用有点非标准的同步原语。
回答by Patrick
If you're using C/C++, try to use the thread functions of the C/C++ runtime. If you use the Win32 (or other non-CRT functions to create threads) the CRT might not be initialized correctly in the new thread, causing all kinds of problem (you can read about it here: http://www.codeguru.com/forum/archive/index.php/t-371305.html).
如果您使用的是 C/C++,请尝试使用 C/C++ 运行时的线程函数。如果您使用 Win32(或其他非 CRT 函数来创建线程),CRT 可能无法在新线程中正确初始化,从而导致各种问题(您可以在这里阅读:http: //www.codeguru.com /forum/archive/index.php/t-371305.html)。
However, most thread-functions (in the CRT, Win32 or pthread) are all based around the functionality to create threads, synchronize threads and destroy threads. In practice this isn't always that easy to use.
但是,大多数线程函数(在 CRT、Win32 或 pthread 中)都基于创建线程、同步线程和销毁线程的功能。在实践中,这并不总是那么容易使用。
In the last year, there is a trend to go to task-based threading (well, I call it that way, I don't know what the official name is). Instead of starting a thread and then executing some logic in it, in task-based threading you create a task and then ask the 'threading logic' to execute the task.
去年有一种趋势是转向基于任务的线程(嗯,我是这么叫的,我不知道官方名称是什么)。在基于任务的线程中,不是启动一个线程然后在其中执行一些逻辑,而是创建一个任务,然后要求“线程逻辑”执行该任务。
Systems that support this new way of working with threads are:
支持这种新的线程工作方式的系统有:
- Visual Studio 2010 (we'll have to wait a few days for it)
- Intel Threading Building Blocks
- Visual Studio 2010(我们将不得不等待几天)
- 英特尔线程构建块
Visual Studio 2010 even has (it seems) special debugging logic to debug the 'parallel tasks'.
Visual Studio 2010 甚至(似乎)有特殊的调试逻辑来调试“并行任务”。
回答by JSB????
If you're going to do much Windows programming, it will pay to learn the basic Win32 threading constructs: critical sections, interlocked functions, CreateThread
, WaitFor*Object
, etc. These aren't hard to understand, and they translate transparently to equivalent objects in other threading frameworks.
如果您要进行大量 Windows 编程,那么学习基本的 Win32 线程结构是值得的:临界区、互锁函数CreateThread
、WaitFor*Object
、 等。这些并不难理解,并且它们透明地转换为其他线程中的等效对象构架。
However, for more advanced threading constructs such as semaphores, events, etc., I would use the pthreads
library, since the documentation on those tends to be clearer and examples more abundant.
但是,对于更高级的线程构造,例如信号量、事件等,我会使用该pthreads
库,因为关于这些的文档往往更清晰,示例也更丰富。
回答by Sniggerfardimungus
I've found that sticking with pthreads saves my sanity on three counts:
我发现坚持使用 pthreads 可以在三个方面拯救我的理智:
- I don't have to fight through WinAPI docs, which aren't habitually of any quality.
- Anyone that does much with threads can help out with pthreads. I've found infinitely more good sources of information about pthreads online.
- Whenever I implement anything more complicated that "Hello World" with the WinAPI, I find it takes far longer than one could reasonably expect. That's just my empirical input, though.
- 我不必与 WinAPI 文档作斗争,这些文档通常没有任何质量。
- 任何对线程做很多事情的人都可以帮助使用 pthread。我在网上找到了更多关于 pthreads 的好信息来源。
- 每当我用 WinAPI 实现比“Hello World”更复杂的东西时,我发现它花费的时间比人们合理预期的要长得多。不过,这只是我的经验输入。
As far as capabilities are concerned, I've never found pthreads to be lacking in anything, so I don't think I've ever found the need to look elsewhere. There is also a great deal to be said for learning a library that you'll be able to use in any environment you tackle.
就功能而言,我从未发现 pthread 缺少任何东西,因此我认为我从未发现需要查看其他地方。学习一个可以在您处理的任何环境中使用的库也有很多要说的。
回答by ROAR
Take a look at std::thread
看看 std::thread
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2184.html
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2184.html
And a introduction
和介绍