我如何制作任何 C++我使库线程安全吗?

发布于 2024-12-06 20:55:13 字数 208 浏览 0 评论 0原文

首先,我对 C++ 非常有经验,并且了解线程和线程同步的基础知识。我还想编写一个自定义内存分配器作为我的宠物项目,并了解到它们应该是线程安全的。

我理解“线程安全”一词的含义,但我不知道如何使 C++ 代码线程安全。

有没有关于如何使代码线程安全的实际示例或教程?

在内存分配器场景中,是否本质上确保所有变异函数都被标记为关键部分?或者还有更多的事情吗?

First of all, I'm fairly experienced with C++ and understand the basics of threading and thread synchronization. I also want to write a custom memory allocator as a pet project of mine and have read that they should be thread-safe.

I understand what the term "thread-safe" means, but I have no idea on how to make C++ code thread-safe.

Are there any practical examples or tutorials on how to make code thread-safe?

In a memory allocator scenario, is it essentially ensuring that all mutating functions are marked as critical sections? Or is there something more to it?

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

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

发布评论

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

评论(1

合久必婚 2024-12-13 20:55:13

与所有线程问题相同:确保当一个线程正在更改某些内容时,没有其他线程正在访问它。对于内存分配系统,我想您需要一种方法来确保不会同时将同一块内存分配给 2 个线程。无论是包装整个搜索,还是允许多次搜索但在更新分配表时锁定(这可能导致搜索结果无效,需要再次搜索),都取决于您。

Same as all threading issues: make sure that when one thread is changing something, no other thread is accessing it. For a memory allocation system, I would imagine you would need a way of making sure you don't allocate the same block of memory to 2 threads at the same time. Whether that is by wrapping the entire search, or by allowing multiple searches but locking when the allocation table is to be updated (which could then cause the result of the search to become invalid, necessitating another search) would be up to you.

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