AnsweredAssumed Answered

Mutexes in C++ Classes

Question asked by Louie Moye on Oct 13, 2015
Latest reply on Oct 22, 2015 by Matthew Kendall

Hi - background dev tool info:  I'm using CW10.6, MQX 4.1, K70 processor, custom BSP cloned from K70 tower board.

 

I seem to be having issues when trying to initialize a mutex in my C++ class.  I can successfully create a mutex in my main task, however any attempt to create/init a mutex in a C++ class fails and returns !MQX_OK.  I tried several examples deom the MQX manuals, and from the forums, but no success.  My function (below) is successful if called from main, but unsuccessful if called from my C++ class.  I've also tried initializing using a mutex attribute and attribute init:

 

/***** CODE START *****/

MUTEX_STRUCT_PTR InitMutex()

{

  MUTEX_STRUCT  newMutex;

 

  // Initialize the mutex 

  if (_mutex_init(&newMutex, 0) != MQX_OK)

  {

  printf("Initialize print mutex failed.\n");

  _task_block();

  }

 

  // Test the new mutex

  if (_mutex_lock(&newMutex) == MQX_OK)

  {

  printf("Mutex lock worked.\n");

  }

  _mutex_unlock(&newMutex);

 

  return (MUTEX_STRUCT_PTR)&newMutex;

}

/***** CODE END *****/

 

Then call with

 

/***** CODE START *****/

MUTEX_STRUCT_PTR m_ptrQueueMutex = InitMutex();

 

  if (m_ptrQueueMutex != NULL)

    printf("Got a mutex pointer\n");

/***** CODE END *****/

 

In main, I get this output:

"Mutex lock worked"

"Got a mutex pointer"

 

And in my class I get:

"Initialize print mutex failed."

 

Any help or suggestions will be greatly appreciated.

Outcomes