Use the thread pool provided by the system in VC

Source: Internet
Author: User

Multithreading is often used in projects. A large number of customers send requests to the server. In this case, threads need to be opened to respond to customer requests. because the number of customers is unpredictable, is there a new thread to respond to a client request? The answer is no. You need to know that it takes quite a long time and resources to open up and undo the thread, so the predecessors have developed many models to solve this problem, where thread pooling is used) it is a good idea. The thread pool is well supported in Windows2000 and later versions. It is convenient and efficient to use the thread pool mechanism provided by the system. We only need to focus on our callback functions. We can implement the thread pool mechanism on our own, but compared with our simple simulation, the thread pool provided by the system has more advantages. First, the number of threads in the thread pool is dynamically adjusted. Second, the thread pool uses the IO to complete the port feature, which can limit the number of concurrent running threads. By default, it will limit the number of CPUs, which can reduce thread switching. It selects the threads that have been executed recently and puts them into execution again, thus avoiding unnecessary thread switching. A huge policy is hidden behind the thread pool provided by the system.

The following is an example of using the system thread pool. The workitem function in this example works like this. It first detects the passed parameters. If it is true, the latency is 1 second, false: the compute function is used to generate a bunch of random numbers (these random numbers are not used here, just to delay the time ^_^). The queueuserworkitem API function is used to use the thread pool provided by the system, this function is used to add your own function to the system thread pool and let the system create a thread to execute your function, you do not need to perform these cumbersome tasks to create and cancel threads. You only need to maintain your own function.

# Include <windows. h> <br/> # include <iostream> <br/> # include <tchar. h> <br/> using namespace STD; </P> <p> DWORD begintime; <br/> long itemcount; <br/> handle completeevent; </P> <p> int compute () <br/>{< br/> srand (begintime); </P> <p> for (INT I = 0; I <20*1000*1000; I ++) <br/> rand (); </P> <p> return rand (); <br/>}</P> <p> DWORD winapi workitem (lpvoid lpparameter) <br/>{< br/> bool bwaitmode = (bool) lpparameter; </P> <p> If (bwaitmode) <br/> sleep (1000); <br/> else <br/> compute (); <br/> cout <itemcount <Endl; <br/> If (interlockeddecrement (& itemcount) = 0) // guarantee atomic operation <br/>{< br/> cout <"time total" <(gettickcount ()-begintime)/1000.0 <"seconds. /n "<Endl; <br/> setevent (completeevent); <br/>}< br/> return 0; <br/>}< br/> void testworkitem (bool bwaitmode, DWORD flag) <br/>{< br/> completeevent = createevent (null, false, FAL Se, null); <br/> begintime = gettickcount (); <br/> itemcount = 100; <br/> hinstance hinst = loadlibrary (_ T ("kernel32.dll"); <br/> If (hinst) <br/>{< br/> typedef bool (winapi * myfunc) (lpthread_start_routine, pvoid, ulong); <br/> myfunc myqueueuserworkitem = NULL; </P> <p> // obtain the queueuserworkitem function pointer <br/> myqueueuserworkitem = (myfunc) getprocaddress (hinst, "queueuserworkitem"); <br/> If (! Myqueueuserworkitem) <br/>{< br/> MessageBox (null, _ T ("function address retrieval failed", "Hello! "), _ T (" error prompt "), mb_ OK); <br/> return; <br/>}</P> <p> for (INT I = 0; I <100; I ++) <br/>{< br/> myqueueuserworkitem (workitem, (pvoid) bwaitmode, flag ); <br/>}< br/> freelibrary (hinst); <br/>}</P> <p> waitforsingleobject (completeevent, infinite ); <br/> closehandle (completeevent); <br/>}< br/> int main () <br/>{< br/> testworkitem (0, wt_executeiniothread ); <br/> cout <"The work item has been executed! "<Endl; <br/> return 0; <br/>}

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.