在Delphi中,OutputDebugString线程安全吗?

发布于 2024-07-13 07:34:33 字数 150 浏览 6 评论 0原文

OutputDebugString(PAnsiChar(''));

线程安全

? 我/我们一直在线程中使用它进行调试,并且我从未想过是否应该以不同的方式进行操作。

(德尔福7)

Is

OutputDebugString(PAnsiChar(''));

thread safe?

I/we have been using it in threads for debugging, and it never occurred to me if I should be doing it a different way.

(Delphi 7)

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

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

发布评论

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

评论(3

み青杉依旧 2024-07-20 07:34:33

好吧,并不是说这不是真的,而是这样,但只是为了让你不必只相信 Lieven 的话:

数据之间的传递
应用程序和调试器已完成
通过 4kbyte 的共享内存块,
带有一个互斥体和两个事件对象
保护对其的访问。 这些是
涉及四个内核对象。

了解 Win32 OutputDebugString 是一篇关于这个问题的优秀文章。

Well, not that it isn't true, it is, but just so that you don't have to just take Lieven word for it:

Passing of data between the
application and the debugger is done
via a 4kbyte chunk of shared memory,
with a Mutex and two Event objects
protecting access to it. These are the
four kernel objects involved.

Understanding Win32 OutputDebugString is an excellent article on the matter.

﹏雨一样淡蓝的深情 2024-07-20 07:34:33

别担心,确实如此。

当应用程序调用 OutputDebugString() 时,它需要这些
脚步。 请注意,任何时候的失败都会放弃整个事情,并且
将调试请求视为无操作(不发送字符串
任何地方)。

  1. 打开 DBWinMutex 并等待,直到我们拥有对它的独占访问权限。
  2. 将DBWIN_BUFFER段映射到内存中:如果没有找到,
    没有运行调试器,因此整个请求将被忽略。
  3. 打开 DBWIN_BUFFER_READY 和 DBWIN_DATA_READY 事件。 与
    共享内存段,缺少对象意味着没有调试器
    可用。
  4. 等待 DBWIN_BUFFER_READY 事件发出信号:这表示
    内存缓冲区不再使用。 大多数时候,这
    事件在检查时会立即发出信号,但不会
    等待缓冲区准备就绪的时间超过 10 秒(超时
    放弃请求)。
  5. 复制最多约4kbytes的数据到内存缓冲区,并存储
    当前的进程 ID 也在那里。 始终在末尾放置一个 NUL 字节
    字符串的。
  6. 通过设置告诉调试器缓冲区已准备好
    DBWIN_DATA_READY 事件。 调试器从那里获取它。
  7. 释放互斥体
  8. 关闭事件和部分对象,但我们保留句柄
    稍后再使用互斥体。

Don't worry, it is.

When OutputDebugString() is called by an application, it takes these
steps. Note that a failure at any point abandons the whole thing and
treats the debugging request as a no-op (the string isn't sent
anywhere).

  1. Open DBWinMutex and wait until we have exclusive access to it.
  2. Map the DBWIN_BUFFER segment into memory: if it's not found,
    there is no debugger running so the entire request is ignored.
  3. Open the DBWIN_BUFFER_READY and DBWIN_DATA_READY events. As with
    the shared memory segment, missing objects mean that no debugger is
    available.
  4. Wait for the DBWIN_BUFFER_READY event to be signaled: this says
    that the memory buffer is no longer in use. Most of the time, this
    event will be signaled immediately when it's examined, but it won't
    wait longer than 10 seconds for the buffer to become ready (a timeout
    abandons the request).
  5. Copy up to about 4kbytes of data to the memory buffer, and store
    the current process ID there as well. Always put a NUL byte at the end
    of the string.
  6. Tell the debugger that the buffer is ready by setting the
    DBWIN_DATA_READY event. The debugger takes it from there.
  7. Release the mutex
  8. Close the Event and Section objects, though we keep the handle to
    the mutex around for later.
你的背包 2024-07-20 07:34:33

不过,我曾经在使用 ISAPI DLL 中的字符串时遇到过麻烦。 由于某些奇怪的原因,未设置 System.pas 中定义的 IsMultiThread 布尔值!

一旦线程运行多个线程,它就会导致奇怪的 AccessViolations...一个简单的“IsMultiThread:=true;” 在单元初始化中修复了它。

I've had trouble once, though, with strings in an ISAPI DLL. For some odd reason the IsMultiThread boolean defined in System.pas was not set!

It was causing weird AccessViolations, once the thread was running more than one thread... A simple "IsMultiThread:=true;" in a unit initialization fixed it.

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