Skip to content

Commit

Permalink
feat: eliminar uso de sstream
Browse files Browse the repository at this point in the history
Con fmt:print() podemos conseguir algo parecido.
  • Loading branch information
jesustorresdev committed Jun 29, 2023
1 parent cf4833f commit e8e02a5
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 48 deletions.
4 changes: 2 additions & 2 deletions src/cap12/pthreads-factorial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ int main()

if (return_code)
{
fmt::print( stderr, "Error ({}) al crear el hilo: {}\n", return_code, strerror(return_code) );
fmt::print( stderr, "Error ({}) al crear el hilo: {}\n", return_code, std::strerror(return_code) );
return EXIT_FAILURE;
}

return_code = pthread_create( &thread2, nullptr, factorial_thread, &thread2_args );
if (return_code)
{
fmt::print( stderr, "Error ({}) al crear el hilo: {}\n", return_code, strerror(return_code) );
fmt::print( stderr, "Error ({}) al crear el hilo: {}\n", return_code, std::strerror(return_code) );

// Al terminar main() aquí, estaremos abortando la ejecución del primer hilo, si no ha terminado antes.
// Este caso es muy sencillo, así que no importa. Pero no suele ser buena idea no dejar que los hilos tengan
Expand Down
16 changes: 3 additions & 13 deletions src/cap12/threads-factorial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
//

#include <iostream>
#include <sstream>
#include <thread>

#define FMT_HEADER_ONLY
#include <fmt/format.h> // Hasta que std::format (C++20) esté disponible
#include <fmt/std.h>

#include <common/bigint_factorial.hpp>

Expand All @@ -22,16 +22,6 @@ void factorial_thread (BigInt& result, BigInt number, BigInt lower_bound)
result = calculate_factorial( number, lower_bound );
}

void print_thread_info(std::thread& thread)
{
std::stringstream ss;
ss << thread.get_id();
fmt::print( "Hilo creado: {} (0x{:x})\n",
ss.str(),
reinterpret_cast<uintptr_t>(thread.native_handle())
);
}

int main()
{
auto number = get_user_input();
Expand All @@ -43,10 +33,10 @@ int main()
auto thread2_number = thread1_lower_bound - 1;

std::thread thread1(factorial_thread, std::ref(thread1_result), number, thread1_lower_bound);
print_thread_info(thread1);
fmt::print( "Hilo creado: {} (0x{:x})\n", thread1.get_id(), thread1.native_handle() );

std::thread thread2(factorial_thread, std::ref(thread2_result), thread2_number, 2);
print_thread_info(thread2);
fmt::print( "Hilo creado: {} (0x{:x})\n", thread2.get_id(), thread2.native_handle() );

// Esperar a que los hilos terminen antes de continuar.
// Si salimos de main() sin esperar, el proceso terminará y todos los hilos morirán inmediatamente,
Expand Down
16 changes: 3 additions & 13 deletions src/cap13/threads-sync-counter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

#include <iostream>
#include <mutex>
#include <sstream>
#include <thread>

#define FMT_HEADER_ONLY
#include <fmt/format.h> // Hasta que std::format (C++20) esté disponible
#include <fmt/std.h>

struct increment_counter_thread_args
{
Expand All @@ -37,26 +37,16 @@ void increment_counter (increment_counter_thread_args& args)
fmt::print( "Valor del contador: {}\n", args.counter );
}

void print_thread_info(std::thread& thread)
{
std::stringstream ss;
ss << thread.get_id();
fmt::print( "Hilo creado: {} (0x{:x})\n",
ss.str(),
reinterpret_cast<uintptr_t>(thread.native_handle())
);
}

int main()
{
increment_counter_thread_args thread_args = { .counter = 0 };

// Crear algunos hilos independientes cada uno de los cuales ejecutará increment_counter()
std::thread thread1(increment_counter, std::ref(thread_args));
print_thread_info(thread1);
fmt::print( "Hilo creado: {} (0x{:x})\n", thread1.get_id(), thread1.native_handle() );

std::thread thread2(increment_counter, std::ref(thread_args));
print_thread_info(thread2);
fmt::print( "Hilo creado: {} (0x{:x})\n", thread2.get_id(), thread2.native_handle() );

// Esperar a que los hilos terminen antes de continuar.
// Si salimos de main() sin esperar, el proceso terminará y todos los hilos morirán inmediatamente,
Expand Down
16 changes: 3 additions & 13 deletions src/cap13/threads-sync-factorial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
#include <functional>
#include <mutex>
#include <numeric>
#include <sstream>
#include <thread>
#include <vector>

#define FMT_HEADER_ONLY
#include <fmt/format.h> // Hasta que std::format (C++20) esté disponible
#include <fmt/std.h>

#include <common/bigint_factorial.hpp>

Expand All @@ -39,16 +39,6 @@ void factorial_thread (factorial_thread_results& results, BigInt number, BigInt
// El mutex se desbloquea al destruirse 'lock'
}

void print_thread_info(std::thread& thread)
{
std::stringstream ss;
ss << thread.get_id();
fmt::print( "Hilo creado: {} (0x{:x})\n",
ss.str(),
reinterpret_cast<uintptr_t>(thread.native_handle())
);
}

int main()
{
auto number = get_user_input();
Expand All @@ -60,10 +50,10 @@ int main()
auto thread2_number = thread1_lower_bound - 1;

std::thread thread1(factorial_thread, std::ref(thread_results), number, thread1_lower_bound);
print_thread_info(thread1);
fmt::print( "Hilo creado: {} (0x{:x})\n", thread1.get_id(), thread1.native_handle() );

std::thread thread2(factorial_thread, std::ref(thread_results), thread2_number, 2);
print_thread_info(thread2);
fmt::print( "Hilo creado: {} (0x{:x})\n", thread2.get_id(), thread2.native_handle() );

// Esperar a que los hilos terminen antes de continuar.
// Si salimos de main() sin esperar, el proceso terminará y todos los hilos morirán inmediatamente,
Expand Down
15 changes: 8 additions & 7 deletions src/cap13/threads-sync-semaphore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,33 @@
// g++ -I../ -I../../lib -o threads-sync-semaphore threads-sync-semaphore.cpp
//

#include <thread>
#include <iostream>
#include <vector>
#include <thread>

#define FMT_HEADER_ONLY
#include <fmt/format.h> // Hasta que std::format (C++20) esté disponible

#include "semaphore.hpp"

void thread_func(semaphore& sem, int thread_id)
void thread_function(semaphore& sem, int thread_id)
{
sem.acquire();
fmt::print( "Hilo {} creado\n", thread_id);
std::this_thread::sleep_for(std::chrono::seconds(2)); // Dormir el hilo para simular trabajo
fmt::print( "Hilo {} terminado\n", thread_id);
fmt::print( "Hilo {} creado\n", thread_id );
std::this_thread::sleep_for( std::chrono::seconds(2) ); // Dormir el hilo para simular trabajo
fmt::print( "Hilo {} terminado\n", thread_id );
sem.release();
}

int main() {
int main()
{
const int num_threads = 10;
semaphore sem(3); // Solo permitir 3 hilos simultáneos

std::vector<std::thread> threads;
for(int i = 0; i < num_threads; i++)
{
threads.push_back(std::thread(thread_func, std::ref(sem), i));
threads.push_back( std::thread( thread_function, std::ref(sem), i ));
}

for(auto& t : threads)
Expand Down

0 comments on commit e8e02a5

Please sign in to comment.