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

use pool in class/struct #48

Closed
profdevc opened this issue Jun 6, 2022 · 3 comments
Closed

use pool in class/struct #48

profdevc opened this issue Jun 6, 2022 · 3 comments

Comments

@profdevc
Copy link

profdevc commented Jun 6, 2022

I'd like to use thread_pool as a static struct/class member, for a test demo code like

//bs_thread_pool.cpp

#include <algorithm> // std::min, std::min_element, std::sort, std::unique
#include <atomic>    // std::atomic
#include <chrono>    // std::chrono
#include <cmath>     // std::abs, std::llround, std::round, std::sqrt
#include <ctime>     // std::localtime, std::strftime, std::time_t
#include <exception> // std::exception
#include <fstream>   // std::ofstream
#include <future>    // std::future
#include <iomanip>   // std::setprecision, std::setw
#include <ios>       // std::fixed
#include <iostream>  // std::cout
#include <limits>    // std::numeric_limits
#include <random>    // std::mt19937_64, std::random_device, std::uniform_int_distribution, std::uniform_real_distribution
#include <stdexcept> // std::runtime_error
#include <string>    // std::string, std::to_string
#include <thread>    // std::this_thread, std::thread
#include <utility>   // std::pair
#include <vector>    // std::begin, std::end, std::vector

// Include the header file for the thread pool library.
#include "BS_thread_pool.hpp"

struct test{
    static BS::thread_pool pool;
    static void testfunc(){
        int squares[100];
        pool.parallelize_loop(0, 100,
                            [&squares](const int a, const int b)
                            {
                                for (int i = a; i < b; ++i)
                                    squares[i] = i * i;
                            })
            .wait();
    }
};

int main(){
    test::testfunc();
    return 0;
}

compile with g++ bs_thread_pool.cpp -std=c++17 -pthread -o bs_thread_pool.cpp.o
I got errors like

/tmp/cc6he5hB.o: In function `test::testfunc()':
bs_thread_pool.cpp:(.text._ZN4test8testfuncEv[_ZN4test8testfuncEv]+0x3f): undefined reference to `test::pool'
collect2: error: ld returned 1 exit status

the testfunc() in my code must be static so I cannot let pool be no-static. It seems that the declaration and instantiation of pool are bound. And I think the error is independent with OS or g++/clang++ version. Do you have any idea about this situation? Maybe I made some stupid mistake
the thread_pool version is the latest 3.0.0

@bshoshany
Copy link
Owner

There are two issues here:

  1. The pool needs to be declared inline static so that it is properly initialized.
  2. In v3.0.0 the constructor was declared as explicit (based on another user's suggestion) and for some reason this prevents the default constructor from being used for a static class member - I'm not exactly sure why. However, this can be resolved by initializing the pool explicitly.

So in other words, you need to declare the pool in your class as follows:

inline static BS::thread_pool pool {0};

Specifying 0 as the parameter will automatically use the hardware concurrency, but you can also specify a different number of threads if you prefer. Hope this helps!

@profdevc
Copy link
Author

profdevc commented Jun 7, 2022

inline static BS::thread_pool pool {0};

thanks for your help. 👍

@bshoshany
Copy link
Owner

No problem! In the next release I will remove the explicit modifier and this should enable using the thread pool as a static member even without this workaround.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants