- 主题:如何计数?
如果想实现一个全局的多读多写的计数器,rust怎么搞呢?
【 在 st123123 的大作中提到: 】
: 搞个结构体不就行了…
--
FROM 221.222.20.*
谢谢,不知道singleton 在rust里的最佳实践有没有?
【 在 st123123 的大作中提到: 】
: 跨线程?mpsc channel就行
--
FROM 221.222.20.*
What is the best way to create and use a struct with only one instantiation in the system? Yes, this is necessary, it is the OpenGL subsystem, and making multiple copies of this and passing it around everywhere would add confusion, rather than relieve it.
The singleton needs to be as efficient as possible. It doesn't seem possible to store an arbitrary object on the static area, as it contains a Vec with a destructor. The second option is to store an (unsafe) pointer on the static area, pointing to a heap allocated singleton. What is the most convenient and safest way to do this, while keeping syntax terse?
https://stackoverflow.com/questions/27791532/how-do-i-create-a-global-mutable-singleton
我想到的就是比如连接数据的client,比较重。
【 在 st123123 的大作中提到: 】
: 具体什么场景用singleton?
--
FROM 114.249.21.*
也有道理
【 在 st123123 的大作中提到: 】
: 我觉得没必要刻意用所谓的singleton模式,你只起一个线程专门处理不就是单例了吗
--
FROM 221.222.20.*
现在才觉得,你这个方法是最佳实践
mutex还是尽量少用
【 在 st123123 的大作中提到: 】
: 我觉得没必要刻意用所谓的singleton模式,你只起一个线程专门处理不就是单例了吗
--
修改:littleSram FROM 114.249.24.*
FROM 114.249.24.*
好吧,go里的话,起一个goroutine开销更小
【 在 st123123 的大作中提到: 】
: 也是go提出的最佳实践吧
--
FROM 114.249.24.*