Windows平台下的多线程编程(计算机论文翻译).doc
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Windows 平台 多线程 编程 计算机论文 翻译
- 资源描述:
-
附录A Under theWindowsplatform, multi-threaded programming A thread is a process execution path, it contains separate stack andCPUregister state, the process of each thread to share all resources, including open files, signal identification and dynamic allocation of memory and so on.All threads within a process using the same address space, and these threads of execution by the system scheduler control thread scheduler to decide which and when to execute the executable threads.Thread has priority, lower priority thread must wait until the higher priority thread after their implementation.In the multi-processor machines, the scheduler can be multiple threads running up into different processors, it will give the processor the task balance, and improve efficiency of the system. Windowsis a multitasking operating system, a process inWindowscontains one or more threads.32-bit WindowsenvironmentWin32 APIprovides a multi-threaded application development interface functions required, and use of VC in the standard C libraries are also provided to develop multithreaded applications, the corresponding MFC class library encapsulates the class of multi-threaded programming users in the development can be based on the needs and characteristics of the application select the appropriate tool.In order so that we can fully understand theWindowsmulti-threaded programming techniques, this article will focus onWin32 APIandMFCare two ways of how to prepare multi-threaded programs. Multithreaded Programming inWin32andMFCclass library under way to support the principle is the same, the main thread of the process at any time needed to create a new thread.When the thread has finished, automatically terminate the thread; when the process is complete, all threads are terminated.Threads share the process of all activities of resources, therefore, be considered when programming multiple threads access the same resource conflict problem.When a thread is accessing a process object, and another thread to change the object, it may produce incorrect results, programming to solve this conflict. Under the multi-threadedWin32 APIprogramming: Win32 APIis theWindowsoperating system kernel and the interface between applications, which will provide the functions of the kernel function wrapper, the application obtained by calling the correlation function of the corresponding system function.In order to provide multi-threading applications,Win32 APIfunction provides some focus on procedures for handling multi-threaded set of functions.Directly with theWin32 APIto program design has many advantages: Win32-based application code execution is small, high efficiency, but it requires programmers to write code more, and all systems need to manage the resources available to the program.Programming with theWin32 APIdirectly on theWindowssystem kernel programmer is required to have a certain understanding, it will take a lot of time on the programmer to manage system resources, thus reducing the efficiency of the programmer. 1.With theWin32function to create and terminate threads Win32function library provides a function of multi-threaded operation, including creating threads, terminate threads, the establishment of exclusive zones.In the application's main thread or other activities to create a new thread thread function is as follows: HANDLE CreateThread(LPSECURITY_ATTRIBUTES lpThreadAttributes,DWORDdwStackSize,LPTHREAD_START_ROUTINElpStartAddress,LPVOIDlpParameter,DWORDdwCreationFlags,LPDWORDlpThreadId);HANDLE CreateThread (LPSECURITY_ATTRIBUTES lpThreadAttributes, DWORD dwStackSize, LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter, DWORD dwCreationFlags, LPDWORD lpThreadId); If you create a successful thread handle is returned, otherwise NULL.Create a new thread, the thread started to execute.However, if used indwCreationFlags CREATE_SUSPENDEDproperty, then the thread is not immediately executed, but the first suspended, until the calls started afterResumeThreadthreads in the process can call the following function to set the priority of the thread: BOOL SetThreadPriority(HANDLE hThread,intnPriority);BOOL SetThreadPriority (HANDLE hThread, intnPriority); When the function returns the calling thread, the thread automatically terminates.If you need to terminate during the execution of the thread can call the function: VOID ExitThread(DWORD dwExitCode);VOID ExitThread (DWORD dwExitCode); If you terminate a thread in the thread on the outside, you can call the following function: BOOL TerminateThread(HANDLE hThread,DWORDdwExitCode);BOOL TerminateThread (HANDLE hThread, DWORD dwExitCode); It should be noted: this function may cause system instability, and the thread does not release the occupied resources.Therefore, under normal circumstances, it is recommended not to use this function. If you want to terminate the thread is the last thread within the process, the thread is terminated after the corresponding process should be terminated. 2.Thread synchronization In the thread body, if the thread is completely independent data access with other threads and other resources without conflict operation, the method can be carried out in accordance with generally single-threaded programming.However, the situation in multi-threaded processing is often not the case, often between threads simultaneously access some resources.Because access to shared resources causes conflict is inevitable, in order to address this thread synchronization problems,Win32 APIprovides a variety of synchronization objects to help programmers access to shared resources to resolve the conflict.In presenting these synchronization objects introduce wait functions before, because all the control object's access control should use this function. Win32 APIprovides a set of threads can block waiting for the implementation of its own function.These functions in its argument object generates one or more synchronization signal, or exceeds the waiting time will be returned.Waiting function is not returned, the threads in a wait state, when the thread consumes very littleCPUtime.Not only can guarantee the wait function using thread synchronization, but also can improve the running efficiency.Waiting function is the most commonly used: DWORD WaitForSingleObject(HANDLE hHandle,DWORD dwMilliseconds);DWORD WaitForSingleObject (HANDLE hHandle, DWORD dwMilliseconds); The function can be used to simultaneously monitor multipleWaitForMultipleObjectsynchronization object, the function is declared as: DWORD WaitForMultipleObject(DWORD nCount,CONST HANDLE *lpHandles,BOOLbWaitAll,DWORDdwMilliseconds);DWORD WaitForMultipleObject (DWORD nCount, CONST HANDLE * lpHandles, BOOL bWaitAll, DWORD dwMilliseconds); (1)mutex object Mutexstate of the object by any thread in that it does not have a time signal, it was a time when no signal.Mutexobjects are suitable for coordination of multiple threads exclusive access to shared resources.The following steps can use the object: First, create a mutex object, get the handle: HANDLE CreateMutex();HANDLE CreateMutex (); Then, in the thread before the possible areas of conflict (ie, before access to shared resources) calls WaitForSingleObject, the handle to the function, the request takes mutex object: dwWaitResult = WaitForSingleObject(hMutex,5000L);dwWaitResult = WaitForSingleObject (hMutex, 5000L); Shared resources conclusion of the visit, the release of the occupation of the mutex object: ReleaseMutex(hMutex);ReleaseMutex (hMutex); Mutex object at the same time can only be occupied by a thread, when the mutex object is occupied by a thread when another thread would like to take it if you must wait until after the release of the previous thread to succeed. (2)signal object Signal objects allow multiple threads simultaneously access shared resources in the creation of object may also specify the maximum number of threads access.When a thread successfully apply for access to the signal by a counter object, callReleaseSemaphorefunction, the signal object counter plus one.Among them, the counter value is greater than or equal to 0, but less than or equal to the maximum specified when created.If an application to create a signal object, the initial value of its counter is set to 0 to block the other thread to protect the resources.And other initialization is complete, callReleaseSemaphorefunction will increase to its maximum value the counter, you can access a normal visit.The following steps can use the object: First, create the signal object: HANDLE CreateSemaphore();HANDLE CreateSemaphore (); A signal or open object: HANDLE OpenSemaphore();HANDLE OpenSemaphore (); Then, access to shared resources in the thread before calling WaitForSingleObject. After access to shared resources should be occupied by the release of the signal objects: ReleaseSemaphore (); (3) the event object Event object(Event)is the simplest synchronization objects, which includes both signal and no signal state.Threads access a resource, you need to wait for some event to occur, then the most appropriate to use the event object.For example: only the data received in the communication port buffer, the monitor thread is activated. The event object is created withCreateEventfunction.This function can be specified event object class and the initial state of the event.If the manual reset event, it always maintained a signal pick-likeBa BanesetEventfunction reset to no signal events.If the event is automatically reset, then its state after the release of a single waiting thread will automatically become free signal.SetEventWithSetEventcan set the event object to signaled.OpenEventIn the establishment of event, named for the object, so that other threads in the process can be used to open the specified function nameOpenEventevent object handle. (4)exclusion zone object Asynchronous execution in the exclusion zone, it can only be in the same process of dealing with shared resources between threads.At a time when several methods described above can be used, however, the use of exclusion zones approach makes more efficient synchronization management. CRITICAL_SECTIONused to define a structure of exclusion zone object is called before the process using the following function to initialize the object: VOID InitializeCriticalSection(LPCRITICAL_SECTION);VOID InitializeCriticalSection (LPCRITICAL_SECTION); When a thread uses the exclusion zone, call the function:EnterCriticalSectionorTryEnterCriticalSection; When asked to take exit exclusion zone, call the function LeaveCriticalSection, the release of the exclusion zone occupied by the object for other threads. MFC-based multi-threaded programming: Microsoft's VC+ + MFCis to provide an integrated environment based library for programmers, it uses the class library to encapsulateWin32 APIway to class provided to developers.Because of its fast, simple, powerful and so loved by the majority of developers.Therefore, it is recommended to useMFCclass library application development. InVC + +with theMFCclass library, provides support for multithreaded programming, basic principles and design based on the sameWin32API, butMFCsynchronization objects on a wrapper, so to achieve more convenient to avoid the object handle management the tedious work. In MFC, the thread is divided into two types: worker threads and user interface thread.Work earlier in the thread with the same thread, the thread is a user interface to receive user input, handle events, and message threads. 1.Worker thread Relatively simple work-thread programming, design ideas and the mentioned earlier the same: a basic function represents a thread, create and start the thread, the thread into the running state; if the thread is used to share resources, the need for resource synchronization.In this way create a thread and start the thread can call the function: CWinThread*AfxBeginThread( AFX_THREADPROC pfnThreadProc, LPVOID pParam,intnPriority= THREAD_PRIORITY_NORMAL,UINT nStackSize =0,DWORD dwCreateFlags=0, LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL);UINT ThreadFunction( LPVOID pParam)。CWinThread * AfxBeginThread (AFX_THREADPROC pfnThreadProc, LPVOID pParam, intnPriority = THREAD_PRIORITY_NORMAL, UINT nStackSize = 0, DWORD dwCreateFlags = 0, LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL);parameterpfnThreadProcis the thread of execution, function prototype is:UINT ThreadFunction (LPVOID pParam). Parameter is passed to the implementation ofpParamargument; Parameter is the threadnPriorityrights, optional values: HREAD_PRIORITY_NORMAL、THREAD_PRIORITY_LOWEST、THREAD_PRIORITY_HIGHEST、THREAD_PRIORITY_IDLE。THREAD_PRIORITY_NORMAL, THREAD_PRIORITY_LOWEST, THREAD_PRIORITY_HIGHEST, THREAD_PRIORITY_IDLE. ParameterdwCreateFlagsis a sign of the thread is created, you can value CREATE_SUSPENDED, that thread is created in a suspended state, calledResumeThreadthread continues to run after the function, or the value of "0"indicatesa thread is created after running. CWinThreadclass object return value is a pointer, its member variablesm_hThreadfor the thread handle, in the way ofWin32 APIfunctions that operate under the parameters of the thread are required to provide a handle to the thread, so when the thread is created you can use all theWin32 APIfunctionpWinThread -> m_Threadthread related operations. Note: If a class object to create and start the thread, the thread function should be defined as global functions outside the class. 2. Theuser interface thread MFC-based applications have an application object, which isCWinAppderived class object that represents the application process, the main thread.When the thread and exit the thread is executed, because no other threads in the process, the process ends automatically.ClassCWinApp derived from theCWinThread,CWinThread user interface thread is the basic class.We write the user interface thread, you need to derive fromCWinThread thread of our own class,ClassWizardcan help us do this work. First useClassWizardto derive a new class, set the base class CwinThread.Note: The classDECLARE_DYNCREATEandIMPLEMENT_DYNCREATEmacros are necessary because you need to create threads dynamically created class object.Necessary initialization and exit code can be placed in the class, respectively, andExitInstancefunction InitInstance.If you need to create a window, can be done in theInitInstancefunction.And then create a thread and start the thread.Two methods can be used to create the user interface thread,MFCprovides two versions of theAfxBeginThreadfunction, one used to create the user interface thread.The second method is divided into two steps: first, the calling thread class constructor creates a thread object; Second, callCWinThread::CreateThreadfunction to create the thread.After the establishment and start the thread, the thread function in the process of implementation has been effective.If the thread object,展开阅读全文
咨信网温馨提示:1、咨信平台为文档C2C交易模式,即用户上传的文档直接被用户下载,收益归上传人(含作者)所有;本站仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。所展示的作品文档包括内容和图片全部来源于网络用户和作者上传投稿,我们不确定上传用户享有完全著作权,根据《信息网络传播权保护条例》,如果侵犯了您的版权、权益或隐私,请联系我们,核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
2、文档的总页数、文档格式和文档大小以系统显示为准(内容中显示的页数不一定正确),网站客服只以系统显示的页数、文件格式、文档大小作为仲裁依据,个别因单元格分列造成显示页码不一将协商解决,平台无法对文档的真实性、完整性、权威性、准确性、专业性及其观点立场做任何保证或承诺,下载前须认真查看,确认无误后再购买,务必慎重购买;若有违法违纪将进行移交司法处理,若涉侵权平台将进行基本处罚并下架。
3、本站所有内容均由用户上传,付费前请自行鉴别,如您付费,意味着您已接受本站规则且自行承担风险,本站不进行额外附加服务,虚拟产品一经售出概不退款(未进行购买下载可退充值款),文档一经付费(服务费)、不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
4、如你看到网页展示的文档有www.zixin.com.cn水印,是因预览和防盗链等技术需要对页面进行转换压缩成图而已,我们并不对上传的文档进行任何编辑或修改,文档下载后都不会有水印标识(原文档上传前个别存留的除外),下载后原文更清晰;试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓;PPT和DOC文档可被视为“模板”,允许上传人保留章节、目录结构的情况下删减部份的内容;PDF文档不管是原文档转换或图片扫描而得,本站不作要求视为允许,下载前可先查看【教您几个在下载文档中可以更好的避免被坑】。
5、本文档所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用;网站提供的党政主题相关内容(国旗、国徽、党徽--等)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
6、文档遇到问题,请及时联系平台进行协调解决,联系【微信客服】、【QQ客服】,若有其他问题请点击或扫码反馈【服务填表】;文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“【版权申诉】”,意见反馈和侵权处理邮箱:1219186828@qq.com;也可以拔打客服电话:0574-28810668;投诉电话:18658249818。




Windows平台下的多线程编程(计算机论文翻译).doc



实名认证













自信AI助手
















微信客服
客服QQ
发送邮件
意见反馈



链接地址:https://www.zixin.com.cn/doc/4514750.html