Add threads example

This commit is contained in:
Timothy Warren 2019-02-05 14:02:40 -05:00
parent 971140b6ff
commit 9c148dcf5c
5 changed files with 105 additions and 16 deletions

1
.idea/misc.xml generated
View File

@ -28,6 +28,7 @@
<cargoProject FILE="$PROJECT_DIR$/reference_counting/Cargo.toml" />
<cargoProject FILE="$PROJECT_DIR$/interior_mutability/Cargo.toml" />
<cargoProject FILE="$PROJECT_DIR$/reference_cycles/Cargo.toml" />
<cargoProject FILE="$PROJECT_DIR$/threads/Cargo.toml" />
</component>
<component name="ComposerJsonPluginSettings">
<unboundedVersionInspectionSettings>

5
.idea/rust.iml generated
View File

@ -114,6 +114,10 @@
<sourceFolder url="file://$MODULE_DIR$/reference_cycles/examples" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/reference_cycles/tests" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/reference_cycles/benches" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/threads/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/threads/examples" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/threads/tests" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/threads/benches" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/add/add-one/target" />
<excludeFolder url="file://$MODULE_DIR$/add/adder/target" />
<excludeFolder url="file://$MODULE_DIR$/adder/target" />
@ -140,6 +144,7 @@
<excludeFolder url="file://$MODULE_DIR$/slices/target" />
<excludeFolder url="file://$MODULE_DIR$/smart_pointers/target" />
<excludeFolder url="file://$MODULE_DIR$/structs/target" />
<excludeFolder url="file://$MODULE_DIR$/threads/target" />
<excludeFolder url="file://$MODULE_DIR$/variables/target" />
</content>
<orderEntry type="inheritedJdk" />

40
.idea/workspace.xml generated
View File

