Recursive mutex can always be obtained even if it's never released

Hi,
I have a question about the recursive mutex. I have the following code:

xSemaphoreHandle myMutex;
void task1(void)
{
  myMutex = xSemaphoreCreateRecursiveMutex();
  . . .

  for (;;;)
  {
    if (xSemaphoreTakeRecursive(myMutex, 0xFFFF) == pdPASS)
    {
      // Can always get the myMutex even if the condition not satisfied
      . . .
    }
  }
}

void task2(void)
{
  if (condition)
  {
    // The below code never get chance to run
    xSemaphoreGiveRecursive(myMutex);
  }
}

It seems the mutex release code is never called. But the task1 can always get the mutex. I’m wondering what is wrong.
Thanks!

A Mutex can only be given by the task that took it, which is different than the behavior of an ordinary semaphore.

This allows Mutexes to support Priority Inheritance to get around the Priority Inversion problem.

Also, the special feature of a Recursive Mutex is that the task that holds it, can take it multiple times, and doesn’t actually release it until it gives it back that many times. This can handle certain nesting issues which otherwise might deadlock.

Hi Richard,
Thanks for your info!
I also tried to use the mutext instead of the recursive mutex. It’s the same thing.
I wonder what I should use in this case. Maybe I should use the counting semaphore?
Thanks.

Signaling from one task to another is a job for a semaphore (or direct to task notifications)

Yes, that should work. Let us know if you face any issue.