Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
rust
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
orestis.malaspin
rust
Commits
a47f8d0f
There was a problem fetching the pipeline summary.
Commit
a47f8d0f
authored
6 years ago
by
orestis.malaspin
Browse files
Options
Downloads
Patches
Plain Diff
added beginning threads
parent
5cd4a67c
Branches
Branches containing commit
No related tags found
No related merge requests found
Pipeline
#
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
presentation/codes/concurrent/src/main.rs
+80
-5
80 additions, 5 deletions
presentation/codes/concurrent/src/main.rs
presentation/intro.md
+28
-4
28 additions, 4 deletions
presentation/intro.md
with
108 additions
and
9 deletions
presentation/codes/concurrent/src/main.rs
+
80
−
5
View file @
a47f8d0f
use
std
::
thread
;
use
std
::
sync
::
Arc
;
fn
main
()
{
fn
move_borrow
()
{
// // cannot borrow v:
// // comportement possible: le programme a fini avant qu'on ait pu faire un push par exemple.
// let mut v = Vec::new();
// let loc = thread::spawn(|| {
// v.push(1);
// });
// v.push(4);
// println!("{:?}", loc.join().unwrap());
let
loc
=
thread
::
spawn
(
||
{
"world"
});
println!
(
"Hello {}!"
,
loc
.join
()
.unwrap
());
// // v moved in thread, cannot be used elsewhere
// let mut v = Vec::new();
// let loc = thread::spawn(move || {
// v.push(1);
// v
// });
// v.push(4);
// println!("{:?}", loc.join().unwrap());
// here it works
let
mut
v
=
Vec
::
new
();
let
loc
=
thread
::
spawn
(
move
||
{
v
.push
(
1
);
v
});
println!
(
"{:?}"
,
loc
.join
()
.unwrap
());
}
fn
share_with_rc
()
{
// let v = Rc::new(vec![1,2,3,4]); // reference counted
// let v_cloned = v.clone(); // create a new reference
//
// // rc cannot be sent between threads safely.
// thread::spawn(move || {
// println!("{:?}", v);
// });
// // Arc can be sent between threads safely.
// let v = Arc::new(vec![1,2,3,4]); // reference counted
// let v_cloned = v.clone(); // create a new reference
// thread::spawn(move || {
// println!("{:?}", v);
// });
// println!("{:?}", v_cloned);
// Arc can be sent between threads safely.
let
v
=
Arc
::
new
(
vec!
[
1
,
2
,
3
,
4
]);
// reference counted
let
v_cloned
=
v
.clone
();
// create a new reference
thread
::
spawn
(
move
||
{
println!
(
"{:?}"
,
v
);
});
println!
(
"{:?}"
,
v_cloned
);
}
fn
spawn_unordered_not_finishing
()
{
for
i
in
0
..
10
{
let
loc
=
thread
::
spawn
(
move
||
{
println!
(
"Hello from child thread {}!"
,
i
);
});
}
for
i
in
0
..
10
{
println!
(
"Hello {} from main thread!"
,
i
);
}
}
fn
spawn_unordered_join
()
{
let
mut
handles
=
Vec
::
new
();
for
i
in
0
..
10
{
handles
.push
(
thread
::
spawn
(
move
||
{
println!
(
"Hello from child thread {}!"
,
i
);
}));
}
for
h
in
handles
{
h
.join
();
}
}
fn
main
()
{
spawn_unordered_not_finishing
()
spawn_unordered_join
();
// share_with_rc();
}
This diff is collapsed.
Click to expand it.
presentation/intro.md
+
28
−
4
View file @
a47f8d0f
...
...
@@ -393,12 +393,36 @@ Les "data-race" surviennent quand:
. . .
Le partage des données et la mutation est incompatible en Rust
.
Le partage des données et la mutation est incompatible en Rust
: leur absence est garantie à la compilation
#
# Rust prévient à la compilation
#
La concurrence en pratique
-
Les accès concurrents.
-
## thread::spawn .join()
La sytaxe pour créer un
`thread`
et le synchroniser
avec le thread principal est
`thread::spawn`
et
`.join()`
<pre><code
data-trim=
"hljs rust"
class=
"lang-rust"
>
fn main() {
let v = vec![1,2,3];
let handle = thread::spawn(move || { // si on enlève move...
println!("{:?}", v); // ça ne compile plus
});
handle.join();
}
</code></pre>
. . .
Sans le
`move`
on peut avoir des
**accès concurrents**
aux données.
## std::rc::Rc
## std::sync::Arc
# And many more
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment