This commit is contained in:
Light-City
2020-03-03 10:00:10 +08:00
parent 535b75acfa
commit 16c12a3bc6
38 changed files with 349 additions and 22 deletions

View File

@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<module classpath="CMake" type="CPP_MODULE" version="4" />

View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
</project>

View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/Threading_In_CPlusPlus.iml" filepath="$PROJECT_DIR$/.idea/Threading_In_CPlusPlus.iml" />
</modules>
</component>
</project>

View File

@@ -0,0 +1,510 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CMakeRunConfigurationManager" shouldGenerate="true" shouldDeleteObsolete="true">
<generated>
<config projectName="Threading_In_CPlusPlus" targetName="intro" />
<config projectName="Threading_In_CPlusPlus" targetName="3.functor" />
<config projectName="Threading_In_CPlusPlus" targetName="critical_section" />
<config projectName="Threading_In_CPlusPlus" targetName="1.thread" />
<config projectName="Threading_In_CPlusPlus" targetName="2.lambda_function" />
<config projectName="Threading_In_CPlusPlus" targetName="1.function_pointer" />
<config projectName="Threading_In_CPlusPlus" targetName="join" />
<config projectName="Threading_In_CPlusPlus" targetName="4.static_member_function" />
<config projectName="Threading_In_CPlusPlus" targetName="detach" />
<config projectName="Threading_In_CPlusPlus" targetName="5.static_member_function" />
</generated>
</component>
<component name="CMakeSettings">
<configurations>
<configuration PROFILE_NAME="Debug" CONFIG_NAME="Debug" />
</configurations>
</component>
<component name="ChangeListManager">
<list default="true" id="496e3cb0-6ee0-494d-9736-c022aecb1d47" name="Default Changelist" comment="" />
<ignored path="$PROJECT_DIR$/cmake-build-debug/" />
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="ClangdSettings">
<option name="formatViaClangd" value="false" />
</component>
<component name="ExecutionTargetManager" SELECTED_TARGET="CMakeBuildProfile:Debug" />
<component name="FileEditorManager">
<leaf SIDE_TABS_SIZE_LIMIT_KEY="300">
<file pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/3.join_detach/detach.cpp">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="285">
<caret line="18" column="8" selection-start-line="18" selection-end-line="33" />
<folding>
<element signature="e#183#202#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/1.thread/intro.cpp">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="229">
<caret line="24" selection-start-line="24" selection-end-line="24" />
<folding>
<element signature="e#39#58#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/1.thread/thread.cpp">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="364">
<caret line="13" column="10" selection-start-line="13" selection-start-column="10" selection-end-line="13" selection-end-column="10" />
<folding>
<element signature="e#39#58#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/4.mutex/critical_section.cpp">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="504">
<caret line="18" column="19" lean-forward="true" selection-start-line="18" selection-start-column="19" selection-end-line="18" selection-end-column="19" />
<folding>
<element signature="e#37#56#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/2.create_type/1.function_pointer.cpp">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="168">
<caret line="6" column="13" lean-forward="true" selection-start-line="6" selection-start-column="13" selection-end-line="6" selection-end-column="13" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/3.join_detach/join.cpp">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="285">
<caret line="19" column="10" selection-start-line="19" selection-start-column="10" selection-end-line="19" selection-end-column="10" />
<folding>
<element signature="e#155#174#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/2.create_type/2.lambda_function.cpp">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="565">
<caret line="25" column="32" lean-forward="true" selection-start-line="25" selection-start-column="32" selection-end-line="25" selection-end-column="32" />
<folding>
<element signature="e#49#66#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/2.create_type/3.functor.cpp">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="336">
<caret line="12" column="7" lean-forward="true" selection-start-line="12" selection-start-column="7" selection-end-line="12" selection-end-column="7" />
<folding>
<element signature="e#40#57#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/2.create_type/4.no_static_member_function.cpp">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="280">
<caret line="10" column="14" selection-start-line="10" selection-start-column="14" selection-end-line="10" selection-end-column="14" />
<folding>
<element signature="e#40#57#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/2.create_type/5.static_member_function.cpp">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="280">
<caret line="11" column="12" lean-forward="true" selection-start-line="11" selection-start-column="12" selection-end-line="11" selection-end-column="12" />
</state>
</provider>
</entry>
</file>
</leaf>
</component>
<component name="IdeDocumentHistory">
<option name="CHANGED_PATHS">
<list>
<option value="$PROJECT_DIR$/CMakeLists.txt" />
<option value="$PROJECT_DIR$/thread/intro.cpp" />
<option value="$PROJECT_DIR$/create_type/3.functor.cpp" />
<option value="$PROJECT_DIR$/create_type/4.static_member_function.cpp" />
<option value="$PROJECT_DIR$/create_type/4.no_static_member_function.cpp" />
<option value="$PROJECT_DIR$/create_type/5.static_member_function.cpp" />
<option value="$PROJECT_DIR$/create_type/1.function_pointer.cpp" />
<option value="$PROJECT_DIR$/create_type/2.lambda_function.cpp" />
<option value="$PROJECT_DIR$/thread/thread.cpp" />
<option value="$PROJECT_DIR$/3.join_detach/detach.cpp" />
<option value="$PROJECT_DIR$/4.mutex/critical_section.cpp" />
<option value="$PROJECT_DIR$/3.join_detach/join.cpp" />
</list>
</option>
</component>
<component name="ProjectFrameBounds" extendedState="6">
<option name="x" value="145" />
<option name="y" value="274" />
<option name="width" value="1196" />
<option name="height" value="754" />
</component>
<component name="ProjectView">
<navigator proportions="" version="1">
<foldersAlwaysOnTop value="true" />
</navigator>
<panes>
<pane id="ProjectPane">
<subPane>
<expand>
<path>
<item name="Threading_In_CPlusPlus" type="b2602c69:ProjectViewProjectNode" />
<item name="Threading_In_CPlusPlus" type="462c0819:PsiDirectoryNode" />
</path>
<path>
<item name="Threading_In_CPlusPlus" type="b2602c69:ProjectViewProjectNode" />
<item name="Threading_In_CPlusPlus" type="462c0819:PsiDirectoryNode" />
<item name="1.thread" type="462c0819:PsiDirectoryNode" />
</path>
<path>
<item name="Threading_In_CPlusPlus" type="b2602c69:ProjectViewProjectNode" />
<item name="Threading_In_CPlusPlus" type="462c0819:PsiDirectoryNode" />
<item name="2.create_type" type="462c0819:PsiDirectoryNode" />
</path>
<path>
<item name="Threading_In_CPlusPlus" type="b2602c69:ProjectViewProjectNode" />
<item name="Threading_In_CPlusPlus" type="462c0819:PsiDirectoryNode" />
<item name="3.join_detach" type="462c0819:PsiDirectoryNode" />
</path>
<path>
<item name="Threading_In_CPlusPlus" type="b2602c69:ProjectViewProjectNode" />
<item name="Threading_In_CPlusPlus" type="462c0819:PsiDirectoryNode" />
<item name="4.mutex" type="462c0819:PsiDirectoryNode" />
</path>
</expand>
<select />
</subPane>
</pane>
<pane id="Scope" />
</panes>
</component>
<component name="PropertiesComponent">
<property name="WebServerToolWindowFactoryState" value="false" />
<property name="last_opened_file_path" value="$PROJECT_DIR$/3.join_detach" />
<property name="nodejs_interpreter_path.stuck_in_default_project" value="undefined stuck path" />
<property name="nodejs_npm_path_reset_for_default_project" value="true" />
</component>
<component name="RecentsManager">
<key name="CopyFile.RECENT_KEYS">
<recent name="$PROJECT_DIR$/3.join_detach" />
<recent name="$PROJECT_DIR$/create_type" />
<recent name="$PROJECT_DIR$/thread" />
</key>
</component>
<component name="RunDashboard">
<option name="ruleStates">
<list>
<RuleState>
<option name="name" value="ConfigurationTypeDashboardGroupingRule" />
</RuleState>
<RuleState>
<option name="name" value="StatusDashboardGroupingRule" />
</RuleState>
</list>
</option>
</component>
<component name="RunManager" selected="CMake Application.critical_section">
<configuration name="1.function_pointer" type="CMakeRunConfiguration" factoryName="Application" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Threading_In_CPlusPlus" TARGET_NAME="1.function_pointer" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="Threading_In_CPlusPlus" RUN_TARGET_NAME="1.function_pointer">
<method v="2">
<option name="com.jetbrains.cidr.execution.CidrBuildBeforeRunTaskProvider$BuildBeforeRunTask" enabled="true" />
</method>
</configuration>
<configuration name="1.thread" type="CMakeRunConfiguration" factoryName="Application" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Threading_In_CPlusPlus" TARGET_NAME="1.thread" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="Threading_In_CPlusPlus" RUN_TARGET_NAME="1.thread">
<method v="2">
<option name="com.jetbrains.cidr.execution.CidrBuildBeforeRunTaskProvider$BuildBeforeRunTask" enabled="true" />
</method>
</configuration>
<configuration name="2.lambda_function" type="CMakeRunConfiguration" factoryName="Application" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Threading_In_CPlusPlus" TARGET_NAME="2.lambda_function" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="Threading_In_CPlusPlus" RUN_TARGET_NAME="2.lambda_function">
<method v="2">
<option name="com.jetbrains.cidr.execution.CidrBuildBeforeRunTaskProvider$BuildBeforeRunTask" enabled="true" />
</method>
</configuration>
<configuration name="3.functor" type="CMakeRunConfiguration" factoryName="Application" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Threading_In_CPlusPlus" TARGET_NAME="3.functor" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="Threading_In_CPlusPlus" RUN_TARGET_NAME="3.functor">
<method v="2">
<option name="com.jetbrains.cidr.execution.CidrBuildBeforeRunTaskProvider$BuildBeforeRunTask" enabled="true" />
</method>
</configuration>
<configuration name="4.static_member_function" type="CMakeRunConfiguration" factoryName="Application" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Threading_In_CPlusPlus" TARGET_NAME="4.static_member_function" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="Threading_In_CPlusPlus" RUN_TARGET_NAME="4.static_member_function">
<method v="2">
<option name="com.jetbrains.cidr.execution.CidrBuildBeforeRunTaskProvider$BuildBeforeRunTask" enabled="true" />
</method>
</configuration>
<configuration name="5.static_member_function" type="CMakeRunConfiguration" factoryName="Application" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Threading_In_CPlusPlus" TARGET_NAME="5.static_member_function" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="Threading_In_CPlusPlus" RUN_TARGET_NAME="5.static_member_function">
<method v="2">
<option name="com.jetbrains.cidr.execution.CidrBuildBeforeRunTaskProvider$BuildBeforeRunTask" enabled="true" />
</method>
</configuration>
<configuration name="critical_section" type="CMakeRunConfiguration" factoryName="Application" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Threading_In_CPlusPlus" TARGET_NAME="critical_section" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="Threading_In_CPlusPlus" RUN_TARGET_NAME="critical_section">
<method v="2">
<option name="com.jetbrains.cidr.execution.CidrBuildBeforeRunTaskProvider$BuildBeforeRunTask" enabled="true" />
</method>
</configuration>
<configuration name="detach" type="CMakeRunConfiguration" factoryName="Application" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Threading_In_CPlusPlus" TARGET_NAME="detach" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="Threading_In_CPlusPlus" RUN_TARGET_NAME="detach">
<method v="2">
<option name="com.jetbrains.cidr.execution.CidrBuildBeforeRunTaskProvider$BuildBeforeRunTask" enabled="true" />
</method>
</configuration>
<configuration name="intro" type="CMakeRunConfiguration" factoryName="Application" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Threading_In_CPlusPlus" TARGET_NAME="intro" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="Threading_In_CPlusPlus" RUN_TARGET_NAME="intro">
<method v="2">
<option name="com.jetbrains.cidr.execution.CidrBuildBeforeRunTaskProvider$BuildBeforeRunTask" enabled="true" />
</method>
</configuration>
<configuration name="join" type="CMakeRunConfiguration" factoryName="Application" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Threading_In_CPlusPlus" TARGET_NAME="join" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="Threading_In_CPlusPlus" RUN_TARGET_NAME="join">
<method v="2">
<option name="com.jetbrains.cidr.execution.CidrBuildBeforeRunTaskProvider$BuildBeforeRunTask" enabled="true" />
</method>
</configuration>
<list>
<item itemvalue="CMake Application.1.function_pointer" />
<item itemvalue="CMake Application.1.thread" />
<item itemvalue="CMake Application.2.lambda_function" />
<item itemvalue="CMake Application.3.functor" />
<item itemvalue="CMake Application.4.static_member_function" />
<item itemvalue="CMake Application.5.static_member_function" />
<item itemvalue="CMake Application.critical_section" />
<item itemvalue="CMake Application.detach" />
<item itemvalue="CMake Application.intro" />
<item itemvalue="CMake Application.join" />
</list>
</component>
<component name="SvnConfiguration">
<configuration />
</component>
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="496e3cb0-6ee0-494d-9736-c022aecb1d47" name="Default Changelist" comment="" />
<created>1580451516661</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1580451516661</updated>
<workItem from="1580451518064" duration="6654000" />
<workItem from="1580469703743" duration="675000" />
<workItem from="1580516401851" duration="3033000" />
</task>
<servers />
</component>
<component name="TimeTrackingManager">
<option name="totallyTimeSpent" value="10362000" />
</component>
<component name="ToolWindowManager">
<frame x="0" y="28" width="1920" height="1052" extended-state="6" />
<editor active="true" />
<layout>
<window_info active="true" content_ui="combo" id="Project" order="0" visible="true" weight="0.17147435" />
<window_info id="Structure" order="1" side_tool="true" weight="0.25" />
<window_info id="Favorites" order="2" side_tool="true" />
<window_info anchor="bottom" id="Message" order="0" />
<window_info anchor="bottom" id="Find" order="1" weight="0.32854864" />
<window_info anchor="bottom" id="Run" order="2" weight="0.29186603" />
<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" />
<window_info anchor="bottom" id="TODO" order="6" />
<window_info anchor="bottom" id="Database Changes" order="7" />
<window_info anchor="bottom" id="Statistic" order="8" />
<window_info anchor="bottom" id="Messages" order="9" weight="0.32854864" />
<window_info anchor="bottom" id="Terminal" order="10" />
<window_info anchor="bottom" id="Event Log" order="11" side_tool="true" />
<window_info anchor="bottom" id="Version Control" order="12" />
<window_info anchor="bottom" id="CMake" order="13" weight="0.086124405" />
<window_info anchor="right" id="Commander" internal_type="SLIDING" order="0" type="SLIDING" weight="0.4" />
<window_info anchor="right" id="Ant Build" order="1" weight="0.25" />
<window_info anchor="right" content_ui="combo" id="Hierarchy" order="2" weight="0.25" />
<window_info anchor="right" id="Database" order="3" />
</layout>
</component>
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="1" />
</component>
<component name="editorHistoryManager">
<entry file="file://$PROJECT_DIR$/main.cpp" />
<entry file="file://$USER_HOME$/gcc8.3/include/c++/8.3.0/chrono">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="113">
<caret line="822" column="60" selection-start-line="822" selection-start-column="60" selection-end-line="822" selection-end-column="60" />
</state>
</provider>
</entry>
<entry file="file://$USER_HOME$/gcc8.3/include/c++/8.3.0/thread">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="295">
<caret line="117" column="6" selection-start-line="117" selection-start-column="6" selection-end-line="117" selection-end-column="6" />
<folding>
<element signature="e#2999#3088#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/CMakeLists.txt">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="112">
<caret line="4" column="28" selection-start-line="4" selection-start-column="28" selection-end-line="4" selection-end-column="28" />
</state>
</provider>
</entry>
<entry file="file://$USER_HOME$/gcc8.3/include/c++/8.3.0/mutex">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="1650">
<caret line="606" column="30" selection-start-line="606" selection-start-column="30" selection-end-line="606" selection-end-column="30" />
</state>
</provider>
</entry>
<entry file="file:///usr/include/x86_64-linux-gnu/bits/pthreadtypes-arch.h">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="336">
<caret line="24" column="10" selection-start-line="24" selection-start-column="10" selection-end-line="24" selection-end-column="10" />
</state>
</provider>
</entry>
<entry file="file:///usr/include/x86_64-linux-gnu/bits/pthreadtypes.h">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="339">
<caret line="69" column="17" selection-start-line="69" selection-start-column="17" selection-end-line="69" selection-end-column="17" />
</state>
</provider>
</entry>
<entry file="file://$USER_HOME$/gcc8.3/include/c++/8.3.0/x86_64-pc-linux-gnu/bits/gthr-default.h">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="299">
<caret line="49" column="10" selection-start-line="49" selection-start-column="10" selection-end-line="49" selection-end-column="10" />
</state>
</provider>
</entry>
<entry file="file://$USER_HOME$/gcc8.3/include/c++/8.3.0/bits/std_mutex.h">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="310">
<caret line="62" column="21" lean-forward="true" selection-start-line="62" selection-start-column="21" selection-end-line="62" selection-end-column="21" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/4.mutex/critical_section.cpp">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="504">
<caret line="18" column="19" lean-forward="true" selection-start-line="18" selection-start-column="19" selection-end-line="18" selection-end-column="19" />
<folding>
<element signature="e#37#56#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/1.thread/intro.cpp">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="229">
<caret line="24" selection-start-line="24" selection-end-line="24" />
<folding>
<element signature="e#39#58#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/2.create_type/1.function_pointer.cpp">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="168">
<caret line="6" column="13" lean-forward="true" selection-start-line="6" selection-start-column="13" selection-end-line="6" selection-end-column="13" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/1.thread/thread.cpp">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="364">
<caret line="13" column="10" selection-start-line="13" selection-start-column="10" selection-end-line="13" selection-end-column="10" />
<folding>
<element signature="e#39#58#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/2.create_type/5.static_member_function.cpp">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="280">
<caret line="11" column="12" lean-forward="true" selection-start-line="11" selection-start-column="12" selection-end-line="11" selection-end-column="12" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/2.create_type/4.no_static_member_function.cpp">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="280">
<caret line="10" column="14" selection-start-line="10" selection-start-column="14" selection-end-line="10" selection-end-column="14" />
<folding>
<element signature="e#40#57#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/2.create_type/3.functor.cpp">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="336">
<caret line="12" column="7" lean-forward="true" selection-start-line="12" selection-start-column="7" selection-end-line="12" selection-end-column="7" />
<folding>
<element signature="e#40#57#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/2.create_type/2.lambda_function.cpp">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="565">
<caret line="25" column="32" lean-forward="true" selection-start-line="25" selection-start-column="32" selection-end-line="25" selection-end-column="32" />
<folding>
<element signature="e#49#66#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/3.join_detach/join.cpp">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="285">
<caret line="19" column="10" selection-start-line="19" selection-start-column="10" selection-end-line="19" selection-end-column="10" />
<folding>
<element signature="e#155#174#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/3.join_detach/detach.cpp">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="285">
<caret line="18" column="8" selection-start-line="18" selection-end-line="33" />
<folding>
<element signature="e#183#202#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
</component>
</project>

View File

@@ -0,0 +1,41 @@
//
// Created by light on 20-1-31.
//
#include <iostream>
#include <chrono>
using namespace std::chrono;
using namespace std;
using ull = unsigned long long;
ull OddSum = 0;
ull EvenSum = 0;
void findEven(ull start, ull end) {
for (ull i = start; i <= end; ++i)
if ((i & 1) == 0)
EvenSum += i;
}
void findOdd(ull start, ull end) {
for (ull i = start; i <= end; ++i)
if ((i & 1) == 1)
OddSum += i;
}
int main() {
ull start = 0, end = 1900000000;
auto startTime = high_resolution_clock::now();
findOdd(start, end);
findEven(start, end);
auto stopTime = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stopTime - startTime);
cout << "OddSum : " << OddSum << endl;
cout << "EvenSum: " << EvenSum << endl;
cout << "Sec: " << duration.count() / 1000000 << endl;
return 0;
}

View File

@@ -0,0 +1,56 @@
//
// Created by light on 20-1-31.
//
#include <iostream>
#include <thread>
#include <chrono>
using namespace std::chrono;
using namespace std;
/**
* 1.普通函数指针
* 2.Lambda函数
* 3.Functors
* 4.非静态成员函数
* 5.静态成员函数
*/
using ull = unsigned long long;
ull OddSum = 0;
ull EvenSum = 0;
void findEven(ull start, ull end) {
for (ull i = start; i <= end; ++i)
if ((i & 1) == 0)
EvenSum += i;
}
void findOdd(ull start, ull end) {
for (ull i = start; i <= end; ++i)
if ((i & 1) == 1)
OddSum += i;
}
int main() {
ull start = 0, end = 1900000000;
auto startTime = high_resolution_clock::now();
std::thread t1(findEven,start,end);
std::thread t2(findOdd,start,end);
t1.join();
t2.join();
auto stopTime = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stopTime - startTime);
cout << "OddSum : " << OddSum << endl;
cout << "EvenSum: " << EvenSum << endl;
cout << "Sec: " << duration.count() / 1000000 << endl;
return 0;
}

