Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
FP
ghostcell
Commits
977e8493
Commit
977e8493
authored
Feb 24, 2021
by
Hai Dang
Browse files
some cleanup
parent
bbdc7000
Pipeline
#42402
passed with stage
in 26 minutes and 56 seconds
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
ghostcell/examples/dlist_arc.rs
View file @
977e8493
...
...
@@ -3,12 +3,10 @@ use ghostcell::GhostToken;
use
std
::
sync
::
RwLock
;
use
std
::{
fmt
,
thread
,
time
};
fn
print_list
<
'id
,
T
:
fmt
::
Debug
>
(
list
:
&
NodePtr
<
'id
,
T
>
,
token
:
&
GhostToken
<
'id
>
)
{
print!
(
"["
);
fn
print_list
<
'id
,
T
:
fmt
::
Debug
>
(
tag
:
char
,
list
:
&
NodePtr
<
'id
,
T
>
,
token
:
&
GhostToken
<
'id
>
)
{
for
d
in
Node
::
iter
(
list
,
token
)
{
print!
(
"{:?}, "
,
d
);
print!
(
"{
}{
:?}, "
,
tag
,
d
);
}
print!
(
"]"
);
}
fn
main
()
{
...
...
@@ -30,7 +28,7 @@ fn main() {
// Print the list we created
print!
(
"Numbers: "
);
// This only needs a &GhostToken
print_list
(
&
list
,
&
token
);
print_list
(
' '
,
&
list
,
&
token
);
println!
();
// Oh, let's print it in parallel with thread `a` and thread `b`.
...
...
@@ -39,12 +37,8 @@ fn main() {
// `b`'s can interleave.
println!
(
"Parallel printing: "
);
rayon
::
join
(
||
for
d
in
Node
::
iter
(
&
list
,
&
token
)
{
print!
(
"a{:?} "
,
d
);
},
||
for
d
in
Node
::
iter
(
&
list
,
&
token
)
{
print!
(
"b{:?} "
,
d
);
}
||
print_list
(
'a'
,
&
list
,
&
token
),
||
print_list
(
'b'
,
&
list
,
&
token
),
);
println!
();
...
...
@@ -59,7 +53,7 @@ fn main() {
});
print!
(
"Post deletion: "
);
print_list
(
&
list
,
&
token
);
print_list
(
' '
,
&
list
,
&
token
);
println!
();
// RwLock on the token to allow concurrent writes and reads.
...
...
@@ -72,15 +66,11 @@ fn main() {
rayon
::
join
(
||
{
let
token
:
&
GhostToken
=
&
token
.read
()
.unwrap
();
for
d
in
Node
::
iter
(
&
list
,
token
)
{
print!
(
"a{:?} "
,
d
);
}
print_list
(
'a'
,
&
list
,
&
token
);
},
||
{
let
token
:
&
GhostToken
=
&
token
.read
()
.unwrap
();
for
d
in
Node
::
iter
(
&
list
,
token
)
{
print!
(
"b{:?} "
,
d
);
}
print_list
(
'b'
,
&
list
,
&
token
);
}
);
println!
();
...
...
@@ -94,9 +84,7 @@ fn main() {
// Sleeps to allow the writer thread to acquire a writer lock.
thread
::
sleep
(
time
::
Duration
::
from_micros
(
10
));
let
token
:
&
GhostToken
=
&
token
.read
()
.unwrap
();
for
d
in
Node
::
iter
(
&
list
,
token
)
{
print!
(
"r{:?} "
,
d
);
}
print_list
(
'r'
,
&
list
,
token
);
},
||
{
let
token
:
&
mut
GhostToken
=
&
mut
token
.write
()
.unwrap
();
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment