LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Queue of Structure datatype

Solved!
Go to solution

Hi,

 

I have a strcuture of following form.

 

typedef struct DaqData
{
int stopFlag; 

char Data[]
int readingData; 
int panel; 
int threadInited;
} AIData;

 

I would like to create a thread safe queue with the above structure. However the element "Data[]"  in the above structure can have varying length and is not fixed size. So, is it feasible to create a threadsafe queue with the above structure. If not , what could be an alternative way to imlement the same.?

 

Best Regards

Deepu Jacob

 

 

-----------------------------------------------------------------------------------------------------------------------------
Waiting For the inner calling 🙂


0 Kudos
Message 1 of 9
(3,541 Views)
Solution
Accepted by djac91

As you can read in the help

If you need to store items of varying size in the thread safe queue, you must either use more than one thread safe queue, store pointers in the queue, or set the size to 1 and treat the queue as a byte stream.

 

Passing pointers will imply for example to allocate memory in the producer thread, pass the pointer to it (together with the block size) in the queue and free the memory in the consumer thread.

Using the queue as a byte stream implies the data block is passed to the queue as a series of single-byte elements regardless the structure definition; the consumer thread reads the amount of bytes from the queue and rebuilds the structure element from them. Again, the size of the data block must be passed in the queue as well.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 2 of 9
(3,513 Views)

passing pointers seems to be a good method. But why do we need the block size anyway?

how to combine block size and pointers as queue datatype? a structure consisting of pointer and block size element?

-----------------------------------------------------------------------------------------------------------------------------
Waiting For the inner calling 🙂


0 Kudos
Message 3 of 9
(3,510 Views)

Unless your variable data is a null-terminated string, you have no way of knowing how much data you are receiving other than passing the block size to the consumer thread.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 4 of 9
(3,499 Views)

Hi Roberto,

 

Based on the suggestions, i have created the following code. There is a structure with the name 'Packet' that defines various members. There is a void pointer member named 'Data'. This pointer is implemented keeping in mind that i can use it to point to data of various datatype and different lengths without affecting the actual size of structure. Since the structure's memory size is constant, i think i can use it with thread safe queue.

 

static CmtTSQHandle PacketQ;
typedef struct Packet
{
unsigned short PacketType;
unsigned short Param1;
unsigned short Param2;
size_t DataSize; // size of the data buffer
void *Data; //pointer to data
};

int main()
{

float64 *Bptr=NULL; //declare a buffer pointer
struct Packet *Mypacket; //declare a pointer to structure of type 'Packet'

//Initailize a queue with size 10 of type Mypacket(i.e Packet)
CmtNewTSQ(10,sizeof(struct Packet),OPT_TSQ_DYNAMIC_SIZE,&PacketQ);

Mypacket = (struct Packet*)malloc(sizeof(struct Packet)); //allocate memory for structure
Mypacket->PacketType = 1; //assign values to structure members.
Mypacket->Param1 = 100;
Mypacket->Param2 = 50;
Mypacket->DataSize = 1000; //no.of elements in databuffer

//dynamically allocate memory for 1000 elements in buffer
if( (Bptr = (float64*)malloc(1000*sizeof(double)))== NULL)
{
MessagePopup("Error","Not Enough memory");
}

SinePattern (1000, 10, 0.0, 100.0,Bptr); //generate 1000 samples and place it in buffer

Mypacket->Data = Bptr; //assigning buffer pointer to void pointer in structure

CmtWriteTSQData(PacketQ, Mypacket, 1, TSQ_INFINITE_TIMEOUT, NULL); //Enqueue data to TSqueue
return 0;
}

Now, i will dequeue the thread safe queue in another thread and get the information from the structure and then release the memory for Mypacket as well as Data. I would like to know whether or not my implementation will work as intended. 

-----------------------------------------------------------------------------------------------------------------------------
Waiting For the inner calling 🙂


0 Kudos
Message 5 of 9
(3,485 Views)

I never used such a schema but it seems to me that it should work.

I would have used calloc instead of malloc since it automatically adapts to data type and inizializes all elements to zero.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 6 of 9
(3,475 Views)

calloc for Mypacket or Data?, or for both?

-----------------------------------------------------------------------------------------------------------------------------
Waiting For the inner calling 🙂


0 Kudos
Message 7 of 9
(3,471 Views)

calloc for Bptr.

I don't think you need dynamical allocation of the structure.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 8 of 9
(3,465 Views)

Hi,

 

It works fine.

 

Thanks Smiley Happy

 

Regards

Deepu Jacob

 

-----------------------------------------------------------------------------------------------------------------------------
Waiting For the inner calling 🙂


0 Kudos
Message 9 of 9
(3,437 Views)