View File

@@ -0,0 +1,23 @@
//
// Created by light on 20-1-31.
//
// 1.函数指针
#include <thread>
#include <iostream>
using namespace std;
void fun(int x) {
while (x-- > 0) {
cout << x << endl;
}
}
// 注意:如果我们创建多线程 并不会保证哪一个先开始
int main() {
std::thread t1(fun, 10);
// std::1.thread t2(fun, 10);
t1.join();
// t2.join();
return 0;
}

View File

@@ -0,0 +1,31 @@
//
// Created by light on 20-1-31.
//
// 1.函数指针
#include <thread>
#include <iostream>
using namespace std;
// 注意:如果我们创建多线程 并不会保证哪一个先开始
int main() {
// 2.Lambda函数
auto fun = [](int x) {
while (x-- > 0) {
cout << x << endl;
}
};
// std::1.thread t1(fun, 10);
// 也可以写成下面:
std::thread t1_1([](int x) {
while (x-- > 0) {
cout << x << endl;
}
}, 11);
// std::1.thread t2(fun, 10);
// t1.join();
t1_1.join();
// t2.join();
return 0;
}

View File

@@ -0,0 +1,25 @@
//
// Created by light on 20-1-31.
//
#include <thread>
#include <iostream>
using namespace std;
// 3.functor (Funciton Object)
class Base {
public:
void operator()(int x) {
while (x-- > 0) {
cout << x << endl;
}
}
};
int main() {
thread t(Base(), 10);
t.join();
return 0;
}

View File

@@ -0,0 +1,26 @@
//
// Created by light on 20-1-31.
//
#include <thread>
#include <iostream>
using namespace std;
// 4.Non-static member function
class Base {
public:
void fun(int x) {
while (x-- > 0) {
cout << x << endl;
}
}
};
int main() {
Base b;
thread t(&Base::fun,&b, 10);
t.join();
return 0;
}

View File

@@ -0,0 +1,25 @@
//
// Created by light on 20-1-31.
//
#include <thread>
#include <iostream>
using namespace std;
// 4.Non-static member function
class Base {
public:
static void fun(int x) {
while (x-- > 0) {
cout << x << endl;
}
}
};
int main() {
thread t(&Base::fun, 10);
t.join();
return 0;
}

View File

@@ -0,0 +1,34 @@
//
// Created by light on 20-1-31.
//
// join 注意点
/**
* 这用于从父线程分离新创建的线程
* 在分离线程之前请务必检查它是否可以joinable
* 否则可能会导致两次分离并且双重detach()将导致程序终止
* 如果我们有分离的线程并且main函数正在返回那么分离的线程执行将被挂起
*/
#include <iostream>
#include <thread>
#include <chrono>
#include <mutex>
using namespace std;
void run(int count) {
while (count-- > 0) {
cout << count << endl;
}
std::this_thread::sleep_for(chrono::seconds(3));
}
int main() {
thread t1(run, 10);
cout << "main()" << endl;
t1.detach();
if(t1.joinable())
t1.detach();
cout << "main() after" << endl;
return 0;
}

