LabWindows/CVI Idea Exchange

cancel
Showing results for 
Search instead for 
Did you mean: 
0 Kudos
holly7787

Create a function that insert an object in the first position of a queue

Status: New

Now I have done this function to do it, you surely can do it better with extended error detection.

 

 

void CmtWriteTSQDataInFirstPosition(CmtTSQHandle Queue_Handle, const void *Buffer, size_t Number_of_Items, int Timeout_ms, int *Number_of_Items_Flushed)
{
	CmtTSQHandle Queue_Handle_Temp = {0};
	const void *BufferTemp = {0};

	size_t ItemsInQueue = {0};
	size_t ItemsSize = {0};
	
	CmtGetTSQAttribute (Queue_Handle, ATTR_TSQ_ITEMS_IN_QUEUE, &ItemsInQueue);
	CmtGetTSQAttribute (Queue_Handle, ATTR_TSQ_ITEM_SIZE, &ItemsSize);

	CmtNewTSQ(ItemsInQueue + Number_of_Items, ItemsSize, 0, &Queue_Handle_Temp);
	CmtWriteTSQData (Queue_Handle_Temp, Buffer, Number_of_Items, Timeout_ms, NULL);
	
	BufferTemp = calloc(ItemsInQueue, ItemsSize); 
	while(ItemsInQueue > 0)
	{
		int CmtStatusOrItemRead = {0};
		CmtStatusOrItemRead = CmtReadTSQData (Queue_Handle, BufferTemp, ItemsInQueue, 0, 0);
		CmtWriteTSQData (Queue_Handle_Temp, BufferTemp, CmtStatusOrItemRead, Timeout_ms, NULL);
		ItemsInQueue -= CmtStatusOrItemRead;
	}
	free(BufferTemp);
	BufferTemp = 0;

	CmtGetTSQAttribute (Queue_Handle_Temp, ATTR_TSQ_ITEMS_IN_QUEUE, &ItemsInQueue);
	CmtGetTSQAttribute (Queue_Handle_Temp, ATTR_TSQ_ITEM_SIZE, &ItemsSize);
	
	BufferTemp = calloc(ItemsInQueue, ItemsSize); 
	while(ItemsInQueue > 0)
	{
		int CmtStatusOrItemRead = {0};
		CmtStatusOrItemRead = CmtReadTSQData (Queue_Handle_Temp, BufferTemp, ItemsInQueue, 0, 0);
		CmtWriteTSQData (Queue_Handle, BufferTemp, CmtStatusOrItemRead, Timeout_ms, Number_of_Items_Flushed);
		ItemsInQueue -= CmtStatusOrItemRead;
	}
	free(BufferTemp);
	BufferTemp = 0;

	return;
}

 

Davide Vittorio G. - TLGB S.R.L.
Italian SW Developer