From ebbd955abd779006fa22bf80774d8ece358959cf Mon Sep 17 00:00:00 2001 From: SCG82 Date: Thu, 12 Dec 2019 02:52:37 -0800 Subject: [PATCH] revert to using std::thread. experimenting --- Makefile | 2 +- src/Dependency.cpp | 2 -- src/DylibBundler.cpp | 58 +++++++++++++++++++--------------- src/ParallelFor.h | 75 ++++++++++++++++++++++---------------------- src/main.cpp | 10 +++--- 5 files changed, 76 insertions(+), 71 deletions(-) diff --git a/Makefile b/Makefile index 8b7fd6e..7e77831 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,7 @@ dylibbundler: $(CXX) $(CXXFLAGS) -c -I./src ./src/Dependency.cpp -o ./Dependency.o $(CXX) $(CXXFLAGS) -c -I./src ./src/main.cpp -o ./main.o $(CXX) $(CXXFLAGS) -c -I./src ./src/Utils.cpp -o ./Utils.o - $(CXX) $(CXXFLAGS) -lpthread -o ./dylibbundler ./Settings.o ./DylibBundler.o ./Dependency.o ./main.o ./Utils.o + $(CXX) $(CXXFLAGS) -o ./dylibbundler ./Settings.o ./DylibBundler.o ./Dependency.o ./main.o ./Utils.o clean: rm -f *.o diff --git a/src/Dependency.cpp b/src/Dependency.cpp index ff342de..b49d405 100644 --- a/src/Dependency.cpp +++ b/src/Dependency.cpp @@ -246,7 +246,6 @@ void Dependency::fixFileThatDependsOnMe(std::string file_to_fix) exit(1); } } - pthread_exit(NULL); }); // FIXME - hackish @@ -273,7 +272,6 @@ void Dependency::fixFileThatDependsOnMe(std::string file_to_fix) exit(1); } } - pthread_exit(NULL); }); } } diff --git a/src/DylibBundler.cpp b/src/DylibBundler.cpp index 3d067b1..16920b2 100644 --- a/src/DylibBundler.cpp +++ b/src/DylibBundler.cpp @@ -52,7 +52,7 @@ void changeLibPathsOnFile(std::string file_to_fix) { deps[i].fixFileThatDependsOnMe(file_to_fix); } - pthread_exit(NULL); + }); } @@ -152,8 +152,11 @@ void fixRpathsOnFile(const std::string& original_file, const std::string& file_t rpaths_to_fix = found->second; } - parallel_for(rpaths_to_fix.size(), [&](int start, int end) - { + int start = 0; + int end = rpaths_to_fix.size(); + + // parallel_for(rpaths_to_fix.size(), [&](int start, int end) + // { for(int i=start; i& lines) if(output.find("can't open file")!=std::string::npos or output.find("No such file")!=std::string::npos or output.size()<1) { + std::cerr << output << std::endl; std::cerr << "Cannot find file " << filename << " to read its dependencies" << std::endl; exit(1); } @@ -217,8 +223,10 @@ void collectDependencies(std::string filename) std::cout << "."; fflush(stdout); const int line_amount = lines.size(); - parallel_for(line_amount, [&](int start, int end) - { + int start = 0; + int end = line_amount; + // parallel_for(line_amount, [&](int start, int end) + // { for(int i=start; i #include #include -#include +// #include -typedef void *(*THREADFUNCPTR)(void *); +// typedef void *(*THREADFUNCPTR)(void *); /** * @param nb_elements size of your for loop @@ -17,73 +17,74 @@ typedef void *(*THREADFUNCPTR)(void *); * @param use_threads : enable / disable threads. * */ -// static void parallel_for(unsigned nb_elements, -// std::function functor, -// bool use_threads = true) static void parallel_for(unsigned nb_elements, std::function functor, bool use_threads = true) { - unsigned nb_threads_hint = std::thread::hardware_concurrency(); - unsigned nb_threads = nb_threads_hint == 0 ? 8 : (nb_threads_hint); + // unsigned nb_threads_hint = std::thread::hardware_concurrency(); + // unsigned nb_threads = nb_threads_hint == 0 ? 8 : (nb_threads_hint); + + unsigned nb_threads = 8; unsigned batch_size = nb_elements / nb_threads; unsigned batch_remainder = nb_elements % nb_threads; - std::vector my_threads(nb_threads); + // std::vector my_threads(nb_threads); + std::vector my_threads(nb_threads+1); - pthread_t threads[nb_threads]; - pthread_attr_t attr; + // pthread_t threads[nb_threads]; + // pthread_attr_t attr; int rc; int i; void *status; // Initialize and set thread joinable - pthread_attr_init(&attr); - pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); + // pthread_attr_init(&attr); + // pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); if (use_threads) { // Multithread execution - // for(unsigned i = 0; i < nb_threads; ++i) - for (i = 0; i < nb_threads; ++i) { + for(unsigned i = 0; i < nb_threads; ++i) { + // for (i = 0; i < nb_threads; ++i) { int start = i * batch_size; - // my_threads[i] = std::thread(functor, start, start+batch_size); - rc = pthread_create(&threads[i], &attr, (THREADFUNCPTR)&functor, (void *)i); - if (rc) { - std::cout << "Error:unable to create thread," << rc << std::endl; - exit(-1); - } - std::cout << "pthread created" << std::endl; + my_threads[i] = std::thread(functor, start, start+batch_size); + // rc = pthread_create(&threads[i], &attr, (THREADFUNCPTR)&functor, (void *)i); + // if (rc) { + // std::cout << "Error:unable to create thread," << rc << std::endl; + // exit(-1); + // } + // std::cout << "pthread created" << std::endl; } // free attribute and wait for the other threads - pthread_attr_destroy(&attr); - for (i = 0; i < nb_threads; ++i) { - rc = pthread_join(threads[i], &status); - if (rc) { - std::cout << "Error:unable to join," << rc << std::endl; - exit(-1); - } - std::cout << "Main: completed thread id :" << i ; - std::cout << " exiting with status :" << status << std::endl; - } + // pthread_attr_destroy(&attr); + // for (i = 0; i < nb_threads; ++i) { + // rc = pthread_join(threads[i], &status); + // if (rc) { + // std::cout << "Error:unable to join," << rc << std::endl; + // exit(-1); + // } + // std::cout << "Main: completed thread id :" << i ; + // std::cout << " exiting with status :" << status << std::endl; + // } - std::cout << "Main: program exiting." << std::endl; - pthread_exit(NULL); + // std::cout << "Main: program exiting." << std::endl; + // pthread_exit(NULL); } else { // Single thread execution (for easy debugging) for (unsigned i = 0; i < nb_threads; ++i){ int start = i * batch_size; - functor( start, start+batch_size ); + functor(start, start+batch_size); } } // Deform the elements left int start = nb_threads * batch_size; - functor(start, start+batch_remainder); + // functor(start, start+batch_remainder); + my_threads[nb_threads] = std::thread(functor, start, start+batch_remainder); // Wait for the other thread to finish their task - // if(use_threads) - // std::for_each(my_threads.begin(), my_threads.end(), std::mem_fn(&std::thread::join)); + if(use_threads) + std::for_each(my_threads.begin(), my_threads.end(), std::mem_fn(&std::thread::join)); } diff --git a/src/main.cpp b/src/main.cpp index cec71ca..534d31d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -154,13 +154,13 @@ int main (int argc, char * const argv[]) std::cout << "* Collecting dependencies"; fflush(stdout); const int amount = Settings::fileToFixAmount(); - parallel_for(amount, [&](int start, int end) - { + int start = 0; + int end = amount; + // parallel_for(amount, [&](int start, int end) + // { for(int i=start; i