View File

@@ -0,0 +1,35 @@
//
// Created by light on 20-1-31.
//
// join 注意点
/**
* 一旦线程开始我们要想等待线程完成需要在该对象上调用join()
* 双重join将导致程序终止
* 在join之前我们应该检查显示是否可以被join,通过使用joinable()
*/
#include <iostream>
#include <thread>
#include <chrono>
using namespace std;
void run(int count) {
while (count-- > 0) {
cout << count << endl;
}
std::this_thread::sleep_for(chrono::seconds(3));
}
int main() {
thread t1(run, 10);
cout << "main()" << endl;
t1.join();
if (t1.joinable()) {
t1.join();
}
cout << "main() after" << endl;
return 0;
}

View File

@@ -0,0 +1,34 @@
//
// Created by light on 20-2-1.
//
#include <iostream>
#include <mutex>
#include <thread>
using namespace std;
int sum = 0; //shared
mutex m;
void *countgold() {
int i; //local to each thread
for (i = 0; i < 10000000; i++) {
m.lock();
sum += 1;
m.unlock();
}
return NULL;
}
int main() {
thread t1(countgold);
thread t2(countgold);
//Wait for both threads to finish
t1.join();
t2.join();
cout << "sum = " << sum << endl;
return 0;
}

View File

@@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 3.14)
project(Threading_In_CPlusPlus)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
add_executable(intro 1.thread/intro.cpp)
add_executable(1.thread 1.thread/thread.cpp)
add_executable(1.function_pointer 2.create_type/1.function_pointer.cpp)
add_executable(2.lambda_function 2.create_type/2.lambda_function.cpp)
add_executable(3.functor 2.create_type/3.functor.cpp)
add_executable(4.static_member_function 2.create_type/4.no_static_member_function.cpp)
add_executable(5.static_member_function 2.create_type/5.static_member_function.cpp)
add_executable(join 3.join_detach/join.cpp)
add_executable(detach 3.join_detach/detach.cpp)
add_executable(critical_section 4.mutex/critical_section.cpp)

View File

@@ -0,0 +1,20 @@
//
// Created by light on 19-11-5.
//
#include <iostream>
#include <thread>
#include <unistd.h>
using namespace std;
void hello() {
cout << "hello world" << endl;
}
int main() {
thread t(hello);
t.join(); // must add this line otherwise will failed!
// 需要注意的是线程对象执行了join后就不再joinable了所以只能调用join一次。
return 0;
}

View File

@@ -0,0 +1,108 @@
//
// Created by light on 19-11-5.
//
#include <iostream>
#include <thread>
#include <unistd.h>
#include <cassert>
using namespace std;
class background_task {
public:
void operator()() const {
cout << "ok" << endl;
}
};
void do_something(int &i) {
cout << "do_something" << endl;
}
struct func {
int &i;
func(int &i_) : i(i_) {}
void operator()() {
for (unsigned j = 0; j < 1000000; ++j) {
do_something(i); // 1. 潜在访问隐患:悬空引用
}
}
};
// 特殊情况下的等待
void f() {
int some_local_state = 0;
func my_func(some_local_state);
std::thread t(my_func);
try {
// do_something_in_current_thread();
}
catch (...) {
t.join(); // 1
throw;
}
t.join(); // 2
}
// try catch 只能捕获轻量级错误,所以如需确保线程在函数之前结束——查看是否因为线程函数使用了局部变量的引用,
// 以及其他原因——而后再确定一下程序可能会退出的途径,无论正常与否,可以提供一个简洁的机制,来做解决这个问题。
// 一种方式是使用“资源获取即初始化方式”(RAIIResource Acquisition Is Initialization)并且提供一个类在析构函数中使用join()
// std::thread支持移动的好处是可以创建thread_guard类的实例并且拥有其线程的所有权。
class thread_guard {
std::thread &t;
public:
explicit thread_guard(std::thread &t_) :
t(t_) {}
~thread_guard() {
if (t.joinable()) // 1
{
t.join(); // 2
}
}
thread_guard(thread_guard const &) = delete; // 3
thread_guard &operator=(thread_guard const &) = delete;
};
void f1()
{
int some_local_state=0;
func my_func(some_local_state);
std::thread t(my_func);
thread_guard g(t);
// do_something_in_current_thread();
} // 4
// 当线程执行到4处时局部对象就要被逆序销毁了。因此thread_guard对象g是第一个被销毁的
// 这时线程在析构函数中被加入2到原始线程中。
// 即使do_something_in_current_thread抛出一个异常这个销毁依旧会发生。
int main() {
background_task f;
// thread t(f); // ok
// t.join();
//声明一个名为my_threadx的函数,这个函数带有一个参数(函数指针指向没有参数并返回background_task对象的函数)返回一个std::thread对象的函数
// thread my_thread1(background_task());
// 针对Most Vexing Parse问题解决如下
// thread my_thread1((background_task())); // 多组括号
// my_thread1.join();
// thread my_thread2{background_task()}; // 新的初始化语法
// my_thread2.join();
// thread myThread([](){
// cout<<"ok"<<endl;
// });
// myThread.join();
// 后台运行线程
std::thread t(f);
t.detach();
assert(!t.joinable());
return 0;
}

