diff --git a/src/cap12/pthreads-factorial.cpp b/src/cap12/pthreads-factorial.cpp index f445ba1..876fd55 100644 --- a/src/cap12/pthreads-factorial.cpp +++ b/src/cap12/pthreads-factorial.cpp @@ -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 diff --git a/src/cap12/threads-factorial.cpp b/src/cap12/threads-factorial.cpp index ce2e314..e51d9e5 100644 --- a/src/cap12/threads-factorial.cpp +++ b/src/cap12/threads-factorial.cpp @@ -9,11 +9,11 @@ // #include -#include #include #define FMT_HEADER_ONLY #include // Hasta que std::format (C++20) esté disponible +#include #include @@ -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(thread.native_handle()) - ); -} - int main() { auto number = get_user_input(); @@ -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, diff --git a/src/cap13/threads-sync-counter.cpp b/src/cap13/threads-sync-counter.cpp index 1d165d5..db16d84 100644 --- a/src/cap13/threads-sync-counter.cpp +++ b/src/cap13/threads-sync-counter.cpp @@ -9,11 +9,11 @@ #include #include -#include #include #define FMT_HEADER_ONLY #include // Hasta que std::format (C++20) esté disponible +#include struct increment_counter_thread_args { @@ -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(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, diff --git a/src/cap13/threads-sync-factorial.cpp b/src/cap13/threads-sync-factorial.cpp index 2c3031c..90b6d26 100644 --- a/src/cap13/threads-sync-factorial.cpp +++ b/src/cap13/threads-sync-factorial.cpp @@ -14,12 +14,12 @@ #include #include #include -#include #include #include #define FMT_HEADER_ONLY #include // Hasta que std::format (C++20) esté disponible +#include #include @@ -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(thread.native_handle()) - ); -} - int main() { auto number = get_user_input(); @@ -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, diff --git a/src/cap13/threads-sync-semaphore.cpp b/src/cap13/threads-sync-semaphore.cpp index 758addb..f5048a3 100644 --- a/src/cap13/threads-sync-semaphore.cpp +++ b/src/cap13/threads-sync-semaphore.cpp @@ -7,32 +7,33 @@ // g++ -I../ -I../../lib -o threads-sync-semaphore threads-sync-semaphore.cpp // -#include #include #include +#include #define FMT_HEADER_ONLY #include // 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 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)