on 2021-12-14 07:34 AM
/* USER CODE BEGIN PV */ uint8_t thread_sender1[THREAD_STACK_SIZE]; uint8_t thread_sender2[THREAD_STACK_SIZE]; uint8_t thread_receiver[THREAD_STACK_SIZE]; uint8_t queue_stack[QUEUE_STACK_SIZE]; TX_THREAD sender1_ptr; TX_THREAD sender2_ptr; TX_THREAD receiver_ptr; TX_QUEUE queue_ptr; /* USER CODE END PV */
/* USER CODE BEGIN PFP */ VOID Sender1 (ULONG initial_input); VOID Sender2 (ULONG initial_input); VOID Receiver (ULONG initial_input); /* USER CODE END PFP */
UINT App_ThreadX_Init(VOID *memory_ptr){ UINT ret = TX_SUCCESS; TX_BYTE_POOL *byte_pool = (TX_BYTE_POOL*)memory_ptr; /* USER CODE BEGIN App_ThreadX_MEM_POOL */ (void)byte_pool; /* USER CODE END App_ThreadX_MEM_POOL */ /* USER CODE BEGIN App_ThreadX_Init */ tx_queue_create(&queue_ptr, "Queue0", 1 ,queue_stack,128); tx_thread_create(&sender1_ptr,"Sender1",Sender1, 0, thread_sender1,THREAD_STACK_SIZE,15,15,1,TX_AUTO_START); tx_thread_create(&sender2_ptr,"Sender2",Sender2, 0, thread_sender2,THREAD_STACK_SIZE,15,15,1,TX_AUTO_START); tx_thread_create(&receiver_ptr,"Receiver",Receiver, 0, thread_receiver,THREAD_STACK_SIZE,15,15,1,TX_AUTO_START); /* USER CODE END App_ThreadX_Init */ return ret; }
/* USER CODE BEGIN 1 */ VOID Sender2 (ULONG initial_input){ /* Infinite loop */ uint32_t message2=2; for(;;) { printf("Sender2\r\n"); tx_queue_send(&queue_ptr,&message2,TX_NO_WAIT); printf("Sender2 delay\r\n"); /* Sleep for 2seconds */ tx_thread_sleep(200); } }As you can see, the Sender2 will always send the message ‘2’ and the Sender1 will continue to increase its message value by one at every interaction.
tx_thread_create(&receiver_ptr,"Receiver",Receiver, 0, thread_receiver,THREAD_STACK_SIZE,10,10,1,TX_AUTO_START);Now that the Receiver thread has a higher priority, we know it will be the first thread to be called when the application starts, plus it can preempt the Sender1 and Sender2 threads whenever the queue is available and it can only be preempted by a priority 10 thread, which is not present on the demo, thus we can expect this to happen: