by devnull3 · 3y ago · 0 comments
Question/Task: Write a lock implementation in Rust
I got this (lol):
use std::sync::Mutex;
struct Lock {
mutex: Mutex<()>,
}
impl Lock {
fn new() -> Lock {
Lock {
mutex: Mutex::new(()),
}
}
fn lock(&self) -> MutexGuard<()> {
self.mutex.lock().unwrap()
}
fn unlock(&self) {
self.mutex.unlock();
}
}OTOH given "write a lock implementation in Rust without using existing standard library synchronization primitives", both try to implement a spinlock. But GPT-4 knows about compare_and_swap, whereas Bard appears to believe that spinning on load(SeqCst) followed by store(SeqCst) is sufficient to synchronize with other threads.
But a task like that is more likely to depend at how much Rust code each model was trained on rather than its capability to reason.