taco
December 29, 2021, 4:39pm
#1
Hello, I have a function that I need to put on a separate thread.
I have a few arguments that I need to pass to the function that I’m threading.
How would I
1: Create a thread
2: Pass arguments to the function that thread is running
3: Start the thread
Thanks for any responses!
2 Likes
taco
December 29, 2021, 6:02pm
#2
@jpearman help papi
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
Assuming you’re using VEXcode C++, you can pass the parameters as additional arguments to the thread
constructor.
void myThreadedFunction(int a, int b){
// do some stuff here
}
int main(void){
// create & start the thread
thread myThread = thread(myThreadedFunction, 42, 314);
}
If you’re using PROS I would imagine you can do something similar.
3 Likes
taco
December 29, 2021, 6:21pm
#4
code not work, error hapen
poop.
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀v
taco
December 29, 2021, 6:30pm
#5
here thing btw
Thing i wanna thread:
void bindTwoMotorsToButtons(
controller::button &btn1,
controller::button &btn2,
int speed,
motor m
) {
while (true) {
if (btn1.pressing()) {
m.spin(directionType::fwd, 100, velocityUnits::pct);
} else if (btn2.pressing()) {
m.spin(directionType::rev, 100, velocityUnits::pct);
} else {
m.stop();
}
this_thread::sleep_for(20);
}
}
code for threading:
thread myThread = thread(bindTwoMotorsToButtons, Controller1.ButtonL1, Controller1.ButtonL2, 100, LiftClaw);
error:
[clang] No matching constructor for initialization of ‘vex::thread’
still poop
Connor
December 29, 2021, 6:32pm
#6
I believe you are referring to multi-tasking correct?
If you would like to do that, you first need to make a function:
void drivePID(){
//Do stuff
}
Then, whenever you would like to run this as a separate task, you can do the following in code that is ran:
void autonomous(void) {
vex::task billWiTheScienceFi(drivePID);
}
I don’t know how to pass parameters directly this way, but you can always use global variables. Here’s what I mean:
int a;
int drivePID(){
if a == 50{
//Do Stuff
}
}
void autonomous(void) {
a = 50;
vex::task billWiTheScienceFi(drivePID);
}
2 Likes
taco
December 29, 2021, 6:34pm
#7
yeah i guess that would work, putting them in global variables kindof sussy ngl.
it doesnt seem very clean saying i know that there is a cleaner was to pass the arguments.
seems like it will work, will do.
thanks bb
2 Likes
Connor
December 29, 2021, 6:36pm
#8
It honestly is sketch, and I bet there’s better alternatives. But until there’s anything better presented then I hope that can be a good ‘patch’ until someone like @jpearman hops on to help out
taco
December 29, 2021, 6:37pm
#9
the patch shall work, until daddy pearman revives himself i will stick with the sussy global variable method
2 Likes
Seriously, we are all on holiday and really have better things to do, but this is something I posted before I think.
example code
/*----------------------------------------------------------------------------*/
/* */
/* Module: main.cpp */
/* Author: james */
/* Created: Wed Dec 29 2021 */
/* Description: V5 project */
/* */
/*----------------------------------------------------------------------------*/
// ---- START VEXCODE CONFIGURED DEVICES ----
// ---- END VEXCODE CONFIGURED DEVICES ----
#include "vex.h"
using namespace vex;
typedef struct _mydata {
int para1;
int para2;
} mydata;
mydata Mydata;
int taskWithArg( void *arg ) {
mydata *d = (mydata *)arg;
printf("taskWithArg %d %d\n", d->para1, d->para2 );
return 0;
}
int anotherTask( void *arg ) {
int *x = (int *)arg;
printf("anotherTask %d\n", *x );
return 0;
}
int main() {
// pass a structure
Mydata.para1 = 9;
Mydata.para2 = 28;
task t1( taskWithArg, (void *)&Mydata );
// if passing a local variable, make sure it doesn't go
// out of scope.
int x = 8;
task t3( anotherTask, (void *)&x );
while(1)
this_thread::sleep_for(10);
}
You can pass exactly one void pointer to a task.
9 Likes
taco
December 29, 2021, 6:49pm
#11
thank you so much papi, this code with make my driver code impeccable. i will not let you down