Mutexes in C++ Classes

cancel
Showing results for 
Search instead for 
Did you mean: 

Mutexes in C++ Classes

Jump to solution
326 Views
louiemoye
Contributor III

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.

0 Kudos
1 Solution
163 Views
matthewkendall
Contributor V

Is your object a global, with a constructor that is being called before MQX is started perhaps?

https://community.freescale.com/message/349610

View solution in original post

2 Replies
164 Views
matthewkendall
Contributor V

Is your object a global, with a constructor that is being called before MQX is started perhaps?

https://community.freescale.com/message/349610

View solution in original post

163 Views
Carlos_Musich
NXP Employee
NXP Employee

Hi Louie,

I assume that you may be able to reproduce the same project using the default K70 BSP. Am I right? Modifying the BSP should not affect the Kernel behavior.

I ask you this because I need to reproduce this issue on my side with K70 default BSP. Can you confirm this and provide the project where you see this problem? It is ok if you create a simple project just to test this case.

Thanks!

Carlos

0 Kudos