I have all the basic components coded for my outlook email arduino notification system. In my C windows app, I am thinking I have to use multithreading because I am constant checking for new email, and I have to send data to the ardunio. Do you think I should use CreateThread() for the serial sending function, or do you think some Sleep statements will suffice.
Also, I got wireless RF modules in today, and my ardunio is constantly checking for serial commands from my C program. How do I get the ardunio to send out wireless data when it's in a loop checking and parsing serial commands, do I have to use multithreading on the arduino too?
Multithreading would be a good idea when you have multiple activities that are happening "at the same time". In a typical GUI application (like the browser you're using) you have the UI that is handled by a thread, the networking part, handled by another thread, the rendering of what has been fetched by the networking in yet another thread etc, and then this multiplied by the number of tabs open. But at the heart of the system, if you have a simple, one core, one pipeline processor the multithreading is a clever trick of scheduling the processor's time.
So the question of whether to have multithreading is a question of do you have such a complex application. If that is a C application without GUI, that opens a socket to the mail application, checks if there is something new and then sends data to the Arduino the question is if the checking takes a lot of time (many seconds?, many minutes?) and do you want to notify the Arduino of the progress while the checking continues? Or is the mail check a quick task (milliseconds)? If so you can have the check and the notification code in a loop following each other.
On the Arduino it will be close to impossible to implement real multithreading.
On avrfreaks.net you can find couple of projects that are RTOSes for AVRs.
Well, I'm probably not the one to answer this, as I'm a rookie overall... but you shouldn't need to multithread anywhere. Just check for email, then send the result to the arduino, then check for email again. You don't need to be doing nothing but checking for email as rapidly as possible; that email can sit there and wait for a second or so. Besides, if you spam-check for mail, you're going to be generating an absurd amount of useless network traffic to your mail server. Why not just check for new email every .
Likewise, on the arduino side, check for incoming serial communication from your PC, parse whatever you receive, then send your wireless data sequentially in the loop. There's no multithreading on the arduino
You can use interrupts to acheive similar results, but I don't think it'll be necessary. While receiving serial data you'll have some buffer space to hold a few bytes (I forget how many?) so losing a couple ms sending RF data shouldn't be a problem.
It is going to be hard to create a background worker process with a user interface in any desktop operating system without using a background thread or background or asynchronous communication method. If you do, you are going to find that for the second the communication occurs, your user interface is unresponsive to the user.
However, whenever using multithreading, use threads sparingly. Creating and managing threads isn't a trivial task, and a good rule of thumb is only have as many threads as processors. If you are going to check every second for new email and your thread is mostly inactive, that works for a multithreading situation. However, check out your serial communications methods. You can also use things like overlapped or asynchronous reads to do the same thing, without all the hassles. Keeping things single thread helps to avoid other problems of passing non-threaded handles across threads and having odd bugs appear.
On the arduino you don't get to ask the question of multithreading, because the processors and libraries don't support it (yet). You can use ISR's (Interrupt service routines) to run periodically and check what you need, but since you have a loop() built in, you don't need the extra work.
Something like the code below will run a task every 5 seconds, yet still give you somewhere to do other things at the same time. Just keep away from delay();
const unsigned long iInterval=5000;
unsigned long last=0;
void setup()
{
last = millis();
}
void loop()
{
if(last+iInterval>millis())
{
// do something one every iInterval here.
last = millis();
}
// do something quickly here every loop iteration.
}
Thanks guys, I really want to avoid delay() at all costs it seems kinda cheap and it stops processing other routines altogether. Same with Sleep() on the Windows side.
Spinlock your code looks promising, since accuracy of the delay is not important, I'm gonna try to implement it later tonight.
Also, how big is the buffer for the arduino serial routines, just a standard serial.read()