From c2ef2eb19abae116d1af81874d4014aeb62d30df Mon Sep 17 00:00:00 2001 From: qwer252 Date: Wed, 11 Jun 2025 17:43:14 +0800 Subject: [PATCH] Update main.rs --- listings/ch20-advanced-features/listing-20-11/src/main.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/listings/ch20-advanced-features/listing-20-11/src/main.rs b/listings/ch20-advanced-features/listing-20-11/src/main.rs index 4e292b1..5938f2f 100644 --- a/listings/ch20-advanced-features/listing-20-11/src/main.rs +++ b/listings/ch20-advanced-features/listing-20-11/src/main.rs @@ -1,8 +1,7 @@ static mut COUNTER: u32 = 0; -/// SAFETY: Calling this from more than a single thread at a time is undefined -/// behavior, so you *must* guarantee you only call it from a single thread at -/// a time. +/// SAFETY: 同时在多个线程调用这个方法是未定义的行为,所以你*必须*保证同一时间只 +/// 有一个线程在调用它。 unsafe fn add_to_count(inc: u32) { unsafe { COUNTER += inc; @@ -11,7 +10,7 @@ unsafe fn add_to_count(inc: u32) { fn main() { unsafe { - // SAFETY: This is only called from a single thread in `main`. + // SAFETY: 它只在 `main` 这一个线程被调用。 add_to_count(3); println!("COUNTER: {}", *(&raw const COUNTER)); }