an Arduino (normally) has only one thread
Does this mean an Arduino can be multithreaded? if so how? I thought arduino was single threaded only?
With respect to the original question, firstly, when a class is intrinsically linked to a single physical device it makes sense to have only a single instance of that class. Secondly, I have created multiple instances of my logger object, and although it does compile and run without error, only the first instance created actually manages to write to the file. This code snippet illustrates the problem
.
#include <Logger.h>
Logger My1stLogFile;
Logger My2ndLogFile;
void setup()
{
Serial.begin(9600);
Serial.println("\n\rStarting......");
My1stLogFile.writeLine("Line one");
My2ndLogFile.writeLine("Line AAA");
My1stLogFile.writeLine("Line two");
My2ndLogFile.writeLine("Line BBB");
My1stLogFile.writeLine("Line three");
My2ndLogFile.writeLine("Line CCC");
My1stLogFile.writeLine("Line four");
My2ndLogFile.writeLine("Line DDD");
My1stLogFile.close();
}
This runs without error but the resulting log file contains the lines written by the first instance of the object, not the second i.e......
Line one
Line two
Line three
Line four
If I reverse the order of the declarations then the file gets the other set of lines instead.
Basically I have 2 questions....
1) is it possible to implement a singleton class?
2) if the answer is 'yes' how do I do it?
Cheers