版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 乘用车销售服务合同
- 玩具制作工岗前岗位环保责任制考核试卷含答案
- 业主-客户信息档案表
- 贝类养殖工保密水平考核试卷含答案
- 印制电路镀覆工岗前技术评优考核试卷含答案
- 潜水指导员安全宣传测试考核试卷含答案
- 蜡油渣油加氢装置操作工岗位晋升竞赛考核试卷含答案
- 货运检查员安全生产意识强化考核试卷含答案
- 罐头杀菌工安全知识宣贯水平考核试卷含答案
- 炼厂气加工工岗中知识更新考核试卷含答案
- 小学数学四年级《一亿有多大:从量感到数感》跨学科主题教学设计
- 中小企业管理技能与实务(第三版)全套课件 第1-9章 中小企业的创立-中小企业财务管理
- 《婴幼儿生理基础》全套教学课件
- 2026中国智能电网分布式能源消纳技术路线与经济性比较
- 急诊分级诊疗指南(2025年版)
- 山东省2026年普通高校招生(春季)统一考试数学试题答案
- 中国水利水电第九工程局有限公司2026届秋季招聘148人笔试历年难易错考点试卷带答案解析
- 2026年建筑行业安全事故案例反思与警示
- 2026年二建《水利水电工程实务》真题卷(附答案解析)
- 化工企业重大隐患自查表 AQ3067
- AutoCAD 2016机械制图案例教程
评论
0/150
提交评论