
196
이펙티브 러스트
이러한 장치에도 불구하고 다음과 같이 클래스를 한 번만 잘못 수정해도 스레드 안전이 깨진
다.
8
// 새로운 C++ 메서드 추가하기...
void pay_interest(int32_t percent) {
// ... 여기서 깜박하고 mu_를 제대로 처리하지 못했다.
int64_t interest = (balance_ * percent) / 100;
balance_ += interest;
}
러스트의 데이터 경쟁
러스트 책인데
C
++ 얘기가 좀 길었다. 방금 살펴본
C
++ 클래스를 다음과 같이 러스트 코드
로 바꿔보자.
pub struct BankAccount {
balance: i64,
}
impl BankAccount {
pub fn new() -> Self {
BankAccount { balance: 0 }
}
pub fn balance(&self) -> i64 {
if self.balance < 0 {
panic!("** 잔액 부족: {}", self.balance);
}
self.balance
}
pub fn deposit(&mut self, amount: i64) {
self.balance += amount
}
pub fn withdraw(&mut self, amount: i64) -> bool {
if self.balance ...