Skip to content

Commit

Permalink
只使用标准库建立定时循环的任务
Browse files Browse the repository at this point in the history
  • Loading branch information
niel committed Dec 23, 2020
1 parent 942f9c1 commit 6bbcbcc
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 0 deletions.
6 changes: 6 additions & 0 deletions sensors.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "sensors", "sensors\sensors.
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "demo", "demo\demo.vcxproj", "{7DDEE0AC-2D93-49DC-A46A-A02729D57E8E}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "work", "work\work.vcxproj", "{6A704374-BC4F-45FC-B661-CFB0E6DDFBC0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x86 = Debug|x86
Expand All @@ -21,6 +23,10 @@ Global
{7DDEE0AC-2D93-49DC-A46A-A02729D57E8E}.Debug|x86.Build.0 = Debug|Win32
{7DDEE0AC-2D93-49DC-A46A-A02729D57E8E}.Release|x86.ActiveCfg = Release|Win32
{7DDEE0AC-2D93-49DC-A46A-A02729D57E8E}.Release|x86.Build.0 = Release|Win32
{6A704374-BC4F-45FC-B661-CFB0E6DDFBC0}.Debug|x86.ActiveCfg = Debug|Win32
{6A704374-BC4F-45FC-B661-CFB0E6DDFBC0}.Debug|x86.Build.0 = Debug|Win32
{6A704374-BC4F-45FC-B661-CFB0E6DDFBC0}.Release|x86.ActiveCfg = Release|Win32
{6A704374-BC4F-45FC-B661-CFB0E6DDFBC0}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
74 changes: 74 additions & 0 deletions work/SensorWork.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// SensorWork.cpp : 定义控制台应用程序的入口点。
//

#include <thread>
#include <spdlog/spdlog.h>
using namespace std;

/// <summary>
/// 只使用标准库建立定时循环的任务。
/// 可以 start()/shutdown() 重入,但没有考虑性能
/// </summary>
class SensorWork
{
const std::chrono::seconds INTERVAL_OF_SENSOR = 5s;
public:
SensorWork() = default;
void start()
{
{
std::lock_guard<std::mutex> gd(_mt);
if (_running) return;
_running = true;
}
std::thread th([&]() {
std::unique_lock<std::mutex> lock(_mt);
while (!_cond.wait_for(lock, INTERVAL_OF_SENSOR, [&]() {return !_running; }))
{
spdlog::info("**tick");
}
spdlog::info("**shutdown");
});
std::swap(th, _th);
}
void shutdown()
{
{
std::lock_guard<mutex> gd(_mt);
if (!_running) return;
_running = false;
_cond.notify_one();
}
if (_th.joinable())
{
_th.join();
}
}
~SensorWork()
{
shutdown();
}
private:
std::thread _th;
std::condition_variable _cond;
std::mutex _mt;
bool _running = false;
};

int main()
{
spdlog::info("start");
SensorWork ins;
ins.start();
this_thread::sleep_for(17s);
spdlog::info("suspend");
ins.shutdown();
this_thread::sleep_for(17s);
spdlog::info("contine");
ins.start();
this_thread::sleep_for(17s);
spdlog::info("end");
ins.shutdown();
return 0;
}

89 changes: 89 additions & 0 deletions work/work.vcxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{6A704374-BC4F-45FC-B661-CFB0E6DDFBC0}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>work</RootNamespace>
<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PrecompiledHeader>
</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="SensorWork.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
22 changes: 22 additions & 0 deletions work/work.vcxproj.filters
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="源文件">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="头文件">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions>
</Filter>
<Filter Include="资源文件">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="SensorWork.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
</Project>

0 comments on commit 6bbcbcc

Please sign in to comment.