Serial
lock()
Serial.lock, lock, Serial1.lock, lock
The USB and UART serial objects do not have built-in thread-safety. If you want to read or write the object from multiple threads, including from a software timer, you must lock and unlock it to prevent data from multiple thread from being interleaved or corrupted.
A call to lock lock()
must be balanced with a call to unlock()
and not be nested. To make sure every lock is released, it's good practice to use WITH_LOCK
like this:
// EXAMPLE USAGE
void loop()
{
WITH_LOCK(Serial) {
Serial.println("Hello there!");
}
}
Never use lock()
or WITH_LOCK()
within a SINGLE_THREADED_BLOCK()
as deadlock can occur.