Skip to content

Commit 811679a

Browse files
committed
WinAPI Threads :-)
1 parent 5db7fab commit 811679a

File tree

5 files changed

+169
-0
lines changed

5 files changed

+169
-0
lines changed

MultiThreadTutorial.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,18 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "MultiThreadTutorial", "Mult
77
EndProject
88
Global
99
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug WINAPI|x64 = Debug WINAPI|x64
1011
Debug|x64 = Debug|x64
12+
Release WINAPI|x64 = Release WINAPI|x64
1113
Release|x64 = Release|x64
1214
EndGlobalSection
1315
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{6DB480D1-A8F9-4791-A730-11887F7DCD12}.Debug WINAPI|x64.ActiveCfg = Debug WINAPI|x64
17+
{6DB480D1-A8F9-4791-A730-11887F7DCD12}.Debug WINAPI|x64.Build.0 = Debug WINAPI|x64
1418
{6DB480D1-A8F9-4791-A730-11887F7DCD12}.Debug|x64.ActiveCfg = Debug|x64
1519
{6DB480D1-A8F9-4791-A730-11887F7DCD12}.Debug|x64.Build.0 = Debug|x64
20+
{6DB480D1-A8F9-4791-A730-11887F7DCD12}.Release WINAPI|x64.ActiveCfg = Release WINAPI|x64
21+
{6DB480D1-A8F9-4791-A730-11887F7DCD12}.Release WINAPI|x64.Build.0 = Release WINAPI|x64
1622
{6DB480D1-A8F9-4791-A730-11887F7DCD12}.Release|x64.ActiveCfg = Release|x64
1723
{6DB480D1-A8F9-4791-A730-11887F7DCD12}.Release|x64.Build.0 = Release|x64
1824
EndGlobalSection

