Add mutex example
This commit is contained in:
parent
9c148dcf5c
commit
ef64ba88ab
1
.idea/misc.xml
generated
1
.idea/misc.xml
generated
@ -29,6 +29,7 @@
|
||||
<cargoProject FILE="$PROJECT_DIR$/interior_mutability/Cargo.toml" />
|
||||
<cargoProject FILE="$PROJECT_DIR$/reference_cycles/Cargo.toml" />
|
||||
<cargoProject FILE="$PROJECT_DIR$/threads/Cargo.toml" />
|
||||
<cargoProject FILE="$PROJECT_DIR$/mutex/Cargo.toml" />
|
||||
</component>
|
||||
<component name="ComposerJsonPluginSettings">
|
||||
<unboundedVersionInspectionSettings>
|
||||
|
5
.idea/rust.iml
generated
5
.idea/rust.iml
generated
@ -118,6 +118,10 @@
|
||||
<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" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/mutex/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/mutex/examples" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/mutex/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/mutex/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" />
|
||||
@ -136,6 +140,7 @@
|
||||
<excludeFolder url="file://$MODULE_DIR$/minigrep/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/minigrep_v2/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/modules/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/mutex/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/rectangles/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/reference_counting/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/reference_cycles/target" />
|
||||
|
46
.idea/workspace.xml
generated
46
.idea/workspace.xml
generated
@ -15,10 +15,10 @@
|
||||
<component name="FileEditorManager">
|
||||
<leaf>
|
||||
<file pinned="false" current-in-tab="true">
|
||||
<entry file="file://$PROJECT_DIR$/threads/src/main.rs">
|
||||
<entry file="file://$PROJECT_DIR$/mutex/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>
|
||||
<caret column="91" selection-start-column="91" selection-end-column="91" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
@ -96,6 +96,7 @@
|
||||
<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" />
|
||||
<option value="$PROJECT_DIR$/mutex/src/main.rs" />
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
@ -129,12 +130,12 @@
|
||||
<path>
|
||||
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
|
||||
<item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" />
|
||||
<item name="threads" type="9f88c78c:ScopeViewTreeModel$FileNode" />
|
||||
<item name="mutex" type="9f88c78c:ScopeViewTreeModel$FileNode" />
|
||||
</path>
|
||||
<path>
|
||||
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
|
||||
<item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" />
|
||||
<item name="threads" type="9f88c78c:ScopeViewTreeModel$FileNode" />
|
||||
<item name="mutex" type="9f88c78c:ScopeViewTreeModel$FileNode" />
|
||||
<item name="src" type="9f88c78c:ScopeViewTreeModel$FileNode" />
|
||||
</path>
|
||||
</expand>
|
||||
@ -168,7 +169,7 @@
|
||||
</component>
|
||||
<component name="PropertiesComponent">
|
||||
<property name="JavaScriptWeakerCompletionTypeGuess" value="true" />
|
||||
<property name="com.android.tools.idea.instantapp.provision.ProvisionBeforeRunTaskProvider.myTimeStamp" value="1549393270881" />
|
||||
<property name="com.android.tools.idea.instantapp.provision.ProvisionBeforeRunTaskProvider.myTimeStamp" value="1549394084677" />
|
||||
<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" />
|
||||
@ -196,7 +197,7 @@
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="RunManager" selected="Cargo Command.Run threads">
|
||||
<component name="RunManager" selected="Cargo Command.Run mutex">
|
||||
<configuration name="<template>" type="TestNG" default="true" selected="false">
|
||||
<option name="MAIN_CLASS_NAME" />
|
||||
<option name="VM_PARAMETERS" value="-ea" />
|
||||
@ -215,6 +216,16 @@
|
||||
<option name="Make" enabled="true" />
|
||||
</method>
|
||||
</configuration>
|
||||
<configuration name="Run mutex" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
|
||||
<option name="channel" value="DEFAULT" />
|
||||
<option name="command" value="run --package mutex --bin mutex" />
|
||||
<option name="allFeatures" value="false" />
|
||||
<option name="nocapture" value="false" />
|
||||
<option name="backtrace" value="SHORT" />
|
||||
<option name="workingDirectory" value="file://$PROJECT_DIR$/mutex" />
|
||||
<envs />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
<configuration name="Run reference_counting" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
|
||||
<option name="channel" value="DEFAULT" />
|
||||
<option name="command" value="run --package reference_counting --bin reference_counting" />
|
||||
@ -255,16 +266,6 @@
|
||||
<envs />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
<configuration name="Test tests::it_sends_an_over_75_percent_warning_message" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
|
||||
<option name="channel" value="DEFAULT" />
|
||||
<option name="command" value="test --package interior_mutability --lib tests::it_sends_an_over_75_percent_warning_message -- --exact" />
|
||||
<option name="allFeatures" value="false" />
|
||||
<option name="nocapture" value="false" />
|
||||
<option name="backtrace" value="SHORT" />
|
||||
<option name="workingDirectory" value="file://$PROJECT_DIR$/interior_mutability" />
|
||||
<envs />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
<configuration default="true" type="JUnit" factoryName="JUnit">
|
||||
<option name="TEST_OBJECT" value="class" />
|
||||
<option name="WORKING_DIRECTORY" value="$MODULE_DIR$" />
|
||||
@ -274,11 +275,11 @@
|
||||
</configuration>
|
||||
<recent_temporary>
|
||||
<list>
|
||||
<item itemvalue="Cargo Command.Run mutex" />
|
||||
<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" />
|
||||
</list>
|
||||
</recent_temporary>
|
||||
</component>
|
||||
@ -765,7 +766,14 @@
|
||||
<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" />
|
||||
<caret line="54" column="9" selection-start-line="54" selection-start-column="9" selection-end-line="54" selection-end-column="9" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/mutex/src/main.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state>
|
||||
<caret column="91" selection-start-column="91" selection-end-column="91" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
|
7
mutex/Cargo.toml
Normal file
7
mutex/Cargo.toml
Normal file
@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "mutex"
|
||||
version = "0.1.0"
|
||||
authors = ["Timothy Warren <twarren@nabancard.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
40
mutex/src/main.rs
Normal file
40
mutex/src/main.rs
Normal file
@ -0,0 +1,40 @@
|
||||
use std::sync::{Arc, Mutex}; // Rc is not thread safe, but Arc is. (Atomic Reference Count)
|
||||
use std::thread;
|
||||
|
||||
fn single_thread_example() {
|
||||
let m = Mutex::new(5);
|
||||
|
||||
{
|
||||
let mut num = m.lock().unwrap();
|
||||
*num = 6;
|
||||
} // Reference to mutex value goes out of scope, lock is released
|
||||
|
||||
println!("m = {:?}", m);
|
||||
}
|
||||
|
||||
fn multi_thread_example() {
|
||||
let counter = Arc::new(Mutex::new(0));
|
||||
let mut handles = vec![];
|
||||
|
||||
for _ in 0..10 {
|
||||
let counter = Arc::clone(&counter);
|
||||
let handle = thread::spawn(move || {
|
||||
let mut num = counter.lock().unwrap();
|
||||
|
||||
*num += 1;
|
||||
});
|
||||
handles.push(handle);
|
||||
}
|
||||
|
||||
for handle in handles {
|
||||
handle.join().unwrap();
|
||||
}
|
||||
|
||||
println!("Result: {}", *counter.lock().unwrap());
|
||||
}
|
||||
|
||||
fn main() {
|
||||
single_thread_example();
|
||||
|
||||
multi_thread_example();
|
||||
}
|
Loading…
Reference in New Issue
Block a user