Win32API多线程编程 培训(英文).ppt_第1页
Win32API多线程编程 培训(英文).ppt_第2页
Win32API多线程编程 培训(英文).ppt_第3页
Win32API多线程编程 培训(英文).ppt_第4页
Win32API多线程编程 培训(英文).ppt_第5页
已阅读5页,还剩32页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Bryan Li 6/5/2007,Agenda,Windows OS history Whats Kernel Object Abstraction Processes, Thread Events, Semaphores, Mutexes MultiThread mode in Win32 GUI thread vs. Gui thread GUI thread vs. worker thread Pricinple,Windows OS history,MS-Dos (single tasking) Windows 1.x 2.x 3.x (cooperative multitaskin

2、g) WindowsNT (32bits, preemptive multitasking),Kernel Object,Definition: Each kernel object is simply a memory block allocated by the kernel and is accessible only by the kernel. In fact kernel object is similar to GDI object such as DC, brushes. However kernel object is controlled by KERNEL32.DLL;

3、GDI object is controlled by GDI32.DLL. Kernel Object includes process, thread, mutexes, semophores, event and so on.,Kernel Object,OS Resource,process,thread,event,mutex,semaphore,file,pipe,Kernel Object,GDI Object,DC,Brusher,.,.,Kernel Object,Process and Thread Definition: A process a collection of

4、 virtual memory space, code, data, and system resources. A thread is routine that is to be serially executed within a process. A processor executes threads, not processes. Difference: Every process have its own data memory location but all related threads can share same data memory and have their ow

5、n individual stacks.,Memory space,code,data,System resources,Stack 1,Stack 2,Process,Intrduction of relevant WinAPI,Creat thread _beginthread(.) -bad deprecated, hasnt some setting optinal(less parameters) such as suspending thread, adjusting priority, etc. _beginThreadex(.) but sub thread has not.

6、Main thread at least need do two things: create sub thread and wait till sub thread ends Sub thread has 3 ways to communicate with main thread: Using thread call back function parameter Using global data Send message to windows handle or main thread. Prefer to use asynchronous function to send messa

7、ge.,MultiThread mode in Win32,GUI thread vs. worker thread,Message queue,Main thread,sub thread,Window handle,message queue,Main thread,Window handle,sub thread,Global data,Int flag; char* buffer;,Send message,PostThreadMessage,Example:Philosophers Dinner,Desc: 10 philosophers want to take a dinner,

8、 but there are only 10 chopsticks between them. How should they take a dinner? Solution1: everyone picks up first left chopsticks second right one on them.- the possibility of deadlock Solution2: everyone picks up left and right chopsticks one time.- the impossibility of deadlock,Philosophers Dinner

9、,Implementation: Subthread means philosophers. Mutex is used to pick up or put down chopstick. Sleep() means the action of eating or resting.,/define global variable const static int PHILOSOPHER_NUM = 20; typedef unsigned (WINAPI * PTHREAD_FUNC)(void* lpParam); void philosopherFun(int indx); / callb

10、ack function fro sub thread HANDLE gChopstick20 = 0; / mutex handles imitate chopsticks HANDLE lock = CreateMutex(NULL, FALSE/*bInitialOwner*/, NULL/*name*/); / use to atomize some action,Philosophers Dinner,int main(int argc, char* argv) for(int i = 0; i PHILOSOPHER_NUM; i+) /create mutexs gChopsti

11、cki = CreateMutex(NULL, FALSE/*bInitialOwner*/, NULL/*name*/); HANDLE philosopherPHILOSOPHER_NUM; for(int i = 0; i PHILOSOPHER_NUM; i+) /create phiosophers, that is threads philosopheri = (HANDLE)_beginthreadex(NULL, 0, (PTHREAD_FUNC)philosopherFun, (void* )i, 0, NULL ); /wait till philosophers all