MultiThreadTutorial.vcxproj

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<ItemGroup Label="ProjectConfigurations">
4+
<ProjectConfiguration Include="Debug WINAPI|x64">
5+
<Configuration>Debug WINAPI</Configuration>
6+
<Platform>x64</Platform>
7+
</ProjectConfiguration>
48
<ProjectConfiguration Include="Debug|x64">
59
<Configuration>Debug</Configuration>
610
<Platform>x64</Platform>
711
</ProjectConfiguration>
12+
<ProjectConfiguration Include="Release WINAPI|x64">
13+
<Configuration>Release WINAPI</Configuration>
14+
<Platform>x64</Platform>
15+
</ProjectConfiguration>
816
<ProjectConfiguration Include="Release|x64">
917
<Configuration>Release</Configuration>
1018
<Platform>x64</Platform>
@@ -24,13 +32,26 @@
2432
<PlatformToolset>v143</PlatformToolset>
2533
<CharacterSet>Unicode</CharacterSet>
2634
</PropertyGroup>
35+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug WINAPI|x64'" Label="Configuration">
36+
<ConfigurationType>Application</ConfigurationType>
37+
<UseDebugLibraries>true</UseDebugLibraries>
38+
<PlatformToolset>v143</PlatformToolset>
39+
<CharacterSet>Unicode</CharacterSet>
40+
</PropertyGroup>
2741
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
2842
<ConfigurationType>Application</ConfigurationType>
2943
<UseDebugLibraries>false</UseDebugLibraries>
3044
<PlatformToolset>v143</PlatformToolset>
3145
<WholeProgramOptimization>true</WholeProgramOptimization>
3246
<CharacterSet>Unicode</CharacterSet>
3347
</PropertyGroup>
48+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release WINAPI|x64'" Label="Configuration">
49+
<ConfigurationType>Application</ConfigurationType>
50+
<UseDebugLibraries>false</UseDebugLibraries>
51+
<PlatformToolset>v143</PlatformToolset>
52+
<WholeProgramOptimization>true</WholeProgramOptimization>
53+
<CharacterSet>Unicode</CharacterSet>
54+
</PropertyGroup>
3455
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
3556
<ImportGroup Label="ExtensionSettings">
3657
</ImportGroup>
@@ -39,20 +60,36 @@
3960
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
4061
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
4162
</ImportGroup>
63+
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug WINAPI|x64'" Label="PropertySheets">
64+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
65+
</ImportGroup>
4266
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
4367
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
4468
</ImportGroup>
69+
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release WINAPI|x64'" Label="PropertySheets">
70+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
71+
</ImportGroup>
4572
<PropertyGroup Label="UserMacros" />
4673
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
4774
<LinkIncremental>true</LinkIncremental>
4875
<OutDir>$(SolutionDir)bin\</OutDir>
4976
<IntDir>$(SolutionDir)obj\</IntDir>
5077
</PropertyGroup>
78+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug WINAPI|x64'">
79+
<LinkIncremental>true</LinkIncremental>
80+
<OutDir>$(SolutionDir)bin\</OutDir>
81+
<IntDir>$(SolutionDir)obj\</IntDir>
82+
</PropertyGroup>
5183
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
5284
<LinkIncremental>false</LinkIncremental>
5385
<OutDir>$(SolutionDir)bin\</OutDir>
5486
<IntDir>$(SolutionDir)obj\</IntDir>
5587
</PropertyGroup>
88+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release WINAPI|x64'">
89+
<LinkIncremental>false</LinkIncremental>
90+
<OutDir>$(SolutionDir)bin\</OutDir>
91+
<IntDir>$(SolutionDir)obj\</IntDir>
92+
</PropertyGroup>
5693
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
5794
<ClCompile>
5895
<WarningLevel>Level3</WarningLevel>
@@ -66,6 +103,19 @@
66103
<GenerateDebugInformation>true</GenerateDebugInformation>
67104
</Link>
68105
</ItemDefinitionGroup>
106+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug WINAPI|x64'">
107+
<ClCompile>
108+
<WarningLevel>Level3</WarningLevel>
109+
<SDLCheck>true</SDLCheck>
110+
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
111+
<ConformanceMode>true</ConformanceMode>
112+
<LanguageStandard>stdcpp17</LanguageStandard>
113+
</ClCompile>
114+
<Link>
115+
<SubSystem>Windows</SubSystem>
116+
<GenerateDebugInformation>true</GenerateDebugInformation>
117+
</Link>
118+
</ItemDefinitionGroup>
69119
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
70120
<ClCompile>
71121
<WarningLevel>Level3</WarningLevel>
@@ -83,8 +133,26 @@
83133
<GenerateDebugInformation>true</GenerateDebugInformation>
84134
</Link>
85135
</ItemDefinitionGroup>
136+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release WINAPI|x64'">
137+
<ClCompile>
138+
<WarningLevel>Level3</WarningLevel>
139+
<FunctionLevelLinking>true</FunctionLevelLinking>
140+
<IntrinsicFunctions>true</IntrinsicFunctions>
141+
<SDLCheck>true</SDLCheck>
142+
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
143+
<ConformanceMode>true</ConformanceMode>
144+
<LanguageStandard>stdcpp17</LanguageStandard>
145+
</ClCompile>
146+
<Link>
147+
<SubSystem>Windows</SubSystem>
148+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
149+
<OptimizeReferences>true</OptimizeReferences>
150+
<GenerateDebugInformation>true</GenerateDebugInformation>
151+
</Link>
152+
</ItemDefinitionGroup>
86153
<ItemGroup>
87154
<ClCompile Include="src\main.cpp" />
155+
<ClCompile Include="src\wWinMain.cpp" />
88156
</ItemGroup>
89157
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
90158
<ImportGroup Label="ExtensionTargets">

MultiThreadTutorial.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,8 @@
1818
<ClCompile Include="src\main.cpp">
1919
<Filter>Source Files</Filter>
2020
</ClCompile>
21+
<ClCompile Include="src\wWinMain.cpp">
22+
<Filter>Source Files</Filter>
23+
</ClCompile>
2124
</ItemGroup>
2225
</Project>

MultiThreadTutorial.vcxproj.user

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,16 @@
77
<LocalDebuggerWorkingDirectory>$(TargetDir)</LocalDebuggerWorkingDirectory>
88
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
99
</PropertyGroup>
10+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug WINAPI|x64'">
11+
<LocalDebuggerWorkingDirectory>$(TargetDir)</LocalDebuggerWorkingDirectory>
12+
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
13+
</PropertyGroup>
1014
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
1115
<LocalDebuggerWorkingDirectory>$(TargetDir)</LocalDebuggerWorkingDirectory>
1216
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
1317
</PropertyGroup>
18+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release WINAPI|x64'">
19+
<LocalDebuggerWorkingDirectory>$(TargetDir)</LocalDebuggerWorkingDirectory>
20+
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
21+
</PropertyGroup>
1422
</Project>

src/wWinMain.cpp

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include <Windows.h>
2+
#include <string>
3+
#include <sstream>
4+
5+
struct THREAD_ARGS {
6+
// Input
7+
std::wstring message;
8+
DWORD threadId;
9+
10+
// Output
11+
BYTE blob = 0;
12+
};
13+
14+
DWORD WINAPI ThreadMain(THREAD_ARGS* args) {
15+
WCHAR* threadName = (WCHAR*)L"";
16+
// Get thread description
17+
GetThreadDescription(GetCurrentThread(), &threadName);
18+
19+
// Build message
20+
std::wstringstream wss;
21+
wss << L"Thread ID " << args->threadId << L" (" << threadName << L")";
22+
23+
// Free name
24+
if (wcslen(threadName)) {
25+
LocalFree(threadName);
26+
}
27+
28+
// Display message box
29+
MessageBox(NULL, args->message.c_str(), wss.str().c_str(), MB_OK | MB_ICONINFORMATION);
30+
31+
// Doing dumb calcs
32+
args->blob = args->threadId % 255;
33+
34+
return 0;
35+
}
36+
37+
INT WINAPI wWinMain(HINSTANCE _In_ hInstance, HINSTANCE _In_opt_ hPrevInstance, PWSTR _In_ cmdArgs, INT _In_ cmdShow) {
38+
// Create thread args
39+
THREAD_ARGS args;
40+
args.message = L"Hello I'm a message for the thread";
41+
42+
// Create thread
43+
DWORD threadId = 0;
44+
HANDLE hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&ThreadMain, &args, CREATE_SUSPENDED, &threadId);
45+
if (hThread) {
46+
// Pass on the threads id
47+
args.threadId = threadId;
48+
49+
// Set thread name
50+
SetThreadDescription(hThread, L"MessageBox Thread");
51+
52+
// Set the core for thread
53+
UINT threadIndex = 31;
54+
DWORD_PTR mask = (1ULL << threadIndex);
55+
SetThreadAffinityMask(hThread, mask);
56+
57+
// Run the thread
58+
if (ResumeThread(hThread) == -1) {
59+
return -1;
60+
}
61+
62+
// Wait for the thread
63+
if (WaitForSingleObject(hThread, 10000) == WAIT_TIMEOUT) {
64+
// Abort other thred
65+
TerminateThread(hThread, -1);
66+
67+
// Display message box
68+
MessageBox(NULL, L"MessageBox not closed within 10 seconds", L"ERROR", MB_OK | MB_ICONERROR);
69+
}
70+
71+
// Thread exit message box
72+
DWORD exitCode = 0;
73+
if (GetExitCodeThread(hThread, &exitCode)) {
74+
std::wstringstream wss;
75+
wss << L"Thread finished with code: 0x" << std::hex << exitCode << std::dec << std::endl << L"The blob is " << args.blob;
76+
MessageBox(NULL, wss.str().c_str(), L"FINISH", MB_OK | MB_ICONINFORMATION);
77+
}
78+
79+
// Destroy thread
80+
CloseHandle(hThread);
81+
}
82+
83+
return 0;
84+
}

0 commit comments

Comments
 (0)