在系统中只创建一个实例的结构创建和使用的最佳方法是什么?是的,这是必要的,它是OpenGL子系统,制作多个副本并将其传递到各处会增加混乱,而不是减轻它。
单身人士需要尽可能高效。在静态区域上存储任意对象似乎不可能,因为它包含带有析构函数的Vec
。第二个选项是在静态区域存储(不安全)指针,指向堆分配的单例。在保持语法简洁的同时,最方便,最安全的方法是什么。
答案 0 :(得分:97)
非答案答案
一般避免全球状态。相反,尽早在某处构建对象(可能在main
中),然后将对该对象的可变引用传递到需要它的位置。这通常会使您的代码更易于推理,并且不需要向后弯曲。
在决定你想要全局可变变量之前,先仔细看看镜子里的自己。在极少数情况下它很有用,这就是为什么它值得知道该怎么做。
还想制作一个......?
lazy-static箱子可以带走一些创造单身人士的苦差事(下图)。这是一个全局可变向量:
#[macro_use]
extern crate lazy_static;
use std::sync::Mutex;
lazy_static! {
static ref ARRAY: Mutex<Vec<u8>> = Mutex::new(vec![]);
}
fn do_a_call() {
ARRAY.lock().unwrap().push(1);
}
fn main() {
do_a_call();
do_a_call();
do_a_call();
println!("called {}", ARRAY.lock().unwrap().len());
}
如果删除Mutex
,那么您就拥有一个没有任何可变性的全局单例。
如果您只需要跟踪整数值,则可以直接使用atomic:
use std::sync::atomic::{AtomicUsize, Ordering};
static CALL_COUNT: AtomicUsize = AtomicUsize::new(0);
fn do_a_call() {
CALL_COUNT.fetch_add(1, Ordering::SeqCst);
}
fn main() {
do_a_call();
do_a_call();
do_a_call();
println!("called {}", CALL_COUNT.load(Ordering::SeqCst));
}
the Rust 1.0 implementation of stdin
严重影响了这一点。您还应该看看io::Lazy
的现代实现。我已经评论了每行的内容。
use std::sync::{Arc, Mutex, Once, ONCE_INIT};
use std::time::Duration;
use std::{mem, thread};
#[derive(Clone)]
struct SingletonReader {
// Since we will be used in many threads, we need to protect
// concurrent access
inner: Arc<Mutex<u8>>,
}
fn singleton() -> SingletonReader {
// Initialize it to a null value
static mut SINGLETON: *const SingletonReader = 0 as *const SingletonReader;
static ONCE: Once = ONCE_INIT;
unsafe {
ONCE.call_once(|| {
// Make it
let singleton = SingletonReader {
inner: Arc::new(Mutex::new(0)),
};
// Put it in the heap so it can outlive this call
SINGLETON = mem::transmute(Box::new(singleton));
});
// Now we give out a copy of the data that is safe to use concurrently.
(*SINGLETON).clone()
}
}
fn main() {
// Let's use the singleton in a few threads
let threads: Vec<_> = (0..10)
.map(|i| {
thread::spawn(move || {
thread::sleep(Duration::from_millis(i * 10));
let s = singleton();
let mut data = s.inner.lock().unwrap();
*data = i as u8;
})
})
.collect();
// And let's check the singleton every so often
for _ in 0u8..20 {
thread::sleep(Duration::from_millis(5));
let s = singleton();
let data = s.inner.lock().unwrap();
println!("It is: {}", *data);
}
for thread in threads.into_iter() {
thread.join().unwrap();
}
}
打印出来:
It is: 0
It is: 1
It is: 1
It is: 2
It is: 2
It is: 3
It is: 3
It is: 4
It is: 4
It is: 5
It is: 5
It is: 6
It is: 6
It is: 7
It is: 7
It is: 8
It is: 8
It is: 9
It is: 9
It is: 9
此代码使用Rust 1.23.0编译。 Stdin
的实际实现使用一些不稳定的特性来尝试释放分配的内存,而这段代码没有。
实际上,您可能希望SingletonReader
实施Deref
和DerefMut
,这样您就不必戳入对象并自行锁定。< / p>
所有这些工作都是lazy-static为你做的。
请注意,您仍然可以使用正常的Rust范围和模块级隐私来控制对static
或lazy_static
变量的访问。这意味着您可以在模块中或甚至在函数内部声明它,并且它不能在该模块/函数之外访问。这有利于控制访问:
use lazy_static::lazy_static; // 1.2.0
fn only_here() {
lazy_static! {
static ref NAME: String = String::from("hello, world!");
}
println!("{}", &*NAME);
}
fn not_here() {
println!("{}", &*NAME);
}
error[E0425]: cannot find value `NAME` in this scope
--> src/lib.rs:12:22
|
12 | println!("{}", &*NAME);
| ^^^^ not found in this scope
但是,变量仍然是全局变量,因为整个程序中存在一个变量实例。
答案 1 :(得分:0)
使用SpinLock进行全局访问。
#[derive(Default)]
struct ThreadRegistry {
pub enabled_for_new_threads: bool,
threads: Option<HashMap<u32, *const Tls>>,
}
impl ThreadRegistry {
fn threads(&mut self) -> &mut HashMap<u32, *const Tls> {
self.threads.get_or_insert_with(HashMap::new)
}
}
static THREAD_REGISTRY: SpinLock<ThreadRegistry> = SpinLock::new(Default::default());
fn func_1() {
let thread_registry = THREAD_REGISTRY.lock(); // Immutable access
if thread_registry.enabled_for_new_threads {
}
}
fn func_2() {
let mut thread_registry = THREAD_REGISTRY.lock(); // Mutable access
thread_registry.threads().insert(
// ...
);
}
如果您想要可变状态(非单例),请参见What Not to Do in Rust以获取更多描述。
希望有帮助。
答案 2 :(得分:0)
回顾一下:在对象发生变化时,不要使用内部可变性 其内部状态,请考虑使用一种模式来推广新 状态成为当前状态,旧状态的当前消费者将 通过将Arc放入RwLock继续保持住它。
use std::sync::{Arc, RwLock};
#[derive(Default)]
struct Config {
pub debug_mode: bool,
}
impl Config {
pub fn current() -> Arc<Config> {
CURRENT_CONFIG.with(|c| c.read().unwrap().clone())
}
pub fn make_current(self) {
CURRENT_CONFIG.with(|c| *c.write().unwrap() = Arc::new(self))
}
}
thread_local! {
static CURRENT_CONFIG: RwLock<Arc<Config>> = RwLock::new(Default::default());
}
fn main() {
Config { debug_mode: true }.make_current();
if Config::current().debug_mode {
// do something
}
}
答案 3 :(得分:-3)
回答我自己的duplicate question。
Cargo.toml:
[dependencies]
lazy_static = "1.4.0"
板条箱根(lib.rs):
#[macro_use]
extern crate lazy_static;
初始化(不需要不安全的阻止):
/// EMPTY_ATTACK_TABLE defines an empty attack table, useful for initializing attack tables
pub const EMPTY_ATTACK_TABLE: AttackTable = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
];
lazy_static! {
/// KNIGHT_ATTACK is the attack table of knight
pub static ref KNIGHT_ATTACK: AttackTable = {
let mut at = EMPTY_ATTACK_TABLE;
for sq in 0..BOARD_AREA{
at[sq] = jump_attack(sq, &KNIGHT_DELTAS, 0);
}
at
};
...
编辑:
设法通过一次宏来解决此问题,而该宏不需要宏。
Cargo.toml:
[dependencies]
once_cell = "1.3.1"
square.rs:
use once_cell::sync::Lazy;
...
/// AttackTable type records an attack bitboard for every square of a chess board
pub type AttackTable = [Bitboard; BOARD_AREA];
/// EMPTY_ATTACK_TABLE defines an empty attack table, useful for initializing attack tables
pub const EMPTY_ATTACK_TABLE: AttackTable = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
];
/// KNIGHT_ATTACK is the attack table of knight
pub static KNIGHT_ATTACK: Lazy<AttackTable> = Lazy::new(|| {
let mut at = EMPTY_ATTACK_TABLE;
for sq in 0..BOARD_AREA {
at[sq] = jump_attack(sq, &KNIGHT_DELTAS, 0);
}
at
});