Pass a parameter using vex::thread

So I am trying to figure out how to start a thread and then do something else. I saw this thread, but I need to pass a parameter to my function. Is there any way to use vex::thread and pass a parameter to a function

Additionally, is there a way to say wait until the function is done before moving on in the program?

I’m not very good at programming, so any help is appreciated. Thanks a lot!

You could define a lambda that calls the actual function with the parameter you want:

void thread_contents(int foo){
    //do some stuff
}

int main(){
    vex::thread myThread = vex::thread([]{
        //define a lambda that calls thread_contents with the desired value
        thread_contents(7);
    });
}

You could probably use a mutex to do this. But in the simplest case, where you want to do something like this:

void thread_contents(int foo){
    //do some stuff
}

int main(){
    vex::thread myThread = vex::thread([]{
        //define a lambda that calls thread_contents with the desired value
        thread_contents(7);
    });

    //main should wait until myThread has finished before continuing

    printf("myThread is done!");
}

then a better solution would be to just call thread_contents directly without spinning off a thread.

3 Likes

Yes, you can pass a void ptr to a thread, no need to use a lambda. The join() member function can cause the current thread to block until the specified thread terminates. See the API docs
https://api.vexcode.cloud/v5/html/classvex_1_1thread.html
But I suspect what you are trying to achieve doesn’t really need this functionality, perhaps you can explain what you are trying to do and we can suggest a simpler method.

6 Likes

Nevermind, I have figured out a way around my first question

In case you’re curious what I needed this for…

We use a P-Loop for our auton. We also have a thread called “Drive,” which accepts a number as a parameter (the distance to drive).

The drive function sets all the target variables to the amount, sets direction bools that the P-loop uses, and starts the P-loop.

Where I was running into trouble was that the Drive function would not finish until the P-loop had finished. This meant that if the robot was driving there was no way that I could do anything while the robot was in motion (E.G. Spin the rollers to unfold the robot). That’s why I was wondering if there was a way that I could start the drive task and then do something else while the task was going on.

What I realized is that I could use the method mentioned in the OP on the P-loop. This meant that the drive function would finish almost instantaneously, before the P-loop was done, meaning that I could do something else while the robot was driving

I am still wondering about my second question. Here is an example in pseudocode

  1. Start the drive thread
  2. Do something while the robot is driving (Intakes)
  3. Wait until the drive is done driving to the target (I.E. Wait until the drive thread has been completed)
  4. Do something else (Throw a ball into a goal)

I am trying to figure out #3, if there is a way to say 'Wait until a thread is done."

Would I be better off creating a “task is finished” bool that turns true if the thread is done, and then using that as #3??

Thanks again for your help.

i have a sight walkaround for this. Basically add another boolean parameter “intake”. Then get an if statements inside the while loop which if “intake” is true, it will turn the intake on. Then, you can just call drive (1000, true) to run the intake

2 Likes

yea, as I said, join() memer function can be used. or you could just block waiting for a variable to be set before the thread exits.

int
mythread() {
  Brain.Screen.print("start");
  Brain.Screen.newLine();
  this_thread::sleep_for(2000);
  Brain.Screen.print("stop");

  return 0;
}

int main() {
    thread t1(mythread);

    // block until thread t1 exits
    t1.join();

    while(1) {
        // Allow other tasks to run
        this_thread::sleep_for(10);
    }
}
5 Likes

I see, thanks a lot for your help