2025-01-31 02:15 AM
Hi!
i have a question about the use of byte pool in threadX.
in the App_ThreadX_Init(VOID *memory_ptr) i have the pointer to the byte pool is given like that:
UINT App_ThreadX_Init(VOID *memory_ptr)
{
UINT ret = TX_SUCCESS;
TX_BYTE_POOL *byte_pool = (TX_BYTE_POOL*)memory_ptr;
and i'm using this pointer to allocate memory for thread, queue ecc .
now i'm passing the pointer byte_pool to my threads like this:
tx_byte_allocate(byte_pool,&pointer,CONSOLETHREAD_STACK_SIZE, TX_NO_WAIT);
tx_thread_create(&threadConsole,"console_thread",console_thread_entry,(ULONG)byte_pool,pointer,CONSOLETHREAD_STACK_SIZE,15,15,1,TX_AUTO_START);
and i want to use that pointer to allocate memory with tx_byte_allocate for sending via queue pointer to data structure instead of the actual data
example:
VOID *pointer;
if(tx_byte_allocate(byte_pool,&pointer,(sizeof(paramRequest_t)), TX_NO_WAIT) == TX_SUCCESS)
{
((paramRequest_t*)pointer)->startParamN = 0;
((paramRequest_t*)pointer)->quantity = paramsNum;
((paramRequest_t*)pointer)->readQueue_ptr = ¶msRequestINmodbusqueue;
if(tx_queue_send(¶msRequestqueue,&pointer, TX_NO_WAIT) == TX_SUCCESS)
{
requestParam = FALSE;
}
}
in another thread when i receive the queue i free the memory
if(tx_queue_receive(¶msRequestqueue,¶mRequest,TX_NO_WAIT) == TX_SUCCESS) // read params request
{tx_byte_release(paramRequest);
the problem is that if i use the same byte_pool for all the thread it crash, if i use separate byte pool work normally
i have tried with a mutex when i use the allocate and free function but not seem to work..
is it possible to use the same byte pool or i must use separate byte pool for each thread?
thanks
2025-01-31 02:49 AM
Hello @ABasi.2 ,
You can use the same pool to allocate thread stacks, queues, semaphores ... this is illustrated in the demo
threadx/samples/demo_threadx.c at master · eclipse-threadx/threadx
see the thread1 & 2 which are sharing a queue in producer consumer model which is quite similar to what you are trying to achieve. note that queue pool allocation and creation is done along with thread memory allocation and creation in tx_application_define.
Regards
2025-01-31 03:04 AM
Hello STea
thank you for your responce
but you aren't acqually using tx_byte_allocate in the threads! all the memory are already allocated Before the thread creation in your example
my issue is when i use the same byte_pool created by the application define in more then one thread
if you notice i'm passing the byte_pool pointer to each threads and using it to allocate memory to send only pointer to memory and not data with queue.
this code is inside a thread:
if(tx_byte_allocate(byte_pool,&pointer,(sizeof(paramRequest_t)), TX_NO_WAIT) == TX_SUCCESS)
{
((paramRequest_t*)pointer)->startParamN = 0;
((paramRequest_t*)pointer)->quantity = paramsNum;
((paramRequest_t*)pointer)->readQueue_ptr = ¶msRequestINmodbusqueue;
if(tx_queue_send(¶msRequestqueue,&pointer, TX_NO_WAIT) == TX_SUCCESS)
{
requestParam = FALSE;
}
}
one work around to that is to declare an array in the thread and create a new memory pool using that array
but if it possible i prefer to use the same pool that i have used to allocate memory for the thread , queue ecc
but seem it generate an issue
thanks