Use of the thread pool for Windows programming

Source: Internet
Author: User

Have to say, do C + + server program development, if do not understand the thread pool, do not understand the thread pool, do C + + server-side program has no meaning. Especially the last time because I understand the wrong thread pool to do a wrong thing, and was instructed to a meal, and still fresh memory, so take advantage of the weekend to learn the use of the thread pool, small results.

Let's look at a simpler implementation of the thread pool.

1#include <windows.h>2#include <string>3#include <iostream>4 5 using namespacestd;6 7 #defineBeginthread (Fun,param) CreateThread (null,0, (lpthread_start_routine) fun,param,0,null);8 9 DWORD WINAPI ThreadProc (LPVOID lpparameter);Ten  One intMain () A { -  -     stringwords[Ten] = {"I am a gay ... ..","I am a gay, too","Let us do something exceting","Ah, do you want to does it in the forest or the the bed?","neither, I want to does it on the street. Haha", the                         "I heard you like music.","Yeah, Just like the light music.","For example ...","bosty Music and so on","Ah, you mean that's the light music ...." }; -  -      for(inti =0; I <Ten; i++) -     { +         //The following code demonstrates that using CreateThread and QueueUserWorkItem, the actual effect -         //is the same, of course, if there are not many threads, be sure to use QueueUserWorkItem if there are many threads +         QueueUserWorkItem (ThreadProc, (PVOID) &words, wt_executelongfunction); A  at         //display the effect of using CreateThread to create multiple threads -Beginthread (ThreadProc, (LPVOID) &words[i]); -     } -  -      while(true); -  in     return 0; - } to  + //The function can be started by a thread of createthread, or it can be started using a thread in the QueueUserWorkItem thread pool - DWORD WINAPI ThreadProc (lpvoid lpparameter) { the      while(true) *     { $STD::stringWord = * (std::string*) Lpparameter;Panax NotoginsengStd::cout <<"thread [ID:"<< GetCurrentThreadID () <<"] said:"<< Word <<Std::endl; -Sleep (5* +); the     } +     return 0; A}

Here we do not use any thread pool information, we can directly use the thread pools, because we use the method in the default to create a thread pool, but in the actual development of the server, we must set the size of the thread pool, you must customize the thread pool properties, you must set the thread pool information, At this point, we need to manually make the thread pool.

Run:

From this image can also be seen in the thread pool execution of the situation with our custom multi-threaded or there is a big difference, multi-threaded output statements are not interrupted, but look at our program, even the output statement was interrupted. (Specific multi-threaded inside the output statement why not interrupted, at present I do not know, supposedly should be interrupted)

Not finished, to be continued ....

Use of the thread pool for Windows programming

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.