One-day training of Windows APIs (42) createthread Function

Source: Internet
Author: User
With the development of the times, computer technology has developed rapidly. The CPU has changed from single-core to multi-core, that is, a CPU has the ability to do multiple things at the same time, instead of using time-sharing, but actually doing multiple things. Therefore, software development has entered a new era, that is, the development of multi-threaded software. How to reasonably allocate multiple threads to run at the same time is a key factor to improve software efficiency. For example, a client such as an online game can use a thread to constantly update the game interface and allocate another thread to continuously send and receive network data, in this way, both CPU cores are constantly working. If you use a thread as before, you will find that only one kernel is doing things. Of course, the use of the thread programming model will greatly simplify the complexity of the software. The following describes how to use the thread's API functions. The createthread function declares the following: winbaseapi _ partition (_ in_opt partition, _ in size_t dwstacksize, _ in partition lpstartaddress, _ in_opt lpvoid lpparameter, _ in DWORD dwcreationflags, _ out_opt lpdword lpthreadid ); LpthreadattributesIs the attribute of the thread. DwstacksizeIs the stack size of the thread. LpstartaddressIs the start address of the thread function. LpparameterIs the parameter passed to the thread function. DwcreationflagsIs the sign for creating a thread, such as suspending a thread. LpthreadidIs the ID of the thread. An example of calling this function is as follows: #001 // #002 // The thread runs the function. #003 // Cai junsheng 2007/09/21 #004 // #005 static DWORD winapi threadproc (#006 lpvoid lpparameter #007) #008 {# 009 // output to the debugging window. #010 outputdebugstring (_ T ("threadproc thread function run/R/N"); #011 #012 // thread return code. #013 return 0; #014} #015 #016 #017 #018 // #019 // response command. #020 // Cai junsheng 2007/09/21 QQ: 9073204 #021 // #022 lresult ccaiwinmsg: oncommand (int nid, int nevent) #023 {#024 // menu option command response: #025 switch (NID) #026 {#027 case idc_createbtn: #028 // display a button. #029 if (! M_hbtn) #030 {#031 m_hbtn = createwindow (_ T ("button"), _ T ("button"), #032 ws_visible | ws_child | bs_pushbutton, #033 50,50, 100,32, #034 m_hwnd, (hmenu) idc_btn, m_hinstance, null); #035} #036 break; #037 case idc_btn: #038 {#039 // simple parameter passed to the thread. #040 int nparam = 110; #041 #042 // the ID of the Save thread. #043 DWORD dwthreadid = 0; #044 #045 // create a thread. #046 handle hthread = createthread (#047 null, // use the default security attribute. #048 0, // The stack size of the thread. #049 threadproc, // address of the thread running function. #050 & nparam, // parameters passed to the thread function. #051 0, // create a flag. #052 & dwthreadid); // ID of the created thread. #053 #054 // wait for the end of the thread. #055 waitforsingleobject (hthread, infinite); #056 #057 // Delete the thread resource. #058 closehandle (hthread); #059 #060 // #061 outputdebugstring (_ T ("click/R/N"); #062 #063} #064 break; #065 default: #066 return ccaiwin: oncommand (NID, nevent); #067 }# 068 #069 return 1; #070}

 

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.