0

I would like to assign a name to a thread, the thread itself must do this. The thread is a class member of the class foo. I would like to start this thread with a lambda but unfortunately I get the error message: no match for call to '(std::thread) (foo::start()::<lambda()>)

Can someone explain to me where the problem is? Previously I had created a temporary thread object, and put this with move on the thread "manage", however, I can then give no name.

class foo { public: int start() { this->manage([this](){ auto nto_errno = pthread_setname_np(manage.native_handle(),"manage"); // Give thread an human readable name (non portable!) while(1){ printf("do work"); } }); return 1; } private: int retVal; std::thread manage; }; 

1 Answer 1

1

You passed the lambda in a wrong way, after initialization the manage thread can't be initialized again. you should create a new std::thread and assign it. the following compiles and indeed prints "manage".

class foo { public: int start() { manage = std::thread([this]{ auto nto_errno = pthread_setname_np(manage.native_handle(),"manage"); char name[16]; pthread_getname_np(pthread_self(), &name[0], sizeof(name)); cout << name << endl; }); manage.join(); return 1; } private: int retVal; std::thread manage; }; 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.