View File

@@ -0,0 +1,27 @@
//
// Created by light on 19-11-5.
//
#include <iostream>
#include <thread>
using namespace std;
class X {
public:
void do_length_work() {};
};
void process_big_object(std::unique_ptr<X>);
int main() {
X my_x;
thread t(&X::do_length_work, &my_x); // 1
std::unique_ptr<X> p(new X);
p->do_length_work();
std::thread tt(process_big_object,std::move(p));
//std::thread实例的可移动且不可复制性。不可复制保性证了在同一时间点
// 一个std::thread实例只能关联一个执行线程可移动性使得程序员可以自己决定哪个实例拥有实际执行线程的所有权。
return 0;
}

View File

@@ -0,0 +1,78 @@
//
// Created by light on 19-11-5.
//
#include <iostream>
#include <thread>
#include <unistd.h>
#include <vector>
#include <algorithm>
using namespace std;
void some_function() {}
void some_other_function() {}
// std::thread不支持拷贝语义。
// std::thread支持移动语义。
// scoped_thread实例
void do_something(int i) {
cout << i << endl;
}
struct func {
int &i;
func(int &i_) : i(i_) {}
void operator()() {
for (unsigned j = 0; j < 1000000; ++j) {
do_something(i);
}
}
};
class scoped_thread {
std::thread t;
public:
explicit scoped_thread(std::thread t_) : // 1
t(std::move(t_)) {
if (!t.joinable()) // 2
throw std::logic_error("No thread");
}
~scoped_thread() {
t.join(); // 3
}
scoped_thread(scoped_thread const &) = delete;
scoped_thread &operator=(scoped_thread const &) = delete;
};
void do_work(unsigned id) {}
void f() {
std::vector<std::thread> threads;
for (unsigned i = 0; i < 20; ++i) {
threads.push_back(std::thread(do_work, i)); // 产生线程
}
std::for_each(threads.begin(), threads.end(),
std::mem_fn(&std::thread::join)); // 对每个线程调用join()
}
int main() {
// std::thread t1(some_function); // 构造一个thread对象t1
// std::thread t2 = std::move(t1); // 把t1 move给另外一个thread对象t2t1不再管理之前的线程了。
// // 这句不需要std::move()从临时变量进行移动是自动和隐式的。调用的是operator=(std::thread&&)
// t1 = std::thread(some_other_function);
// std::thread t3;
// t3 = std::move(t2); // 把t2 move给t3
// // 把t3 move给t1非法。因为`t1`已经有了一个相关的线程,会调用`std::terminate()`来终止程序。
// t1 = std::move(t3);
f();
return 0;
}

