简体   繁体   English

直观的线程安全

[英]Intuitive thread safety

After attempting concurrency in my 3D game project, I am beginning to wonder whether it's possible to implement thread safety in a way that's intuitive, clean, and implicit if possible. 在我的3D游戏项目中尝试并发之后,我开始怀疑是否有可能以直观,干净和隐含的方式实现线程安全。 I don't like having to explicitly lock/unlock mutexes just to access a resource - I'd much rather a solution which is completely hidden from the code that actually does the work. 我不喜欢只是为了访问资源而显式锁定/解锁互斥锁 - 我更倾向于一个完全隐藏在实际工作的代码中的解决方案。 Is there any design pattern or structure which allows this in C++11? 在C ++ 11中是否有允许这样的设计模式或结构?

A specific example of a situation where I would like this implemented is detailed below. 下面详细说明我希望实现这种情况的具体示例。

Class 1: Has a shared resource (a std::unordered_map ), which holds std::shared_ptr s to Class 3 instances. 第1类:具有共享资源( std::unordered_map ),它将std::shared_ptr保存到Class 3实例。 This class accesses these shared resources. 该类访问这些共享资源。

Class 2: Has a function which is run in it's own thread. 第2类:有一个在它自己的线程中运行的函数。 This function accesses the shared resources from Class 1 (including accessing Class 3 instances). 此函数访问Class 1中的共享资源(包括访问Class 3实例)。

Class 3: Functions of this class are accessed by both threads. 第3类:两个线程都可以访问此类的函数。 Either thread may read/write. 任何一个线程都可以读/写。

Any suggestions? 有什么建议么? Perhaps the real problem here more likely my design which is making thread safety impractical? 也许这里的真正问题更可能是我的设计使得线程安全不切实际?

There are a crap load of concurrency patterns and idioms. 并发模式和习语有一堆废话。 Nothing however so generic you can just forget about all synchronization with one exception: immutable data types. 无论多么通用,您都可以忘记所有同步与一个例外:不可变数据类型。 If nothing ever writes to a value then threads don't need to synchronize their access because you can only data race if you're someone is writing. 如果没有任何东西写入某个值,那么线程就不需要同步它们的访问权限,因为如果有人在写, 只能进行数据竞争。

This includes reference counts. 这包括参考计数。 If you're using a reference counted pimpl implementation in your value types then the reference count will have to be written on construction and deletion. 如果您在值类型中使用引用计数pimpl实现,则必须在构造和删除时写入引用计数。 Most often this is done with atomic operations which are faster than mutex locks or semaphors but can carry their own issues. 大多数情况下,这是通过原子操作完成的,这些操作比互斥锁或信号量更快,但可以带来自己的问题。

There's copy/modify/swap. 有复制/修改/交换。 Copy the data from an immutable, shared value. 从不可变的共享值复制数据。 Modify the copy. 修改副本。 Swap with the immutable value. 使用不可变值交换。 Look out for the ABA problem. 留意ABA问题。

There's message passing rather than using shared data. 传递消息而不是使用共享数据。

Finally (just what I can think of off the top of my head that is) there's wrapping up shared values into an object that does most of the synchronization stuff for you with some proxy magic. 最后(正如我能想到的那样)我们将共享值包装到一个对象中,该对象通过一些代理魔术为您完成大部分同步操作。 Facebook released one in their folly lib: https://github.com/facebook/folly/blob/master/folly/docs/Synchronized.md This may simplify a lot of already simple issues but isn't a catch-all answer. Facebook在其愚蠢的lib中发布了一个: https//github.com/facebook/folly/blob/master/folly/docs/Synchronized.md这可能会简化许多已经很简单的问题,但并不是一个全面的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM