Commit b60eed77 authored by captainwong's avatar captainwong

test mutex

parent e1fe42b3
...@@ -21,11 +21,28 @@ namespace jlib ...@@ -21,11 +21,28 @@ namespace jlib
using namespace date; using namespace date;
using Timestamp = date::sys_time<std::chrono::microseconds>; using Timestamp = date::sys_time<std::chrono::microseconds>;
static constexpr Timestamp nowTimestamp() { return floor<std::chrono::microseconds>(std::chrono::system_clock::now()); } //! now
inline constexpr Timestamp nowTimestamp() { return floor<std::chrono::microseconds>(std::chrono::system_clock::now()); }
//! (first - second) duration in micro-seconds
inline constexpr long long timeDifference(const Timestamp& first, const Timestamp& second) {
return floor<std::chrono::microseconds>(first - second).count();
}
//! (nowTimestamp - t) duration in micro-seconds
inline constexpr long long timeDifference(const Timestamp& t) {
return floor<std::chrono::microseconds>(nowTimestamp() - t).count();
}
//! (first - second) duration in seconds(double)
inline constexpr double timeDifferenceInS(const Timestamp& first, const Timestamp& second) {
return static_cast<double>(floor<std::chrono::microseconds>(first - second).count()) / 1000000.0;
}
//! (nowTimestamp - t) duration in seconds(double)
inline constexpr double timeDifferenceInS(const Timestamp& t) {
return static_cast<double>(floor<std::chrono::microseconds>(nowTimestamp() - t).count()) / 1000000.0;
}
//class ENABLE_EBO Timestamp : public jlib::copyable //class ENABLE_EBO Timestamp : public jlib::copyable
......
...@@ -245,6 +245,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test_atomic", "test_atomic\ ...@@ -245,6 +245,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test_atomic", "test_atomic\
EndProject EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "logstream_unittest", "logstream_unittest\logstream_unittest.vcxproj", "{B8F4AB37-A049-443B-8B75-4D0C831B234B}" Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "logstream_unittest", "logstream_unittest\logstream_unittest.vcxproj", "{B8F4AB37-A049-443B-8B75-4D0C831B234B}"
EndProject EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test_mutex", "test_mutex\test_mutex.vcxproj", "{F1EFC4C7-AF79-4A65-9FE1-7C5305DC2E6A}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64 Debug|x64 = Debug|x64
...@@ -427,6 +429,14 @@ Global ...@@ -427,6 +429,14 @@ Global
{B8F4AB37-A049-443B-8B75-4D0C831B234B}.Release|x64.Build.0 = Release|x64 {B8F4AB37-A049-443B-8B75-4D0C831B234B}.Release|x64.Build.0 = Release|x64
{B8F4AB37-A049-443B-8B75-4D0C831B234B}.Release|x86.ActiveCfg = Release|Win32 {B8F4AB37-A049-443B-8B75-4D0C831B234B}.Release|x86.ActiveCfg = Release|Win32
{B8F4AB37-A049-443B-8B75-4D0C831B234B}.Release|x86.Build.0 = Release|Win32 {B8F4AB37-A049-443B-8B75-4D0C831B234B}.Release|x86.Build.0 = Release|Win32
{F1EFC4C7-AF79-4A65-9FE1-7C5305DC2E6A}.Debug|x64.ActiveCfg = Debug|x64
{F1EFC4C7-AF79-4A65-9FE1-7C5305DC2E6A}.Debug|x64.Build.0 = Debug|x64
{F1EFC4C7-AF79-4A65-9FE1-7C5305DC2E6A}.Debug|x86.ActiveCfg = Debug|Win32
{F1EFC4C7-AF79-4A65-9FE1-7C5305DC2E6A}.Debug|x86.Build.0 = Debug|Win32
{F1EFC4C7-AF79-4A65-9FE1-7C5305DC2E6A}.Release|x64.ActiveCfg = Release|x64
{F1EFC4C7-AF79-4A65-9FE1-7C5305DC2E6A}.Release|x64.Build.0 = Release|x64
{F1EFC4C7-AF79-4A65-9FE1-7C5305DC2E6A}.Release|x86.ActiveCfg = Release|Win32
{F1EFC4C7-AF79-4A65-9FE1-7C5305DC2E6A}.Release|x86.Build.0 = Release|Win32
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
...@@ -475,6 +485,7 @@ Global ...@@ -475,6 +485,7 @@ Global
{7DC0D84B-7F96-4BB9-9B8A-D86B7B031E61} = {D9BC4E5B-7E8F-4C86-BF15-CCB75CBC256F} {7DC0D84B-7F96-4BB9-9B8A-D86B7B031E61} = {D9BC4E5B-7E8F-4C86-BF15-CCB75CBC256F}
{58DA7AE8-9975-4F28-A1BF-44A7C01B6F7F} = {D9BC4E5B-7E8F-4C86-BF15-CCB75CBC256F} {58DA7AE8-9975-4F28-A1BF-44A7C01B6F7F} = {D9BC4E5B-7E8F-4C86-BF15-CCB75CBC256F}
{B8F4AB37-A049-443B-8B75-4D0C831B234B} = {D9BC4E5B-7E8F-4C86-BF15-CCB75CBC256F} {B8F4AB37-A049-443B-8B75-4D0C831B234B} = {D9BC4E5B-7E8F-4C86-BF15-CCB75CBC256F}
{F1EFC4C7-AF79-4A65-9FE1-7C5305DC2E6A} = {D9BC4E5B-7E8F-4C86-BF15-CCB75CBC256F}
EndGlobalSection EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A8EBEA58-739C-4DED-99C0-239779F57D5D} SolutionGuid = {A8EBEA58-739C-4DED-99C0-239779F57D5D}
......
#include "../../jlib/base/timestamp.h"
#include <mutex>
#include <thread>
#include <vector>
#include <stdio.h>
#include <assert.h>
using namespace jlib;
using namespace std;
mutex g_mutex;
vector<int> g_vec;
const int kCount = 10 * 1000 * 1000;
void func() {
for (int i = 0; i < kCount; i++) {
lock_guard<mutex> lock(g_mutex);
g_vec.push_back(i);
}
}
int g_count = 0;
int foo() {
lock_guard<mutex> lock(g_mutex);
++g_count;
return 0;
}
int main()
{
assert(foo() == 0);
if (g_count != 1) {
printf("MCHECK calls twice.\n");
abort();
}
const int kMaxThreads = 8;
g_vec.reserve(kMaxThreads * kCount);
Timestamp start(nowTimestamp());
for (int i = 0; i < kCount; ++i) {
g_vec.push_back(i);
}
printf("single thread without lock %lf\n", timeDifferenceInS(start));
start = nowTimestamp();
func();
printf("single thread with lock %lf\n", timeDifferenceInS(start));
for (int nthreads = 1; nthreads < kMaxThreads; ++nthreads) {
std::vector<thread> threads;
g_vec.clear();
start = nowTimestamp();
for (int i = 0; i < nthreads; ++i) {
threads.emplace_back(func);
}
for (auto& t : threads) {
t.join();
}
printf("%d thread(s) with lock %lf\n", nthreads, timeDifferenceInS(start));
}
}
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" 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>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<ProjectGuid>{F1EFC4C7-AF79-4A65-9FE1-7C5305DC2E6A}</ProjectGuid>
<RootNamespace>testmutex</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</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>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<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|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="test_mutex.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<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="test_mutex.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
</Project>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment