1
0
This commit is contained in:
2025-10-10 02:17:07 +02:00
commit ee9f86004b
48 changed files with 2161 additions and 0 deletions

31
P5/P5.sln Normal file
View File

@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.11.35327.3
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "smt4497-P5", "smt4497-P5\smt4497-P5.vcxproj", "{B823A30A-0FA2-4E84-99F8-5A5B241871FD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B823A30A-0FA2-4E84-99F8-5A5B241871FD}.Debug|x64.ActiveCfg = Debug|x64
{B823A30A-0FA2-4E84-99F8-5A5B241871FD}.Debug|x64.Build.0 = Debug|x64
{B823A30A-0FA2-4E84-99F8-5A5B241871FD}.Debug|x86.ActiveCfg = Debug|Win32
{B823A30A-0FA2-4E84-99F8-5A5B241871FD}.Debug|x86.Build.0 = Debug|Win32
{B823A30A-0FA2-4E84-99F8-5A5B241871FD}.Release|x64.ActiveCfg = Release|x64
{B823A30A-0FA2-4E84-99F8-5A5B241871FD}.Release|x64.Build.0 = Release|x64
{B823A30A-0FA2-4E84-99F8-5A5B241871FD}.Release|x86.ActiveCfg = Release|Win32
{B823A30A-0FA2-4E84-99F8-5A5B241871FD}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A5FD762A-271B-490E-9D8C-A719EDCC3EF4}
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,129 @@
#include "smt4497-p5.h"
#define INCREMENTS (100*1000)
#define THREADS 20
#define ITERATIONS 100
using namespace ss;
static HANDLE mutex;
static HANDLE* turn_events;
static Dbg _dbg(true);
int main(int argc, char* argv[], char* envp[])
{
setlocale(LC_ALL, "Spanish");
/*printf("\nEmpezando sin mutex...\n");
for (int i = 1; i <= ITERATIONS; i++)
{
test_simple_non_reentrant_thread(INCREMENTS, THREADS);
}
printf("\nEmpezando con mutex...\n");
for (int i = 1; i <= ITERATIONS; i++)
{
test_simple_non_reentrant_thread_mutex(INCREMENTS, THREADS);
}*/
test_thread_turns(THREADS);
printf("\nPor favor pulse ENTER para terminar...");
(void)getchar();
return 0;
}
VOID simple_non_reentrant_thread(int times, int* pCounter)
{
for (int i = 0; i < times; i++)
{
*pCounter += times + 1;
*pCounter -= times + 1;
}
}
static BOOL test_simple_non_reentrant_thread(int increments, int threads_number)
{
int counter = 0;
_dbg.CronoInicio();
thread* threads = new thread[threads_number];
for (int i = 0; i < threads_number; i++)
{
threads[i] = thread(simple_non_reentrant_thread, increments, &counter);
_dbg.CheckError(threads[i].native_handle() == NULL, "No se pudo crear el hilo");
}
for (int i = 0; i < threads_number; i++)
threads[i].join();
double secs = _dbg.CronoLee();
printf("[SIN MUTEX] Contador vale: %d (calculado con %d hilos, en %f segundos)\n", counter, threads_number, secs);
return counter == 0;
}
VOID simple_non_reentrant_thread_mutex(int times, int* pCounter)
{
DWORD res = WaitForSingleObject(mutex, INFINITE); // critical section begins
_dbg.CheckError(res != 0, 2, "Error en el bloqueo\n");
for (int i = 0; i < times; i++)
{
*pCounter += times + 1;
*pCounter -= times + 1;
}
_dbg.CheckError(ReleaseMutex(mutex) == FALSE, 1, "Error liberando mutex\n"); // critical section ends
}
static BOOL test_simple_non_reentrant_thread_mutex(int increments, int threads_number)
{
int counter = 0;
_dbg.CronoInicio();
mutex = CreateMutexA(NULL, FALSE, NULL);
_dbg.CheckError(mutex == NULL, 1, "Error creando el mutex\n");
thread* threads = new thread[threads_number];
for (int i = 0; i < threads_number; i++)
{
threads[i] = thread(simple_non_reentrant_thread_mutex, increments, &counter);
_dbg.CheckError(threads[i].native_handle() == NULL, "No se pudo crear el hilo");
}
for (int i = 0; i < threads_number; i++)
threads[i].join();
double secs = _dbg.CronoLee();
printf("[CON MUTEX] Contador vale: %d (calculado con %d hilos, en %f segundos)\n", counter, threads_number, secs);
return counter == 0;
}
static VOID thread_turns(int thread_number)
{
WaitForSingleObject(turn_events[thread_number], INFINITE);
for (int i = 0; i < thread_number; i++)
printf(" ");
printf("hilo %i iniciado >>", thread_number);
for (int i = 0; i < thread_number; i++)
printf(" ");
printf("<< hilo %i finalizado\n", thread_number);
SetEvent(turn_events[thread_number + 1]);
}
static VOID test_thread_turns(int thread_numbers)
{
turn_events = new HANDLE[thread_numbers + 1];
for (int i = 0; i <= thread_numbers; i++)
{
turn_events[i] = CreateEventA(NULL, FALSE, FALSE, NULL);
_dbg.CheckError(turn_events[i] == NULL, 1, "Error creando el evento\n");
}
thread* threads = new thread[thread_numbers];
for (int i = 0; i < thread_numbers; i++)
{
threads[i] = thread(thread_turns, i);
_dbg.CheckError(threads[i].native_handle() == NULL, "No se pudo crear el hilo");
}
SetEvent(turn_events[0]);
for (int i = 0; i < thread_numbers; i++)
threads[i].join();
for (int i = 0; i <= thread_numbers; i++)
CloseHandle(turn_events[i]);
delete[] turn_events;
delete[] threads;
}

View File

@@ -0,0 +1,154 @@
<?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>17.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{b823a30a-0fa2-4e84-99f8-5a5b241871fd}</ProjectGuid>
<RootNamespace>smt4497P5</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>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</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>
<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 Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<IncludePath>$(VC_IncludePath);$(WindowsSDK_IncludePath);$(AAComm)inc</IncludePath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<IncludePath>$(VC_IncludePath);$(WindowsSDK_IncludePath);$(AAComm)inc</IncludePath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<IncludePath>$(VC_IncludePath);$(WindowsSDK_IncludePath);$(AAComm)inc</IncludePath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<IncludePath>$(VC_IncludePath);$(WindowsSDK_IncludePath);$(AAComm)inc</IncludePath>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>$(AAComm)lib\sslib\x86\SSLib.lib;$(CoreLibraryDependencies);%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>$(AAComm)lib\sslib\x86\SSLib.lib;$(CoreLibraryDependencies);%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>$(AAComm)lib\sslib\x64\SSLib.lib;$(CoreLibraryDependencies);%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>$(AAComm)lib\sslib\x64\SSLib.lib;$(CoreLibraryDependencies);%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="smt4497-P5.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="smt4497-p5.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ClCompile Include="smt4497-P5.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="smt4497-p5.h" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LocalDebuggerEnvironment>PATH=$(AAComm)bin\sslib\$(PlatformTarget);%PATH%$(LocalDebuggerEnvironment)</LocalDebuggerEnvironment>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LocalDebuggerEnvironment>PATH=$(AAComm)bin\sslib\$(PlatformTarget);%PATH%$(LocalDebuggerEnvironment)</LocalDebuggerEnvironment>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LocalDebuggerEnvironment>PATH=$(AAComm)bin\sslib\$(PlatformTarget);%PATH%$(LocalDebuggerEnvironment)</LocalDebuggerEnvironment>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LocalDebuggerEnvironment>PATH=$(AAComm)bin\sslib\$(PlatformTarget);%PATH%$(LocalDebuggerEnvironment)</LocalDebuggerEnvironment>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,14 @@
#pragma once
#include <windows.h>
#include <thread>
#include <stdio.h>
#include <sslib\SSLib.h>
#include <locale.h>
VOID simple_non_reentrant_thread(int times, int* pCounter);
VOID simple_non_reentrant_thread_mutex(int times, int* pCounter);
static BOOL test_simple_non_reentrant_thread(int increments, int threads_number);
static BOOL test_simple_non_reentrant_thread_mutex(int increments, int threads_number);
static VOID thread_turns(int thread_number);
static VOID test_thread_turns(int thread_numbers);