Skip to content

Commit 010c779

Browse files
prashanthpaigavrie
authored andcommitted
examples/threads: Drop lock early
Demonstrate releasing the thread safe context lock as soon as we're done accessing redis data structures. Earlier, the lock was being held until the value was in scope, which included the sleep time.
1 parent 379a506 commit 010c779

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

examples/threads.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
#[macro_use]
22
extern crate redis_module;
33

4-
use bitflags::_core::time::Duration;
5-
use redis_module::{Context, RedisError, RedisResult, ThreadSafeContext};
4+
use redis_module::{Context, RedisResult, ThreadSafeContext};
5+
use std::mem::drop;
66
use std::thread;
7+
use std::time::Duration;
78

89
fn threads(_: &Context, _args: Vec<String>) -> RedisResult {
910
thread::spawn(move || {
1011
let thread_ctx = ThreadSafeContext::new();
1112

12-
for _ in 0..2 {
13+
loop {
1314
let ctx = thread_ctx.lock();
1415
ctx.call("INCR", &["threads"]).unwrap();
15-
thread::sleep(Duration::from_millis(100));
16+
// release the lock as soon as we're done accessing redis memory
17+
drop(ctx);
18+
thread::sleep(Duration::from_millis(1000));
1619
}
1720
});
1821

0 commit comments

Comments
 (0)