Is there a way for a class to have it’s own thread running?
I know that you cannot simply call a non-static class function, but std::thread
has plenty of ways around that, vex::thread
does seem to allow that. It cannot do arguments, and for some reason you cannot capture anything if you use a lambda…
withing a class…
vex::thread([this](){
//code
}).detach();
Error Message
[clang] No matching conversion for functional-style cast from ‘(lambda at src/lib81k/twModel.cpp:12:15)’ to ‘vex::thread’
- vex_thread.h(29, 9): Candidate constructor (the implicit copy constructor) not viable: no known conversion from ‘(lambda at src/lib81k/twModel.cpp:12:15)’ to ‘const vex::thread’ for 1st argument
- vex_thread.h(42, 7): Candidate constructor not viable: no known conversion from ‘(lambda at src/lib81k/twModel.cpp:12:15)’ to ‘int (*)()’ for 1st argument
- vex_thread.h(49, 7): Candidate constructor not viable: no known conversion from ‘(lambda at src/lib81k/twModel.cpp:12:15)’ to ‘void (*)()’ for 1st argument
- vex_thread.h(35, 7): Candidate constructor not viable: requires 0 arguments, but 1 was provided
[clang] Lambda capture ‘this’ is not used (fix available)
Notice that this works…
vex::thread([](){
//code
}).detach();
If you were able to pass arguments when running a thread you would simply be able to run a static function and pass the pointer to your class.
Is there something I am missing or is this just not possible. If it is not possible are there plans on implementing this, it would be very nice.
A custom solution
There would be a way to get around this limitation.- Create a global variable that stores a function, either use a lambda that captures something or have another variable for a class pointer
- When you need to run a class specific function you create a thread, which looks at the global variable and runs it
You could also store the functions in a list if you need to make sure two threads don’t accidentally use this at the same time.
If there is no way to accomplish it without this, I will probably make some simple code to do this and post it.
Edit: Here it is…
Class specific vex::thread