LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Hi Chris -- follow up to my question about using TSQ to store strings of different sizes

Thanks for your response.
I did read about those method you mentioned. I have some troubles with each method:

1/ Using TSQ as byte stream.
This is what I chose to use. I basically write to the queue the way you described with an exception about the number of byte to write. I did as followed:

CmtWriteTSQData (tsqHandle, buffer, strlen (buffer),TSQ_INFINITE_TIMEOUT, NULL);

with buffer as the buffer containing my string. Note that strlen(buffer) is not same each time. Also a thread is doing the writing to the queue and another thread is doing the reading. This leads to my next question: How do you read these strings out?

Right now I am reading one
byte at a time with:

while (1)
{
CmtReadTSQData (tsqHandle, tempbuf, 1, 10, 0);
.....
}

in a loop, with tempbuf to contain this single char, and then store it in another buffer until I read out a CR of LF which signals the end of a string.

Will I have any problem?
Could you please provide an alternative?

2/ Using TSQ to contain pointers to strings.
I haven't tried this method since I think it requires allocation of rather large static array of buffer and also requires keeping track of these pointer in a link list.
Would you elaborate how you would use TSQ this way in my case.

3/ Lastly, the way I described in (1) works only if I have the main thread to do writing, and another thread to do reading. Some how it doesn't work when I used one thread to do writing, one thread to do reading, and the main thread to handle user interface. I found out that the program spends all its time in the reading thread (inside the while loop).
Why is it different if the writ
ing thread is the main thread? Do I miss something like priority,... or anything like that?

Thank you so much.
0 Kudos
Message 1 of 2
(2,975 Views)
You really should consider using event driven reading. There is examples of this in the Multithreading examples in cvi\samples\utility\Threading. Then you will get an event in the reader thread whenever a certain number of items are available to read. If you use the byte stream method, you should send the message length with each method so you know how to package the messages back on the reader side.

For the pointer method, you would malloc the buffer on the writer side, send the pointer to the reader, then the reader would free the memory when it is done with the data.

I don't understand why what you are describing in 3 occurs. If you aren't setting priority to something other than default, then each thread should get time. If you want to free up time i
n a thread that is taking to much, just put a small Delay in that thread.

If you want more help on this, you can look at the examples I mentioned above, read the Multithreading Overview available in PDF form in the cvi\bin directory or contact NI support at http://www.ni.com/ask.

Best Regards,

Chris Matthews
Measurement Studio Support Manager
Message 2 of 2
(2,975 Views)