Skip to content

Commit 819e787

Browse files
committed
Add project files.
1 parent 4d2e8e0 commit 819e787

File tree

6 files changed

+225
-0
lines changed

6 files changed

+225
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
bin/*
2+
obj/*
3+
.vs/*

MultiThreadTutorial.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.0.31606.5
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "MultiThreadTutorial", "MultiThreadTutorial.vcxproj", "{6DB480D1-A8F9-4791-A730-11887F7DCD12}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|x64 = Debug|x64
11+
Release|x64 = Release|x64
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{6DB480D1-A8F9-4791-A730-11887F7DCD12}.Debug|x64.ActiveCfg = Debug|x64
15+
{6DB480D1-A8F9-4791-A730-11887F7DCD12}.Debug|x64.Build.0 = Debug|x64
16+
{6DB480D1-A8F9-4791-A730-11887F7DCD12}.Release|x64.ActiveCfg = Release|x64
17+
{6DB480D1-A8F9-4791-A730-11887F7DCD12}.Release|x64.Build.0 = Release|x64
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {E77BE9AB-6C3E-400E-949B-4B569E0196CB}
24+
EndGlobalSection
25+
EndGlobal

MultiThreadTutorial.vcxproj

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup Label="ProjectConfigurations">
4+
<ProjectConfiguration Include="Debug|x64">
5+
<Configuration>Debug</Configuration>
6+
<Platform>x64</Platform>
7+
</ProjectConfiguration>
8+
<ProjectConfiguration Include="Release|x64">
9+
<Configuration>Release</Configuration>
10+
<Platform>x64</Platform>
11+
</ProjectConfiguration>
12+
</ItemGroup>
13+
<PropertyGroup Label="Globals">
14+
<VCProjectVersion>16.0</VCProjectVersion>
15+
<Keyword>Win32Proj</Keyword>
16+
<ProjectGuid>{6db480d1-a8f9-4791-a730-11887f7dcd12}</ProjectGuid>
17+
<RootNamespace>MultiThreadTutorial</RootNamespace>
18+
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
19+
</PropertyGroup>
20+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
21+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
22+
<ConfigurationType>Application</ConfigurationType>
23+
<UseDebugLibraries>true</UseDebugLibraries>
24+
<PlatformToolset>v143</PlatformToolset>
25+
<CharacterSet>Unicode</CharacterSet>
26+
</PropertyGroup>
27+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
28+
<ConfigurationType>Application</ConfigurationType>
29+
<UseDebugLibraries>false</UseDebugLibraries>
30+
<PlatformToolset>v143</PlatformToolset>
31+
<WholeProgramOptimization>true</WholeProgramOptimization>
32+
<CharacterSet>Unicode</CharacterSet>
33+
</PropertyGroup>
34+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
35+
<ImportGroup Label="ExtensionSettings">
36+
</ImportGroup>
37+
<ImportGroup Label="Shared">
38+
</ImportGroup>
39+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
40+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
41+
</ImportGroup>
42+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
43+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
44+
</ImportGroup>
45+
<PropertyGroup Label="UserMacros" />
46+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
47+
<LinkIncremental>true</LinkIncremental>
48+
<OutDir>$(SolutionDir)bin\</OutDir>
49+
<IntDir>$(SolutionDir)obj\</IntDir>
50+
</PropertyGroup>
51+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
52+
<LinkIncremental>false</LinkIncremental>
53+
<OutDir>$(SolutionDir)bin\</OutDir>
54+
<IntDir>$(SolutionDir)obj\</IntDir>
55+
</PropertyGroup>
56+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
57+
<ClCompile>
58+
<WarningLevel>Level3</WarningLevel>
59+
<SDLCheck>true</SDLCheck>
60+
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
61+
<ConformanceMode>true</ConformanceMode>
62+
<LanguageStandard>stdcpp17</LanguageStandard>
63+
</ClCompile>
64+
<Link>
65+
<SubSystem>Console</SubSystem>
66+
<GenerateDebugInformation>true</GenerateDebugInformation>
67+
</Link>
68+
</ItemDefinitionGroup>
69+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
70+
<ClCompile>
71+
<WarningLevel>Level3</WarningLevel>
72+
<FunctionLevelLinking>true</FunctionLevelLinking>
73+
<IntrinsicFunctions>true</IntrinsicFunctions>
74+
<SDLCheck>true</SDLCheck>
75+
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
76+
<ConformanceMode>true</ConformanceMode>
77+
<LanguageStandard>stdcpp17</LanguageStandard>
78+
</ClCompile>
79+
<Link>
80+
<SubSystem>Console</SubSystem>
81+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
82+
<OptimizeReferences>true</OptimizeReferences>
83+
<GenerateDebugInformation>true</GenerateDebugInformation>
84+
</Link>
85+
</ItemDefinitionGroup>
86+
<ItemGroup>
87+
<ClCompile Include="src\main.cpp" />
88+
</ItemGroup>
89+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
90+
<ImportGroup Label="ExtensionTargets">
91+
</ImportGroup>
92+
</Project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup>
4+
<Filter Include="Source Files">
5+
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
6+
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
7+
</Filter>
8+
<Filter Include="Header Files">
9+
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
10+
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
11+
</Filter>
12+
<Filter Include="Resource Files">
13+
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
14+
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
15+
</Filter>
16+
</ItemGroup>
17+
<ItemGroup>
18+
<ClCompile Include="src\main.cpp">
19+
<Filter>Source Files</Filter>
20+
</ClCompile>
21+
</ItemGroup>
22+
</Project>

MultiThreadTutorial.vcxproj.user

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<ShowAllFiles>true</ShowAllFiles>
5+
</PropertyGroup>
6+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
7+
<LocalDebuggerWorkingDirectory>$(TargetDir)</LocalDebuggerWorkingDirectory>
8+
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
9+
</PropertyGroup>
10+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
11+
<LocalDebuggerWorkingDirectory>$(TargetDir)</LocalDebuggerWorkingDirectory>
12+
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
13+
</PropertyGroup>
14+
</Project>

src/main.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#define _USE_MATH_DEFINES
2+
#include <iostream>
3+
#include <chrono>
4+
#include <cmath>
5+
#include <thread>
6+
#include <vector>
7+
#include <cassert>
8+
9+
// unsigned int doneWork = 0;
10+
11+
double work(unsigned int idx, double input) {
12+
// doneWork = doneWork + 1;
13+
return abs(sin(input * idx)) * input;
14+
}
15+
16+
void doWork(double* data, unsigned int count, unsigned int offset = 0) {
17+
for (unsigned int i = offset; i < count; i++) {
18+
data[i] = work(i, data[i]);
19+
}
20+
}
21+
22+
void doWorkMT(double* data, unsigned int count, unsigned int threadCount) {
23+
// Work per thread
24+
unsigned int wpt = count / threadCount;
25+
26+
// Schedule word
27+
std::vector<std::thread> threads;
28+
for (unsigned int i = 0; i < threadCount; i++) {
29+
// Create thread
30+
threads.push_back(std::thread(&doWork, data, wpt, i * wpt));
31+
}
32+
33+
// Wait for threads to finish
34+
for (auto it = threads.begin(); it != threads.end(); it++) {
35+
it->join();
36+
}
37+
}
38+
39+
int main() {
40+
// Heavy work count
41+
const unsigned int workCount = 1024 * 1024 * 32;
42+
// Genneratew work
43+
std::cout << "=== GENNERATE WORK ===" << std::endl;
44+
double* workData = (double*) malloc(sizeof(double) * workCount);
45+
std::srand(time(0));
46+
for (unsigned int i = 0; i < workCount; i++) {
47+
workData[i] = std::rand() / (double)RAND_MAX;
48+
}
49+
50+
// Work and time
51+
std::cout << "=== START WORK ===" << std::endl;
52+
auto tStart = std::chrono::steady_clock::now();
53+
doWorkMT(workData, workCount, 8);
54+
auto tStop = std::chrono::steady_clock::now();
55+
56+
// Print timing result
57+
std::cout << std::endl << "Timings: " << std::endl
58+
<< "NS: " << std::chrono::duration_cast<std::chrono::nanoseconds>(tStop - tStart).count() << std::endl
59+
<< "US: " << std::chrono::duration_cast<std::chrono::microseconds>(tStop - tStart).count() << std::endl
60+
<< "MS: " << std::chrono::duration_cast<std::chrono::milliseconds>(tStop - tStart).count() << std::endl
61+
<< " S: " << std::chrono::duration_cast<std::chrono::seconds>(tStop - tStart).count() << std::endl;
62+
63+
// Assert my work
64+
// assert(doneCount == workCount && "Work success");
65+
66+
// Free
67+
free(workData);
68+
return 0;
69+
}

0 commit comments

Comments
 (0)