summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/basic.rs8
-rw-r--r--examples/double_mutex.rs8
-rw-r--r--examples/list.rs8
3 files changed, 21 insertions, 3 deletions
diff --git a/examples/basic.rs b/examples/basic.rs
index 4ff85f8..8842e16 100644
--- a/examples/basic.rs
+++ b/examples/basic.rs
@@ -7,12 +7,18 @@ const N: usize = 10;
static DATA: Mutex<i32> = Mutex::new(0);
fn main() {
+ let mut threads = Vec::new();
for _ in 0..N {
- thread::spawn(move || {
+ let th = thread::spawn(move || {
let key = ThreadKey::lock().unwrap();
let mut data = DATA.lock(key);
*data += 1;
});
+ threads.push(th);
+ }
+
+ for th in threads {
+ _ = th.join();
}
let key = ThreadKey::lock().unwrap();
diff --git a/examples/double_mutex.rs b/examples/double_mutex.rs
index 1469f45..d1a939c 100644
--- a/examples/double_mutex.rs
+++ b/examples/double_mutex.rs
@@ -8,14 +8,20 @@ static DATA_1: Mutex<i32> = Mutex::new(0);
static DATA_2: Mutex<String> = Mutex::new(String::new());
fn main() {
+ let mut threads = Vec::new();
for _ in 0..N {
- thread::spawn(move || {
+ let th = thread::spawn(move || {
let key = ThreadKey::lock().unwrap();
let data = (&DATA_1, &DATA_2);
let mut guard = LockGuard::lock(&data, key);
*guard.1 = (100 - *guard.0).to_string();
*guard.0 += 1;
});
+ threads.push(th);
+ }
+
+ for th in threads {
+ _ = th.join();
}
let key = ThreadKey::lock().unwrap();
diff --git a/examples/list.rs b/examples/list.rs
index 1f811db..3903260 100644
--- a/examples/list.rs
+++ b/examples/list.rs
@@ -26,8 +26,9 @@ fn random(key: &mut ThreadKey) -> usize {
}
fn main() {
+ let mut threads = Vec::new();
for _ in 0..N {
- thread::spawn(move || {
+ let th = thread::spawn(move || {
let mut key = ThreadKey::lock().unwrap();
let mut data = Vec::new();
for _ in 0..3 {
@@ -40,6 +41,11 @@ fn main() {
*guard[1] += *guard[2];
*guard[2] += *guard[0];
});
+ threads.push(th);
+ }
+
+ for th in threads {
+ _ = th.join();
}
let mut key = ThreadKey::lock().unwrap();