ivanseidel / ArduinoThread

⏳ A simple way to run Threads on Arduino
MIT License
955 stars 196 forks source link

Any Possible to run Two Threads as same time (MultiThreading) #10

Closed androsathish closed 7 years ago

androsathish commented 8 years ago

I want to run some code in background .

Is there any possible to do this?

In Arduino , I want to run some connection process as background like BT connection , LWifi Connection , etc,

for example , here i'm trying to connect LWiFi as separate process (background)

void setup() { // put your setup code here, to run once:

 Serial.begin(115200);
    LWiFi.begin();
    Serial.println("WiFi Begin Succeed..!");

} void loop() { // put your loop code here, to run always:

Serial.Println("Loop is running");
  // Assume here i want to connect LWiFi
 LWiFi.connect(WIFI_AP, LWiFiLoginInfo(WIFI_AUTH, WIFI_PASSWORD));
// this statement take some time. so i want to avoid that delay

}

always run Serial.println() without any delay, and my LWiFi.connect should be run always in background .

how to do this?

ivanseidel commented 8 years ago

Hi Sathish,

In order to avoid "delay" in the function "LWiFi.connect", you must go deep into that function code and extract partes where it can be separated, and then, create a thread that will deal with the state of it.

Let's say that there is a method inside it, that waits for a response or successfull connection, (with a while for instance), you will have to make that while a verificiation that will run inside a thread, and make it skip that method at that time if not connected yet.

There is no other way to do that, unless stack manipulation is used. I prefer a less intrusive approach like the one I told you, as most arduino doesn't have a long stack and can be overflown very easily

Kind regards,

Ivan Seidel Gomes http://TendaDigital.net http://GitHub.com/ivanseidel http://github.com/ivanseidel

2016-02-16 4:16 GMT-02:00 Sathish Kumar J notifications@github.com:

I want to run some code in background .

Is there any possible to do this?

In Arduino , I want to run some connection process as background like BT connection , LWifi Connection , etc,

for example , here i'm trying to connect LWiFi as separate process (background)

void setup() { // put your setup code here, to run once:

Serial.begin(115200); LWiFi.begin(); Serial.println("WiFi Begin Succeed..!");

} void loop() { // put your loop code here, to run always:

Serial.Println("Loop is running"); // Assume here i want to connect LWiFi LWiFi.connect(WIFI_AP, LWiFiLoginInfo(WIFI_AUTH, WIFI_PASSWORD)); // this statement take some time. so i want to avoid that delay

}

always run Serial.println() without any delay, and my LWiFi.connect should be run always in background .

how to do this?

— Reply to this email directly or view it on GitHub https://github.com/ivanseidel/ArduinoThread/issues/10.

CAHEK7 commented 8 years ago

This simple threads library doesn't provide any functionality for preemptive multitasking. You need fully-functional dynamic task scheduler tied with timer interrupts to make it work. But there could be small possibility to run some kind of 'simultaneous tasks' as long as the tasks are using delay function inside. delay calls yield function which can be reimplemented to support simple task switching, but since we must keep accurate timing, it can be very dangerous to blindly switch any tasks. So it's still open question.

ivanseidel commented 7 years ago

@CAHEK7 explained the reason. Closing...