@@ -82,6 +82,46 @@ static auto run_on_core(const auto &f, int core_id, size_t stack_size_bytes = 20
82
82
}
83
83
}
84
84
}
85
+
86
+ // / Run the given function on the specific core without blocking the calling thread
87
+ // / @details This function will run the given function on the specified core,
88
+ // / without blocking the calling thread / context. A new thread is
89
+ // / spawned for the function even if the requested core is the same as
90
+ // / the core on which the calling thread is running.
91
+ // / @param f The function to run
92
+ // / @param core_id The core to run the function on
93
+ // / @param stack_size_bytes The stack size to allocate for the function
94
+ // / @param priority The priority of the task
95
+ // / @note This function is only available on ESP32
96
+ // / @note If you provide a core_id < 0, the thread will not be pinned to any
97
+ // / specific core, instead the scheduler will decide which core to run
98
+ // / the thread on
99
+ // / @note If you provide a core_id >= configNUM_CORES, the function will run on
100
+ // / the last core
101
+ static void run_on_core_non_blocking (const auto &f, int core_id, size_t stack_size_bytes = 2048 ,
102
+ size_t priority = 5 ) {
103
+ // Otherwise run the function on the desired core
104
+ if (core_id > configNUM_CORES - 1 ) {
105
+ // If the core id is larger than the number of cores, run on the last core
106
+ core_id = configNUM_CORES - 1 ;
107
+ }
108
+ auto thread_config = esp_pthread_get_default_config ();
109
+ thread_config.thread_name = " run_on_core_thread" ;
110
+ if (core_id >= 0 )
111
+ thread_config.pin_to_core = core_id;
112
+ thread_config.stack_size = stack_size_bytes;
113
+ thread_config.prio = priority;
114
+ // this will set the config for the next created thread
115
+ auto err = esp_pthread_set_cfg (&thread_config);
116
+ if (err != ESP_OK) {
117
+ // failed to set the config, can't create the thread; simply run the function
118
+ // on the current core
119
+ f ();
120
+ return ;
121
+ }
122
+ auto thread = std::thread (f);
123
+ thread.detach ();
124
+ }
85
125
#endif
86
126
} // namespace task
87
127
} // namespace espp
0 commit comments