LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Some concepts of Thread Safety Queue

Hello ,
There are some questions about TSQ .
1,If the mechanism of TSQ is the same as the general "Queue" type described in data structure course ? that means FIFO mechanism .

2,If it is not ,then when read out data from the TSQ ,does it clear automaticlly ?

3,Using TSQ , assuming one thread writing data to the queue , and the other thread just read data from the same queue , does I need to protect the variable of the queue's handle ?

In addition , once using the thread protection mechanism , if it would cause a delay in a transaction process ? e.g., when a thread writing something to an dynamically allocated memory ,at the same time ,another thread which want to read out something from the memory ,then , it would have to wait
until the writing thread finish its work .

David
0 Kudos
Message 1 of 7
(4,164 Views)
1) It's not the same as a general queue object, it is a much more advanced method of passing streams of data.

2) It depends on how you call the function. The CmtTSQReadData function has a parameter called options. If you pass a value of 0 in the options parameter, it pulls the data off the queue when you read it. If you pass the constant OPT_TSQ_READ_PEEK in the options parameter, it will leave the data on the queue. You may want to do this for example, if you have multiple reader threads.

3) No, the queue handles and the data do not need to be protected in your code. The protection is built into the TSQ.

4) The TSQ should not introduce any significant delay in your code that isn't necessary to protect the data. Yes, as will all synchronizatio
n, if a thread is writing to the data buffer at the same spot another thread is trying to access, the other thread will have to wait. However, with the queue, the architecture of it means that this doesn't happen much, since the readers are usually pulling off data from the bottom of the queue and the writer is writing to the top of the queue. The TSQ provides the most efficient way to pass a stream of data from one thread to another.

Best Regards,

Chris Matthews
National Instruments
0 Kudos
Message 2 of 7
(4,164 Views)
Thank you , Chris . Now I am facing a trouble of how to protect a dynamically allocated memory . The description is this :
Process of function 1 :
1, Using calloc () function to generate a sequential space in the memory , filling in the space with a struct type data .
2, Return head address of the allocated space .
In function 2 :
1, Define a global struct pointer type variable , which point to the returned value from function 1.
2 , spawn 2 threads , one used to write data to the preallocated memory , and the other used to read out data from it .
In such an application , actually , the allocated space can be accessed by an array of struct pointer ,so if define such a global : struct myData * mData ; you can use subscript of the pointer to access t
he space , e.g., mData [0] = the first data of the space .
Having to protect the dynamically allocated memory , so I want to use the method of thread safe variable , that is , using array as safe variable . but before stating the macro of DefineThreadSafeArrayVar (struct myData, mData, number, 0), the array size must be known in advance , due to the space is allocated dynamically , it is sure that can not know the array size before stating the macro . So , is there any way to resolve the problem ?
Please do not consider TSQ method , since the actually situation do not let me to create such a mechanism . Your advise is highly appreciated .

David
0 Kudos
Message 3 of 7
(4,164 Views)
Personally, I would use a TSQ for passing dynamic data between threads, but since you said not to consider that method I'll assume you know reasons why that wouldn't work for your program.

Thread Safe Variable are not designed to be used with dynamically allocated buffers. What you would need to use is just a thread lock for each buffer.

When you allocate the memory create a thread lock with CmtNewLock. Then, when you want to read or write from the buffer. Call CmtGetLock, do your reading or writing to the memory, then call CmtReleaseLock. Wrapping your reading and writing to the memory with a thread lock will ensure threads don't interfere with each other.

Best Regards,

Chris Matthews
National Instruments
0 Kudos
Message 4 of 7
(4,164 Views)
From the description you mention, it seems that the TSQ would be your best option since it can grow dynamically and you do not initially have to know its size. Otherwise, if you create your own structure, you will be implementing your own thread-safe array, in this case, except you would need to keep track of its size and allocate more memory as you see fit, which could get complicated. If you don't mind my curiosity, why is the TSQ not a good option for you in this scenario?

Regards,
Azucena
NI
0 Kudos
Message 5 of 7
(4,164 Views)
Hello Cris and Azucena ,

Thank you for your reply .
In fact ,the preallocated memory is a realtime database, its mechanism likes NI's BridgeVIEW somewhat . The system using TSQ read a struct type item data from device server , for updating RTDB ,it just write an element of the struct data to the RTDB ,not the overall struct . Due to there has already a TSQ between RTDB and device server , so if using TSQ to prevent RTDB , system would have to maintain another queue which just is the same copy of the mentioned TSQ . I think this is not good method ,because it wastes memory space . If using thread lock , I am afraid that the system performance would be affected . I am no idea , please give me a help .

In addition , I want to post a mes
sage from one thread to another , and there is also a API named � PostThreadMessage()� in SDK , can I use it directly without using �RegisterWinMsgCallback()�? If it can , does it still need to use "PeekMessage()" to force the system to create the message queue ? Your advise is highly appreciated.

David
0 Kudos
Message 6 of 7
(4,164 Views)
Please see the reply to Chris . Thank you .
0 Kudos
Message 7 of 7
(4,164 Views)