Visual C 中的 Pthreads

发布于 2024-08-24 17:09:30 字数 369 浏览 7 评论 0原文

我正在 Windows 中尝试多线程,想知道是否应该

如果我学习 Pthreads 将会很有用尝试在不同平台上开发此类应用程序 - 但如果不学习 Win32 API,我会失去什么吗?或者两者足够相似,以至于学习其中一个可以让我轻松地找出另一个?

I'm experimenting with multithreading in Windows and was wondering whether I should

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

岁月静好 2024-08-31 17:09:30
  1. 使用增强线程。当 C++0x 出现时,我们将拥有 std::threads。 Boost 线程具有与 std 线程最接近的实现。

  2. 否则使用 pthreads。 Pthreads 与 std::threads 第二接近,并且形成了 std 线程和 boost 线程的主要基础。

  3. 否则直接进行 Windows 线程处理。您仍然可以了解线程如何工作,并形成事物的心理模型。它只是倾向于使用有点不标准的同步原语。

  1. Use Boost Threads. When C++0x comes along, we will have std::threads. Boost threads has the closest implementation to std threads.

  2. else use pthreads. Pthreads is second closest to std::threads, and formed the main basis of std threads and boost threads.

  3. 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.

微暖i 2024-08-31 17:09:30

如果您要进行大量 Windows 编程,那么学习基本的 Win32 线程结构是值得的:临界区、互锁函数、CreateThread、WaitFor*Object 等。这些并不难理解,并且它们可以透明地转换为其他线程框架中的等效对象。

但是,对于更高级的线程结构(例如信号量、事件等),我会使用 pthreads 库,因为有关这些结构的文档往往更清晰,示例也更丰富。

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.

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.

胡渣熟男 2024-08-31 17:09:30

如果您使用的是 C/C++,请尝试使用 C/C++ 运行时的线程函数。
如果您使用 Win32(或其他非 CRT 函数来创建线程),CRT 可能无法在新线程中正确初始化,从而导致各种问题(您可以在此处阅读相关内容:http://www.codeguru.com/forum/archive/index.php/t-371305.html )。

然而,大多数线程函数(在 CRT、Win32 或 pthread 中)都是基于创建线程、同步线程和销毁线程的功能。实际上,这并不总是那么容易使用。

去年,有一种趋势是转向基于任务的线程(好吧,我就是这么称呼它的,我不知道正式名称是什么)。在基于任务的线程中,您不是启动一个线程然后在其中执行一些逻辑,而是创建一个任务,然后要求“线程逻辑”执行该任务。

支持这种新的线程处理方式的系统有:

  • Visual Studio 2010(我们必须等待几天)
  • Intel 线程构建模块

Visual Studio 2010 甚至有(看起来)特殊的调试逻辑来调试“并行任务” '。

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).

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.

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 even has (it seems) special debugging logic to debug the 'parallel tasks'.

柏林苍穹下 2024-08-31 17:09:30

我发现坚持使用 pthreads 在三个方面可以让我保持理智:

  • 我不必费尽心思
    WinAPI 文档,不习惯使用
    任何品质的。
  • 任何经常使用线程的人
    可以帮忙
    线程。我在网上找到了无数关于 pthreads 的好信息来源。
  • 每当我执行更多操作时
    复杂的是“Hello World”
    WinAPI,我发现它需要很长时间
    比合理的时间长
    预计。这只是我的经验
    不过,输入。

就功能而言,我从未发现 pthreads 缺乏任何东西,因此我认为我没有必要去其他地方寻找。学习一个可以在您处理的任何环境中使用的库也有很多值得说的。

I've found that sticking with pthreads saves my sanity on three counts:

  • 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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文