Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

threads-JoshuaGuerrero-tomas vogel- bruno barraud #11

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ClaseHilos/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
//_5Mutex.Excecute();

//6- Uso de lock
_6lock.Excecute();

//_6lock.Excecute();
Solution.Excecute();


143 changes: 105 additions & 38 deletions ClaseHilos/solution.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
using System;
using System.Collections.Generic;
using System.Threading;

namespace ClaseHilos
{
internal class Producto
{
public string Nombre { get; set; }
public decimal PrecioUnitarioDolares { get; set; }
public int CantidadEnStock { get; set; }

public Producto(string nombre, decimal precioUnitario, int cantidadEnStock)
{
Nombre = nombre;
PrecioUnitarioDolares = precioUnitario;
CantidadEnStock = cantidadEnStock;
}
}
internal class Solution //reference: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/lock
{

static List<Producto> productos = new List<Producto>
internal class Producto
{
public string Nombre { get; set; }
public decimal PrecioUnitarioDolares { get; set; }
public int CantidadEnStock { get; set; }

public Producto(string nombre, decimal precioUnitario, int cantidadEnStock)
{
Nombre = nombre;
PrecioUnitarioDolares = precioUnitario;
CantidadEnStock = cantidadEnStock;
}
}

internal class Solution
{
static List<Producto> productos = new List<Producto>
{
new Producto("Camisa", 10, 50),
new Producto("Pantalón", 8, 30),
Expand All @@ -25,24 +29,87 @@ internal class Solution //reference: https://learn.microsoft.com/en-us/dotnet/cs
new Producto("Gorra", 16, 10)
};

static int precio_dolar = 500;

static void Tarea1()
{
throw new NotImplementedException();
}
static void Tarea2()
{
throw new NotImplementedException();
}
static void Tarea3()
{
throw new NotImplementedException();
}

internal static void Excecute()
{
throw new NotImplementedException();
}
}
}
static int precio_dolar = 500;
static readonly object lockObject = new object();
static ManualResetEvent tarea1Completed = new ManualResetEvent(false);
static ManualResetEvent tarea2Completed = new ManualResetEvent(false);
static ManualResetEvent tarea4Completed = new ManualResetEvent(false);

static void Tarea1()
{
lock (lockObject)
{
foreach (var producto in productos)
{
producto.CantidadEnStock += 10;
}
tarea1Completed.Set();
}
}

static void Tarea2()
{
lock (lockObject)
{
precio_dolar = 520; //suponemos un precio dolar de 520
tarea2Completed.Set();
}
}

static void Tarea4()
{
lock (lockObject)
{
foreach (var producto in productos)
{
producto.PrecioUnitarioDolares *= 1.10m; // aumentamos un 10%
}
tarea4Completed.Set();
}
}

static void Tarea3()
{
// esperamos que se completen las primera 2
WaitHandle.WaitAll(new WaitHandle[] { tarea1Completed, tarea2Completed, tarea4Completed });

lock (lockObject)
{
decimal totalInventario = 0;
Console.WriteLine("Informe de productos:");
foreach (var producto in productos)
{
decimal precioEnPesos = producto.PrecioUnitarioDolares * precio_dolar;
totalInventario += precioEnPesos * producto.CantidadEnStock;
Console.WriteLine($"Producto: {producto.Nombre}, Cantidad: {producto.CantidadEnStock}, Precio Unitario en Pesos: {precioEnPesos}");
}
Console.WriteLine($"Precio total del inventario en pesos: {totalInventario}");
}
}

internal static void Excecute()
{
Thread thread1 = new Thread(Tarea1);
Thread thread2 = new Thread(Tarea2);
Thread thread3 = new Thread(Tarea3);
Thread thread4 = new Thread(Tarea4);

thread1.Start();
thread2.Start();
thread4.Start();
thread3.Start();
thread1.Join();
thread2.Join();
thread4.Join();
thread3.Join();
}
}

class Program
{
static void Main(string[] args)
{
Solution.Excecute();
}
}
}