ftrias / TeensyThreads

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Multiple Arguments at Thread Launch

SuperLaserX opened this issue · comments

Hi Team,
I'm already using TeensyThreads for a project of mine, however, I am attempting to extend the funcionality I already have, but require launching a new thread with multiple arguments. The docs say it supports std::thread-style implementation, however you can't launch it as you would a normal std::thread(func, arg1, arg2, arg3, etc). I've tried to make it work passing my args as a pointer to a struct (void *), but have had absolutely no luck (the thread just never launches). What is the proper way to do this using TeensyThreads?

To simplify, I'm just doing something like:
`struct a {
int b;
int c;
};

void setup() {
a *x = malloc(sizeof(a));
a->b = 0;
a->c = 1;
std::thread z(dostuff, (void *)x);
z.detach();
}

void dostuff(void *args) {
a x = (a)args;
println(a->b);
println(a->c);
}`

I couldn't find any examples that use multiple args on a Teensy, so I got a bit lost.

I corrected my test app (since I hadn't built it before, I wrote it instead of dumping a novel's worth of code to point out a minor issue), and built it functional here:
`#include <TeensyThreads.h>
#include <stdint.h>
#include <stdio.h>
struct a {
int b;
byte c;
};

void setup() {
Serial.begin(9600);
}

void dostuff(void *args) {
a x = (a)args;
Serial.println(x->b);
Serial.println(x->c);
delete *x;
}

void loop() {
// put your main code here, to run repeatedly:
a *x;
x = malloc(sizeof(a));
x->b = 0;
x->c = 0x31;
std::thread z(dostuff, (void *)x);
z.detach();
}`

It appears to be functioning properly in this case. The issue I was seeing in my app was something else, and is a moot point now. Thanks so much for the prompt replies, you helped me resolve the primary issue in my application.