LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

qsort

I'd like to break 'qsort' function when it runs. Is it possible ?

In specific cases it works over 10 minutes so program doesn't response for
user requests.

void qsort (void *Base_Array_Element, size_t Number_of_Elements, size_t
Element_Size,
int (*Comparison_Function)(const void *, const void *));


1. The simplest way is specific value returned by 'Comparision_funtion'
wchich means 'break sorting'

2. Second solution is to write qsort independently.

What should I do?

Regards
Jaroslaw Przywieczerski
0 Kudos
Message 1 of 3
(3,178 Views)
In my opinion the most effective solution to your problem is to run "qsort' from another thread.
If necessary apply some thread synchronization techniques (e.g. mutexes) to avoid reading/writing the list during the execution of qsort form the "sorting thread".
If you exploit the thread function parameter, you can also run two or more threads to sort different set of data at the same time. For performance reasons avoid more than 16 threads in the same process.

Although CVI 5.5 provides the support for multithrading, I prefer using the native Win32 SDK function (see CreateThread, CreateMutex, CloseHandle, etc.).

Bye
Giovanni
Message 2 of 3
(3,178 Views)
Hello Reks,
An easy way to restore responsiveness to the user interface while a qsort is running would be to put ProcessSystemEvents() into the comparison function. In this way, every time the comparison function is called, the user interface would be able to process one event. However, this might call ProcessSystemEvents() too often and unnecessarily slow down your qsort. As such, you might add some code such that it is only called every 10 or 100 comparisons (depending on your system). A better solution that should give better UI responsiveness and faster qsort processing would be to use multiple threads. When the user initiates a qsort (by pressing a button on the UI), dim the button and possibly light an LED to let the user know that the sort is taking place
, then create another thread to perform the qsort. The CVI Multithreading functions are located under Library >> Utility >> Multithreading. We have a good introduction document available online at http://www.ni.com by searching for "+multithread +C" that is titled "General Information on Multithreading for C Programmers". Example multithreading programs are located under \samples\utility\Threading. The CVI function that you would most likely use is CmtScheduleThreadPoolFunctionAdv(). I mention the "Adv"anced function because you may want to give your qsort thread slightly higher thread priority so that it completes faster (Above Normal for example).

Jeremiah Cox
Applications Engineer
National Instruments
http://www.ni.com/ask
Message 3 of 3
(3,178 Views)