Im new to dual core programming and i do not know the best practices yet. In my current project, i would like the second core to only do something after it is being told by the first core.

The approach i am using somewhat of a brute force is something like this:

bool indicator = false;
int a,b;

void loop () { // core1
  if(indicator == false){  //check if core2 is still processing
     indicator = true;

void loop1(){ //core2
  if(indicator == true){
     int sum = a+b;
     indicator == false;

this is just a simplified idea of how my code is going to be. Are there better ways of going about this? In general i want to tell core2 from core1 that it needs to start/stop doing something.

1 Answer 1


What you have created there is called a semaphore.

In multi-threaded (or multi-core) programming there are two important signalling concepts:

  • Mutex - This is an object of some form that is used to allow access to a shared resource by only one core or thread at a time. A thread tries to "lock" a mutex before using a resource. A mutex can only be "locked" by one thread at a time, and if the mutex is already locked by another thread it can either block (aka a "spinlock") waiting for the mutex to be unlocked, or it can fail gracefully and try again later.
  • Semaphore - This is a simple flag that is used for one thread to tell another thread that something has happened or that it should do something. It is typically set by one thread then read and cleared by the other thread.

As I mentioned before, a semaphore is what you have created, and a simple boolean value is the most basic semaphore there is. Note, though, that you should really have the variable as volatile to prevent the compiler optimizing it away.

The RP2040 SDK has some functions built in for allowing the cores to communicate, including a "lockout" facility allowing one core to tell the other to stall until such time as it should start processing again, and a FIFO to allow the asynchronous passing of data from one core to the other.

  • Even after two days im still having troubles grasping the concepts. Which one should i use? both mutex and semaphore seems to be able to do the job, but which is much proper?
    – DrakeJest
    Nov 8, 2021 at 17:21
  • Also , in my example i just used basic datatypes. is it possible to lock a class data type.
    – DrakeJest
    Nov 8, 2021 at 17:23
  • @DrakeJest A mutex is used when you want to share a resource. For example if you have two threads that both want to print to serial. One thread "locks" the serial port mutex, prints what it wants, then releases it again. The other thread waits until it is able to lock the mutex itself before printing. That way you keep the serial data separate and don't end up with a garbled mess.
    – Majenko
    Nov 8, 2021 at 18:40
  • A semaphore is used to just inform another thread that something has happened. Maybe thread 1 has filled up a buffer with data and thread 2 should grab and process that data. Or maybe thread 1 has detected something happen on a sensor and wants to tell thread 2 that it happened.
    – Majenko
    Nov 8, 2021 at 18:41
  • And yes, you can use anything you like as a semaphore or mutex. You rarely want more than a simple bool or maybe an int. If you have a class you'll just be using a bool or int inside that class as the semaphore/mutex.
    – Majenko
    Nov 8, 2021 at 18:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.