@ -15,10 +15,10 @@
<component name="FileEditorManager">
<leaf>
<file pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/reference_cycles/src/main.rs">
<entry file="file://$PROJECT_DIR$/threads/src/main.rs">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="160">
<caret line="10" column="11" lean-forward="true" selection-start-line="10" selection-start-column="11" selection-end-line="10" selection-end-column="11" />
<state relative-caret-position="864">
<caret line="54" column="9" lean-forward="true" selection-start-line="54" selection-start-column="9" selection-end-line="54" selection-end-column="9" />
</state>
</provider>
</entry>
@ -95,6 +95,7 @@
<option value="$PROJECT_DIR$/interior_mutability/src/lib.rs" />
<option value="$PROJECT_DIR$/reference_counting/src/main.rs" />
<option value="$PROJECT_DIR$/reference_cycles/src/main.rs" />
<option value="$PROJECT_DIR$/threads/src/main.rs" />
</list>
</option>
</component>
@ -128,12 +129,12 @@
<path>
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
<item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" />
<item name="reference_cycles" type="9f88c78c:ScopeViewTreeModel$FileNode" />
<item name="threads" type="9f88c78c:ScopeViewTreeModel$FileNode" />
</path>
<path>
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
<item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" />
<item name="reference_cycles" type="9f88c78c:ScopeViewTreeModel$FileNode" />
<item name="threads" type="9f88c78c:ScopeViewTreeModel$FileNode" />
<item name="src" type="9f88c78c:ScopeViewTreeModel$FileNode" />
</path>
</expand>
@ -167,7 +168,7 @@
</component>
<component name="PropertiesComponent">
<property name="JavaScriptWeakerCompletionTypeGuess" value="true" />
<property name="com.android.tools.idea.instantapp.provision.ProvisionBeforeRunTaskProvider.myTimeStamp" value="1549383008644" />
<property name="com.android.tools.idea.instantapp.provision.ProvisionBeforeRunTaskProvider.myTimeStamp" value="1549393270881" />
<property name="javascript.nodejs.core.library.configured.version" value="7.1.0" />
<property name="js.eslint.eslintPackage" value="$USER_HOME$/.yarn-config/global/node_modules/.bin/eslint" />
<property name="js.eslint.nodeInterpreter" value="project" />
@ -195,7 +196,7 @@
</list>
</option>
</component>
<component name="RunManager" selected="Cargo Command.Run reference_cycles">
<component name="RunManager" selected="Cargo Command.Run threads">
<configuration name="&lt;template&gt;" type="TestNG" default="true" selected="false">
<option name="MAIN_CLASS_NAME" />
<option name="VM_PARAMETERS" value="-ea" />
@ -234,13 +235,13 @@
<envs />
<method v="2" />
</configuration>
<configuration name="Run smart_pointers" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
<configuration name="Run threads" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
<option name="channel" value="DEFAULT" />
<option name="command" value="run --package smart_pointers --bin smart_pointers" />
<option name="command" value="run --package threads --bin threads" />
<option name="allFeatures" value="false" />
<option name="nocapture" value="false" />
<option name="backtrace" value="SHORT" />
<option name="workingDirectory" value="file://$PROJECT_DIR$/smart_pointers" />
<option name="workingDirectory" value="file://$PROJECT_DIR$/threads" />
<envs />
<method v="2" />
</configuration>
@ -273,11 +274,11 @@
</configuration>
<recent_temporary>
<list>
<item itemvalue="Cargo Command.Run threads" />
<item itemvalue="Cargo Command.Run reference_cycles" />
<item itemvalue="Cargo Command.Run reference_counting" />
<item itemvalue="Cargo Command.Test lib::tests" />
<item itemvalue="Cargo Command.Test tests::it_sends_an_over_75_percent_warning_message" />
<item itemvalue="Cargo Command.Run smart_pointers" />
</list>
</recent_temporary>
</component>
@ -317,8 +318,8 @@
<frame x="1680" y="-410" width="1050" height="1657" extended-state="6" />
<editor active="true" />
<layout>
<window_info content_ui="combo" id="Project" order="0" sideWeight="0.49644932" visible="true" weight="0.26141885" />
<window_info id="Structure" order="1" sideWeight="0.50355065" side_tool="true" visible="true" weight="0.26141885" />
<window_info content_ui="combo" id="Project" order="0" sideWeight="0.49586776" visible="true" weight="0.26141885" />
<window_info id="Structure" order="1" sideWeight="0.5041322" side_tool="true" visible="true" weight="0.26141885" />
<window_info id="Image Layers" order="2" />
<window_info id="Designer" order="3" />
<window_info id="Capture Tool" order="4" />
@ -326,7 +327,7 @@
<window_info id="Cargo" order="6" sideWeight="0.49791494" weight="0.3286119" />
<window_info anchor="bottom" id="Message" order="0" />
<window_info anchor="bottom" id="Find" order="1" weight="0.32979318" />
<window_info anchor="bottom" id="Run" order="2" weight="0.29696578" />
<window_info active="true" anchor="bottom" id="Run" order="2" visible="true" weight="0.29696578" />
<window_info anchor="bottom" id="Debug" order="3" weight="0.4" />
<window_info anchor="bottom" id="Cvs" order="4" weight="0.25" />
<window_info anchor="bottom" id="Inspection" order="5" weight="0.4" />
@ -756,8 +757,15 @@
</entry>
<entry file="file://$PROJECT_DIR$/reference_cycles/src/main.rs">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="160">
<caret line="10" column="11" lean-forward="true" selection-start-line="10" selection-start-column="11" selection-end-line="10" selection-end-column="11" />
<state relative-caret-position="768">
<caret line="48" column="1" lean-forward="true" selection-start-line="48" selection-start-column="1" selection-end-line="48" selection-end-column="1" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/threads/src/main.rs">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="864">
<caret line="54" column="9" lean-forward="true" selection-start-line="54" selection-start-column="9" selection-end-line="54" selection-end-column="9" />
</state>
</provider>
</entry>

7
threads/Cargo.toml Normal file
View File

@ -0,0 +1,7 @@
[package]
name = "threads"
version = "0.1.0"
authors = ["Timothy Warren <twarren@nabancard.com>"]
edition = "2018"
[dependencies]

68
threads/src/main.rs Normal file
View File

@ -0,0 +1,68 @@
use std::thread;
use std::time::Duration;
use std::sync::mpsc;
fn basic_thread_usage() {
let v = vec![1, 2, 3];
// Move is a keyword, not a parameter, it gives
// ownership of closure values to the thread
let handle = thread::spawn(move|| {
for i in 1..10 {
println!("hi number {} from the spawned thread!", i);
println!("Here's a vector: {:?}", v);
thread::sleep(Duration::from_millis(1));
}
});
handle.join().unwrap();
for i in 1..5 {
println!("hi number {} from the main thread!", i);
thread::sleep(Duration::from_millis(1));
}
}
fn messages_example() {
let (tx, rx) = mpsc::channel();
let tx1 = mpsc::Sender::clone(&tx);
thread::spawn(move || {
let vals = vec![
String::from("hi"),
String::from("from"),
String::from("the"),
String::from("thread"),
];
for val in vals {
tx1.send(val).unwrap();
thread::sleep(Duration::from_secs(1));
}
});
thread::spawn(move || {
let vals = vec![
String::from("more"),
String::from("messages"),
String::from("for"),
String::from("you"),
];
for val in vals {
tx.send(val).unwrap();
thread::sleep(Duration::from_secs(1));
}
});
for received in rx {
println!("Got: {}", received);
}
}
fn main() {
basic_thread_usage();
messages_example();
}