Mutexes in C++ Classes

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

Mutexes in C++ Classes

跳至解决方案
753 次查看
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 项奖励
1 解答
590 次查看
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

在原帖中查看解决方案

2 回复数
591 次查看
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

590 次查看
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 项奖励