12、finish their dinners WaitForMultipleObjects(PHILOSOPHER_NUM, philosopher, TRUE, INFINITE); for(int i = 0; i PHILOSOPHER_NUM; +i) CloseHandle(philosopheri); system(pause); return EXIT_SUCCESS; ,Philosophers Dinner,/call back function for thread, that is philosophers action void philosopherFun(int ind

13、x) int currentIndx = indx; HANDLE myChopsticks2; /left and right chopsticks for(int i = 0; i 2; i+) myChopsticksi = gChopstick(currentIndx + i) % PHILOSOPHER_NUM; /they shoud finish dinner within 15 seconds. int end = time(NULL) + 15; while(time(NULL) - end 0) WORD rc = WaitForMultipleObjects(2/*cou

14、nt*/, myChopsticks, TRUE/*bWaitAll*/, INFINITE); if(WAIT_OBJECT_0 = rc) WaitForSingleObject(lock, INFINITE); cout philosopher currentIndx : get a couple of chopstick. endl; Sleep(rand() % 500); /eating ReleaseMutex(lock); ReleaseMutex(myChopsticks0); ReleaseMutex(myChopsticks1); Sleep(rand() % 500);

15、 / resting ,Philosophers Dinner,MultiThread mode in Win32,GUI thread vs. GUI thread Main thread and sub thread both are GUI thread. So they both has their own message queue. When worker thread calls PeekMessage() or GetMessage(), OS automatically create message queue for worker thread. At this time,

16、 worker thread changes to GUI thread. Interaction way between main thread and sub thread: Send message to sub thread(good) Using thread call back function parameter Send message to windows handle or main thread. Prefer to asynchronously send message. (good) Using global data(bad),MultiThread mode in

17、 Win32,message queue,message queue,Main thread,sub thread,Window handle,After finish creating message queue,Send message to main thread,MultiThread mode in Win32,message queue,message queue,Main thread,sub thread,Window handle,CreatEvent,PostThreadMessage(),Event guarantee creating message queue,Exa

18、mple:Philosopher and Girl,Desc: a philosopher love a girl, he wants to propose to her. In the begining, the girl always refuses him. Several times later, eventually the girl approves his proposal.So they have a honey life. Imitation: Main thread means the girl Sub thread means the philosopher PostTh

19、readMesssage() means their communication,Philosopher and Girl,typedef unsigned (WINAPI * PTHREAD_FUNC)(void* lpParam); /philosohpers action void philosopherFun(void* para) ; /philosopher propose to girl void propose(unsigned girlAddr) ; /girl respond to philosopher void reponse(unsigned philosopherA

20、ddr) ; /girl approve philosophers proposal void approve(unsigned philosopherAddr) ; / means messages are from philosopher. const int WM_MSG_FORM_PHILOSOPHER = WM_USER + 1; / means messages are from girl. const int WM_MSG_FORM_GIRL = WM_USER + 2; / means girl approve his proposal const int WM_MSG_APP

21、ROVE = WM_USER + 3; /the time that philosopher propose to the girl. int proposalTime = 0; HANDLE eventFlag = CreateEvent(NULL, TRUE, FALSE, NULL);,Philosopher and Girl,int _tmain(int argc, _TCHAR* argv) unsigned girlAddr = GetCurrentThreadId(); unsigned philosopherAddr = 0; HANDLE philosopher = (HAN

22、DLE)_beginthreadex(NULL, 0, (PTHREAD_FUNC)philosopherFun, (void*)girlAddr, 0, ,Philosopher and Girl,/philosohpers action void philosopherFun(void* paraGirlAddr) unsigned girlAddr = (unsigned)paraGirlAddr; MSG msg; /create message queue PeekMessage( ,Philosopher and Girl,/philosopher propose to girl

23、void propose(unsigned girlAddr) Sleep(rand() % 500); / writing letter string* response = new string(I love you, please marry me.); PostThreadMessage(girlAddr, WM_MSG_FORM_PHILOSOPHER, 0, (LPARAM)response); /girl respond to philosopher void reponse(unsigned philosopherAddr) Sleep(rand() % 500); / wri

24、ting letter string* response = new string(I am shy becuase I am girl.); BOOL bRec = PostThreadMessage(philosopherAddr, WM_MSG_FORM_GIRL, 0, (LPARAM)response); /girl approve philosophers proposal void approve(unsigned philosopherAddr) Sleep(rand() % 500); / writing letter PostThreadMessage(philosophe

25、rAddr, WM_MSG_APPROVE, 0, 0); ,Philosopher and Girl,The running result :,Suggestion,If you want sub-thread to have message queue, please exactly call PeekMessage() in sub-thread and ensure that no other thread called PostThreadMessage() to this thread before PeekMessage() . If you use MFC, in sub-thread, please be careful to Call API related to GDI, sometimes, strange debug spoil you because some of these API are directly or non-directly related to main thread handle. You should try your best to use Event kernel object, instead of

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论