Class specific vex::thread

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.
  1. Create a global variable that stores a function, either use a lambda that captures something or have another variable for a class pointer
  2. 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

1 Like

yes, but it’s complicated as callbacks must be static member functions.
(and args will be added at some point, the underlying vexos implementation supports passing a void * to a thread, but it was never added into the C++ API)

u might can start a private message and add everyone to the class one at a time. if that don’t make sense I can explain one step at a time

What I mean is do something like this…

#include <vector>

std::vector<std::function<void()>> functionVector;
void runThread(std::function<void()> newRunFunc)
{
  functionVector.push_back(newRunFunc);
  vex::thread([](){
    if(functionVector.size() > 0)
    {
      std::function<void()> runFunc = functionVector.end()[-1];
      functionVector.pop_back();
      runFunc();
    }
  }).detach();
}

And then you can do this…

class myClass
{
  void myThreadFunc(){}
  myClass()
  {
    runThread([this](){myThreadFunc();});
  }
};