1

Is there any way to get a different value in a constexpr thread_local variable for every thread?

constexpr thread_local someType someVar = ......;

It seems like constexpr thread_local is supported but the thread_local indicator doesnt seem to do anything in this case.

11
  • 6
    You seem to have some conflicting requirements. constexpr is for compile-time constants, while threads are a pure run-time concept. What is it you're trying to do? What is the actual problem you want to solve? Please take some time to read about the XY problem and think about how it relates to this question. Sep 29, 2017 at 12:58
  • What are you trying to achieve with this? Sep 29, 2017 at 13:02
  • I want to check whether something will be called from the right thread at compile-time Sep 29, 2017 at 13:05
  • 2
    You might be looking for std::this_thread::get_id().
    – nwp
    Sep 29, 2017 at 13:05
  • Well you can't do that. Calls happen at run-time, you can't make run-time checks at compile-time, it's simply not possible. Perhaps it's time to think (or rather rethink) about your requirements or your design? Sep 29, 2017 at 13:08

1 Answer 1

0

If you think about your question, you yourself can see why this is not possible.

What is constexpr?

According to the informal standard site cppreference:

The constexpr specifier declares that it is possible to evaluate the value of the function or variable at compile time.

The compiler has to resolve the value at compile time and this value should not change throughout the execution of the program.

Thread-local storage

A thread, on the contrary, is a run-time concept. C++11 introduced the thread concept into the language, and thus you could say that a compiler can be "aware" of the thread concept. But, the compiler can't always predict if a thread is going to be executed (Maybe you run the thread only upon specific configuration), or how many instances are going to be spawn, etc.

Possible implementation

Instead of trying to enforce access to a specific module/method to a single thread using hacks and tricks, why not use a very primitive feature of the language?

You could just as well implement this using simple encapsulation. Just make sure that the only object that "sees" this method you are trying to protect is the thread object itself, for example:

#include <iostream>
#include <thread>
#include <chrono>

using namespace std;

class SpecialWorker
{
public:
    void start()
    {
        m_thread = std::move(std::thread(&SpecialWorker::run, this));
    }

    void join()
    {
        m_thread.join();
    }

protected:
    virtual void run() { protectedTask(); }

private:
    void protectedTask()
    {
        cout << "PROTECT ME!" << endl;
    }

    std::thread m_thread;
};

int main(int argc, char ** argv)
{
    SpecialWorker a;
    a.start();
    a.join();

    return 0;
}

Please note that this example is lacking in error handling and is not production grade code! Make sure to refine it if you intend to use it.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.