2

I have a constexpr array like this:

constexpr std::array<int, 4> BASES_TO_CHECK = { 8, 16, 32, 64 };

I would like to do something akin to:

std::array<std::thread, BASES_TO_CHECK.size()> calc;
for(size_t i = 0; i<BASES_TO_CHECK.size(); ++i)
{
    calc[i]=std::thread(calculate<BASES_TO_CHECK[i]>,  std::ref(recordMap[BASES_TO_CHECK[i]]),  std::ref(counterMap.at(BASES_TO_CHECK[i])),  std::ref(done));
}

However, as the variable is used as a template parameter, so that won't work. I have ended up doing this:

std::array<std::thread, BASES_TO_CHECK.size()> calc = {
    std::thread(calculate<BASES_TO_CHECK[0]>,  std::ref(recordMap[BASES_TO_CHECK[0]]),  std::ref(counterMap.at(BASES_TO_CHECK[0])),  std::ref(done)),
    std::thread(calculate<BASES_TO_CHECK[1]>,  std::ref(recordMap[BASES_TO_CHECK[1]]),  std::ref(counterMap.at(BASES_TO_CHECK[1])),  std::ref(done)),
    std::thread(calculate<BASES_TO_CHECK[2]>,  std::ref(recordMap[BASES_TO_CHECK[2]]),  std::ref(counterMap.at(BASES_TO_CHECK[2])),  std::ref(done)),
    std::thread(calculate<BASES_TO_CHECK[3]>,  std::ref(recordMap[BASES_TO_CHECK[3]]),  std::ref(counterMap.at(BASES_TO_CHECK[3])),  std::ref(done))
};

And this works, but is relying on me not changing the number of elements in the BASES_TO_CHECK without also manually updating the part of the code where calc array is initialised.

2
  • Can you elaborate a little more on what you mean when you say the variable is used as a template parameter? Commented Mar 27, 2019 at 17:51
  • @AndyG There is a template parameter, and i needed to be able to change it sequentually (see example, that was already in the question) Commented Mar 28, 2019 at 14:33

1 Answer 1

3
template<std::size_t... i>
std::array<std::thread, BASES_TO_CHECK.size()> gen_impl(std::index_sequence<i...>) {
    return {
                std::thread(calculate<BASES_TO_CHECK[i]>,
                            std::ref(recordMap[BASES_TO_CHECK[i]]),
                            std::ref(counterMap.at(BASES_TO_CHECK[i])),
                            std::ref(done)
                           )...
           };
}

auto calc = gen_impl(std::make_index_sequence<BASES_TO_CHECK.size()>{});
Sign up to request clarification or add additional context in comments.

3 Comments

I don't quite understand. Would i need to make everything global for this? BASES_TO_CHECK can be global, it's even better that way, but recordMap and counterMap are local variables.
Ok I have made BASES_TO_CHECK global, and added passed by reference parameters recordMap, counterMap, and done (as they are suppose to be local variables). Thanks a lot. It would be nice to have explanation as to what is happening, but i'll try to read it up myself.
Well your recordMap, counterMap and done can still be passed as parameters to the gen_impl function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.