Multi-threaded Programming instance----Multithreading simultaneous sorting _ Multithreading

Source: Internet
Author: User

This is an interesting program, is the use of multithreading using a variety of sorting algorithms at the same time to sort, the following is a screenshot of the main screen:

Think of this program is very simple thinking, nothing more than open 5 threads, and then each thread's entry function corresponding to a sorting algorithm, and then if the user selected is a single thread, then let these several sorting algorithms can be executed serially. But you should pay attention to a problem, how to timing. Single-threaded to say, so many threads of it. The answer lies in WaitForMultipleObjects. This should be easy to understand.

This is a word in the source program: DW = WaitForMultipleObjects (4,hthread,true, 1);

There should be no problem, that is, when all the threads are over, then it's time to calculate the end. But I am not very familiar with the timing, or to see how it timed:

struct _TIMEB timetr1;
struct _TIMEB timetr2;

time_t sec1,sec2;//global variable

Time (&SEC1);
_ftime (&TIMETR1)/time to start

_ftime (&TIMETR2);
Time (&SEC2)/Time End
Long milliseconds= (SEC2-SEC1) *1000+ (TIMETR2.MILLITM-TIMETR1.MILLITM)//Calculation time spent

time_t returns an integer time, in seconds, that requires _TIMEB to compute the millisecond time.

All right, a simpler but more interesting multithreaded routine!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.