LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How does thread scheduling work in CVI?

Hi,

I have written a multi-threaded application in CVI 6.0 but am unhappy with the performance of the GUI. I think it may be because of the way I am locking variables / thread priority and would welcome some advice.

I have the main thread (GUI), an async timer on a 3 second interval that polls a serial device and waits for it to respond, an analogue DAQ running continuously in its own thread and finally two counter DAQs only one of which runs at a time, again in its own thread.

I am using the default thread pool so have not specifically prioritised the threads. Would it be helpful to lower the priority of the DAQ threads (I assume the main thread priority cannot be changed)?

I am making heavy use of a global structure t
hat I obtain a pointer to in most of the secondary threads. In general I obtain the pointer at the start of a processing loop and release it at the end, this is mainly done to ensure data coherence when using the structure data. My main questions are:

a) Would I see a significant performance boost if I made a local copy of the structure at each loop iteration then immediately released the pointer before doing processing on the local copy, any write back to the structure occuring at the end?

b) If another thread function blocks when it tries to get a pointer to the structure while the other thread function has it, will it always execute as soon as the first thread releases the pointer, or is there a chance that the first thread will release the pointer then re-obtain it at the next loop iteration without a thread-switch occuring in between. In short, are such requests for the pointer queued and processed in order?

With thanks in advance for any help given.

Regards

Jamie F
raser
0 Kudos
Message 1 of 2
(3,019 Views)
Hello

DAQ should always be running at the same or a higher priority if you want to keep the data transfer rate high and/or avoid any buffer overflow problems.

I doubt making local copies would provide any dvantage in terms of performance , since now you added the overhead of making the copy. Not to mention adding the overhead of thread local variables into the equation might actaully slow things down for you a little more. You could, however, make sure that you lock sections that you absolutely need to. But I guess this would start depending on your application and how that is set up. But generally make sure your critical sections contain as few instructions as possible.

If you are expierencing sluggish UI behavior, you might also want to check that you
are making good use of ProcessSystemEvents() in your app.

If the first thread does go ahead a lock the global resource before using it, you want to make sure that it releases it only after its done using it. You should not grab and release a lock in every iteration of a while loop, simply becuase its not efficient and the first thread could get preempted at any time by any of the other threads that you created. There is no guarantee of which thread you could be preempted by, since this is something the OS is incharge of. Windows uses a thread scheduler that makes little distinction between threads that represent different process and threads in the same process. CVI lets the OS handling thread scheduling for it.

If you want more information about the sync primitives available in CVI, check the link here


I hope this helps

Bilal Durrani
NI
Bilal Durrani
NI
0 Kudos
Message 2 of 2
(3,019 Views)