我如何制作任何 C++我使库线程安全吗?
首先,我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
与所有线程问题相同:确保当一个线程正在更改某些内容时,没有其他线程正在访问它。对于内存分配系统,我想您需要一种方法来确保不会同时将同一块内存分配给 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.