View File

@@ -0,0 +1,60 @@
//
// Created by light on 19-11-5.
//
#include <iostream>
#include <algorithm>
#include <thread>
#include <vector>
#include <numeric>
using namespace std;
//使得每个线程具有最小数目的元素以避免过多的线程开销
template<typename Iterator, typename T>
struct accumulate_block {
void operator()(Iterator first, Iterator last, T &result) {
result = std::accumulate(first, last, result);
}
};
template<typename Iterator, typename T>
T parallel_accumlate(Iterator first, Iterator last, T init) {
unsigned long const length = std::distance(first, last);
if (!length)
return init;
unsigned long const min_per_thread = 25;
unsigned long const max_threads = (length + min_per_thread - 1) / min_per_thread;
cout<<max_threads<<endl;
unsigned long const hardware_threads = std::thread::hardware_concurrency();
cout<<hardware_threads<<endl;
unsigned long const num_threads = std::min(hardware_threads != 0 ? hardware_threads : 2, max_threads);
cout<<num_threads<<endl;
unsigned long const block_size = length / num_threads;
cout<<block_size<<endl;
std::vector<T> results(num_threads);
std::vector<std::thread> threads(num_threads - 1);
Iterator block_start = first;
for (unsigned long i = 0; i < (num_threads - 1); ++i) {
Iterator block_end = block_start;
std::advance(block_end, block_size);
threads[i] = std::thread(accumulate_block<Iterator, T>(), block_start, block_end, std::ref(results[i]));
block_start = block_end;
}
accumulate_block<Iterator, T>()(block_start, last, results[num_threads - 1]);
std::for_each(threads.begin(), threads.end(), std::mem_fn(&std::thread::join));
return std::accumulate(results.begin(), results.end(), init);
}
int main() {
vector<int> v{3,4,5,6};
int res=0;
cout<<parallel_accumlate(v.begin(),v.end(),res);
return 0;
}

View File

@@ -0,0 +1,34 @@
//
// Created by light on 19-11-5.
//
#include <iostream>
#include <thread>
using namespace std;
// 线程的通用标识符
std::thread::id master_thread;
void do_master_thread_work() {
cout << "master" << endl;
}
void do_common_work() {
cout << "common" << endl;
}
void some_core_part_of_algorithm() {
if (std::this_thread::get_id() == master_thread) {
do_master_thread_work();
}
do_common_work();
}
int main() {
std::cout << std::this_thread::get_id() << endl;
thread t(some_core_part_of_algorithm);
t.join();
return 0;
}