HEAP_NO_SERIALIZE 标志

发布于 2024-09-16 01:41:22 字数 231 浏览 9 评论 0原文

当我在前面的代码示例中调用 HeapCreate 函数时,我使用了 HEAP_NO_SERIALIZE 标志,因为 示例代码的其余部分不是多线程安全的。

Jeffrey Richter 在他的书中写下了这句话(Windows via C/C++)
但这很奇怪。
如果代码不是多线程安全的,他就不必使用该标志。
这是一个错误吗?或者我误解了什么?

When I called the HeapCreate function in the preceding code sample, I used the HEAP_NO_SERIALIZE flag because the
remainder of the sample code is not multithread-safe.

Jeffrey Richter wrote the sentence in his book(Windows via C/C++)
But it's weird.
If the codes are not multithread-safe he didn't have to use the flag.
Is it a bug? Or am I misunderstanding something?

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

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

发布评论

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

评论(2

满意归宿 2024-09-23 01:41:22

使用 HEAP_NO_SERIALIZE 标志,您只需告诉堆它永远不会被不同的线程访问,因此根本不需要线程安全。

如果您不指定此标志,则堆将在每次调用 HeapXXX 函数时在内部获取一把锁,因此尽管您仅从一个线程访问堆,但您仍会产生这种开销。

编辑:在这个示例中,由于它根本不是线程安全的(因此我假设不以任何方式使用线程),因此通知堆它不能是线程安全的是非常有意义的。

With the HEAP_NO_SERIALIZE flag you just tell the Heap that it will never be accessed by different threads, therefore there is no need for thread-safeness at all.

If you do not specify this flag, the heap will internally acquire a lock at every call to the HeapXXX Functions, so you would have the overhead of this although you are accessing the heap from only one thread.

EDIT: In this sample, as it is not thread-safe at all ( and therefore I assume does not employ threading in any way ), it makes perfect sense to inform the heap, that it mustn't be threadsafe.

清泪尽 2024-09-23 01:41:22

默认情况下,Windows 堆执行附加逻辑以确保没有两个线程同时从堆分配内存。具体如何完成仍然是一个秘密,但可能是这样的:

EnterCriticalSection (&cs);
... // Perform logic to allocate memory, set list pointers, ...
LeaveCriticalSection (&cs);

但是,如果您的应用程序不使用多线程,则关键部分可能会产生不可忽视的开销。要消除这种开销,您必须传递标志 HEAP_NO_SERIALIZE,这将消除对关键部分的调用,从而使应用程序稍微快一些。

By default the Windows heap performs additional logic to make sure that no two threads allocate memory from the heap at the same time. How this is exactly done remains a secret, but it will probably be something like this:

EnterCriticalSection (&cs);
... // Perform logic to allocate memory, set list pointers, ...
LeaveCriticalSection (&cs);

However, if your application is not using multithreading, the critical sections may have a non-neglectable overhead. To remove this overhead, you have to pass the flag HEAP_NO_SERIALIZE, which will remove the calls to the critical section, resulting in a slightly faster application.

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