-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp32_stdmutex_003.ino
64 lines (48 loc) · 1.32 KB
/
esp32_stdmutex_003.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// std::thread plus mutex for ESP32, Arduino IDE
#include <Arduino.h>
#include <thread>
#include <freertos/task.h>
#include <mutex>
const auto one_sec = std::chrono::seconds { 1 };
#ifndef LED_BUILTIN
#define LED_BUILTIN 13
#endif
std::mutex serial_mutex;
void loop1() {
thread_local uint32_t counter = 0, i=0;
vTaskPrioritySet(NULL,0); //set Priority
while(true)
serial_mutex.lock();
Serial.println((String)"this is loop1, counter="+counter);
serial_mutex.unlock();
counter++;
// do stuff
delay(10);
}
}
void loop2() {
thread_local uint32_t counter = 0, i=0;
vTaskPrioritySet(NULL,0); //set Priority
while(true) {
serial_mutex.lock();
Serial.println((String)"this is loop2, counter="+counter);
serial_mutex.unlock();
counter++;
// do stuff
delay(10);
}
}
void setup() {
Serial.begin(115200);
delay(1000);
std::thread thread_1 (loop1);
std::thread thread_2 (loop2);
}
void loop() {
static uint32_t main_loop_counter = 0;
serial_mutex.lock();
Serial.println((String)"\nthis is main loop, main_loop_counter="+main_loop_counter );
serial_mutex.unlock();
delay(10);
main_loop_counter++;
}