2012年3月21日星期三

Moncler SaleThis problemis actually a multithread scheduling problem

This problemis actually a multithread scheduling problem ,because MFC has given us a good package thread class ,CWinThread class ,so we have to do is simply to each philosopher respectively create process .
Program logic is not very complicated ,but in the use MFC to do the dining philosophers problem demonstration problems, related to the image rendering may be more trouble ,because you want to give each philosopher of the current state of the corresponding icon .
I wrote this is the main interface of the demonstration program ,using graphics display dining philosophers in some state switching ,right the edit box to each philosopher with text .A first demo program ,that portion of the core, CWinThread and CCriticalSection critical area MFC give us a good package of thread type CWinThread for us in procedures to create a new thread ,which provides two threads ,thread and user thread ( specific differences may consult the relevant data ,this is not to say a ) ,the procedure was used in the work thread ( thread is in the background of a large amount of data processing ,does not require a response to the message request ,the user is in thread thread running time also always capture user message request ) .
Work thread to create simple CWinThread * pThread ;pThread-> ;AfxBeginThread ( AFX_THREADPROC pfnThreadProc ,LPVOID pParam ,int nPriority = THREAD_PRIORITY_NORMAL ,UINT nStackSize = 0 ,DWORD dwCreateFlags = 0 ,LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL ) ;function specific parameters can be found in MSDN ,we used is in front of two parameters ,the first is the designated we create thread code being executed ,the second parameter is to be passed to the thread of our data structure .
This procedure because of six philosophers ,so I created 6 threads ,and its designated executive function CWinThread * pthread ;for ( int i = 0 ;i< ;6 ;I + + ) pthread = AfxBeginThread ( Eat ,& ;imageinfo ;Eat ) I am the specified thread execution of code, because the demonstration program six philosophers do are the same ,so each philosopher thread executing code is the same ,imageinfo is I want from the main program passed to a thread of some of the basic data information .
Struct ImageInfo {int index ;/ / index ,said that the current number of philosophers threads executing within the CPoint pt_chair ;/ / chair coordinates CPoint pt_chopstick_middle ;/ / CPoint pt_chopstick_start ;/ / fork coordinate information CPoint pt_chopstick_end ;/ / double radius_chair ;/ / chair radius double radius_table ;/ / table of radius CWnd * pWnd ;/ / the main program window handle CRect rect ;HWND edit ;/ / right corresponding edit box handle } ;UINT Eat ( LPVOID param ) {while ( true ) / / set as wireless loop ,so that the thread continues to run on {ImageInfo * imageinfo = ( ImageInfo * CDC * ) param ;pDC = imageinfo-> ;pWnd-> ;GetDC ( ) ;/ / get the main program window DC / / char textbuff ;/ / GetWindowText ::( imageinfo-> ;edit ,textbuff ,1024) ;CString text ;CString temp ;bool caneat = FALSE ;/ / identification variables ,validation when Former philosopher thread can eat CBrush brush_think ,brush_eat ,brush_wait ,* oldbrush ;/ / a variety of state of the brush and brush CPen, whitepen ,blackpen ,backpen ,oldpen ;whitepen.
CreatePen ( PS_SOLID ,4 ,RGB ( 255255255) ) ;blackpen.CreatePen ( PS_SOLID ,RGB ( 4 ,0,Moncler Sale, 0,Coach Bag, 0) ) ;backpen.CreatePen ( PS_SOLID ,4 ,RGB ( 240240240) ) ;brush_think.CreateSolidBrush ( RGB ( 255, 0, 0) ) ;brush_eat.
CreateSolidBrush ( RGB ( 0255, 0) ) ;brush_wait.CreateSolidBrush ( RGB ( 0, 0255) ) ;CRect rect ;/ / thinking stage temp.Format ( " ;" ;start thinking !) ;/ / text.Format ( textbuff ) ;/ / text = text+" ; R n" ;temp ;SetWindowText ::( imageinfo-> ;edit ,temp ( LPCTSTR ) ) ;oldbrush = ( CBrush * pDC-> ) ;SelectObject ( & ;brush_think ) ;rect.
left = imageinfo-> ;pt_chair.x-imageinfo-> ;radius_chair ;rect.right = imageinfo-> ;pt_chair.x+imageinfo-> ;radius_chair ;rect.top = imageinfo-> ;pt_chair.y-imageinfo-> ;radius_chair ;rect.
bottom = imageinfo-> ;pt_chair.y+imageinfo-> ;radius_chair ;pDC-> ;Ellipse ( rect ) ;/ / num.Format ( " ;%d" ;imageinfo-> ;index+1 ,) ;/ / pDC-> ;DrawText (Num ,rect ,DT_CENTER DT_VCENTERDT_SINGLELINE ) ;pDC-> ;SelectObject ( oldbrush ) ;g_cls.
Lock ( ) ;/ / g_cls for critical variables ,to enter the critical section if ( chopsticks imageinfo-> = 1& ;index ;& ;chopsticks ( imageinfo-> ;index+5 ) %6 = = 1 ) {chopsticks imageinfo-> index = 0 ;chopsticks ;( imageinfo-> ;index+5 ) %6 = 0 ;caneat = TRUE ;} g_cls.
Unlock ( ) ;/ / exit critical Sleep (2000 ) ;if ( FALSE = = caneat ) {temp.Format ( " ;didn forks, unable to eat !" ::SetWindowText ( ;;) ( edit ,imageinfo-> ;LPCTSTR temp ) ;Sleep ( 1000 ) ) ;temp.
Format ( " ;" ;start waiting ... ) ;::SetWindowText ( imageinfo-> ;edit ,temp ( LPCTSTR ) ) ;oldbrush = ( CBrush * pDC-> ) ;SelectObject ( & ;brush_wait ) ;pDC-> ;Ellipse ;pDC-> ;SelectObject ( rect ) ( oldbrush ) ;/ / pDC-> ;DrawText ( Num ,rect ,DT_CENTERDT_SINGLELINEDT_VCENTER ) ;Sleep ( 2000) ;} if ( TRUE = = caneat ) {temp.
Format ( " ;get the two forks ,began to eat !" ::SetWindowText ;) ;( imageinfo-> ;edit ( LPCTSTR ) ,temp ) ;Sleep ( t 1000 ) ;Emp.Format ( " ;" ;imageinfo-> ;eat.
.. ,index ) ;/ / GetWindowText ::( imageinfo-> ;edit ,textbuff ,1024) ;/ / text.Format ( textbuff ) ;/ / text = text+" ; R n" ;temp ;SetWindowText ::( imageinfo-> ;edit ,temp ( LPCTSTR ) ) ;/ / eat state ,picked up the chopsticks and oldpen = ( CPen * pDC-> ) ;SelectObject ( & ;blackpen ) ;pDC-> ( imageinfo-> ;MoveTo ;pt_chair ;pDC-> ) ;LineTo ( imageinfo-> ;pt_chopstick_middle ) ;MoveTo ( pDC-> ;imageinfo-> ;pt_chair ;pDC-> ) ;LineTo ( imageinfo-> ;pt_chopstick_middle ) ;pDC-> ;SelectObject ( oldpen ) ;oldpen = ( CPen * pDC-> ) ;SelectObject ( & ;whitepen ) ;MoveTo ( pDC-> ;imageinfo-> ;pt_chopstick_middle ) ;pDC-> ;LineTo ( imageinfo-> ;pt_chopstick_end ) ;pDC-> ;MoveTo ( imageinfo-> ;pt_chopstick_middle ;pDC-> ( imageinfo-> ;LineTo ) ;pt_chopstick_end ) ;pDC-> ;SelectObject ( oldpen ) ;oldbrush = ( CBrush * pDC-> ) ;SelectObject ( & ;brush_eat ) ;pDC-> ;Ellipse ;pDC-> ;SelectObject ( rect ) ( oldbrush ) ;/ / pDC-> ;DrawText (Num ,rect ,DT_VCENTER DT_CENTERDT_SINGLELINE ( ) ;Sleep ( imageinfo-> ;index+1 ) * 1000 ) ;/ / a meal, put down the chopsticks oldpen = ( CPen * pDC-> ) ;SelectObject ( & ;blackpen ) ;pDC-> ( imageinfo-> ;MoveTo ;pt_chopstick_middle ) ;pDC-> ;LineTo ( imageinfo-> ;pt_chopstick_end ;pDC-> MoveTo ( ) ;imageinfo-> ;pt_chopstick_middle ) ;pDC-> ;LineTo ( imageinfo-> ;pt_chopstick_end ) ;pDC-> ;SelectObject ( oldpen ) ;oldpen = ( CPen * pDC-> ) ;SelectObject ( & ;backpen ) ;MoveTo ( pDC-> ;imageinfo-> ;pt_chair ;pDC-> ) ;LineTo ( imageinfo-> ;pt_chopstick_middle ) ;pDC-> ;MoveTo ( imageinfo-> ;pt_chair ) ;LineTo ( pDC-> ;imageinfo-> ;pt_chopstick_middle ) ;pDC-> ;SelectObject ( oldpen ) ;oldbrush = ( CBrush * pDC-> ) ;SelectObject ( & ;brush_eat ) ;pDC-> ;Ellipse ;pDC-> ;SelectObject ( rect ) ( oldbrush ) ;g_cls.
Lock ( ) ;chopsticks imageinfo-> ; = 1 ;chopsticks ( imageinfo-> ;index+5 ) %6 = 1 ;g_cls.Unlock ( ) ;temp.Format ( " ;after the meal, the fork down !" ::SetWindowText ;) ;( imageinfo-> ;edit ( LPCTSTR ) ,temp ) ;Sleep ( 1000) ;caneat = FALSE ;} } / / ::SetWindowText ( image Info-> ;edit ," ;1" ;return TRUE ;} ) ;thread processing function inside the main is a critical variable g_cls ,the MFC provides a synchronization mutex class CCriticalSection of an object ,its management thread of control access to shared resources ,MFC altogether provides four synchronization mutex class can to handle the shared variable problem ( CMutex ,CSemaphore ,CEvent ,CCriticalSection ,each having different ,specific can go online to find relevant information or watch MSDN ) .
Critical section object to and shared variables together in MFC frame code outside / / global variable CCriticalSection g_cls ;/ / critical section object int chopsticks ;g_cls.Lock ( ) and g_cls.
Unlock ( ) between the variables will be limited to the same time only to make a thread access .Two ,demo graphics rendering this part mainly is the calculation of the coordinates of a little trouble ,drawing using CDC comes with some graphics functions to draw .
Void CPhilosophy_EatingDlg: :OnPaint ( ) {if ( IsIconic ( ) ) {CPaintDC DC ( this ) ;/ / device context for paintingSendMessage ( WM_ICONERASEBKGND ( WPARAM ) ,dc.GetSafeHdc ( ) ,0) ;/ / Center icon in client rectangleint cxIcon = GetSystemMetrics ( SM_CXICON ) ;int cyIcon = GetSystemMetrics ( SM_CYICON CRect rect ) ;;GetClientRect ( & ;rect ) ;int x = ( rect.
Width ) ( cxIcon + 1 ) / 2 ;int y = ( rect.Height ) ( cyIcon + 1 ) / 2 ;/ / Draw the icondc.DrawIcon ( x ,y ,m_hIcon ) ;} else {CDialog: :OnPaint ( ) ;} / / to obtain a display box size rectCRect rect ;GetDlgItem ( IDC_STATIC_TABLE ) - > ;GetWindowRect ( & ;rect ) ;ScreenToClient ( & ;rect ) ;/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / calculate the rect coordinates of the center of the table ,the central coordinates ,Bose Headphones,as well as the external rectangular coordinate / / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / 1 center coordinate pt_table.
x calculation table = ( rect.right+rect.left ) / 2 ;pt_table.y = ( rect.bottom+rect.top ) / 2 ;/ / 2 calculation table exterior rectangle of size ( rect.right-rect.left ) / radius_table = 3 ;/ / we To display frame length of 1 / 3 as table radius rect.
left = pt_table.x-radius_table ;rect.right = pt_table.x+radius_table ;rect.top = pt_table.y-radius_table ;rect.bottom = pt_table.y+radius_table ;CDC * pDC = GetDC ( ) ;pDC-> ;Ellipse ( rect ) ;/ / calculate the coordinates of the center of each chair double pi = 3.
1415926535 ;double sin_temp ,cos_temp double x ;= pi / 3 ;for ( int i = 0 ;i< ;6 ;I + + ) {sin_temp = sin ( I * x ) ;cos_temp = cos ( I * x ) ;pt_chair . X = pt_table.
x ( 3 * radius_table ) / 2 * sin_temp ;pt_chair . Y = pt_table.y ( 3 * radius_table ) / 2 * cos_temp ;} radius_chair = radius_table / 4 ;/ / calculate each chair external rectangle, and draw the chair for ( I = 0 ;i< ;6 ;I + + ) {rect.
left = pt_chair . X-radius_chair ;rect.right = pt_chair the. X+radius_chair ;rect.top = pt_chair . Y-radius_chair ;rect.bottom = pt_chair . Y+radius_chair ;pDC-> ;Ellipse ( rect ) ;CString num ;num.
Format ( " ;%d" ;pDC-> ;DrawText ,i+1 ) ;( Num ,Cheap Sunglasses,& ;rect ,DT_CENTERDT_VCENTERDT_SINGLELINE ) ;/ / single centered } / / computational fork the starting coordinates and termination of for ( I = 0 ;i< ;6 ;I + + ) {pt_chopstick_start .
X = pt_table.x-radius_table * sin ( I * x+pi / 6 ) ;pt_chopstick_start y = pt_table.y-radius_table * cos ( I * x+pi / 6 ) ;pt_chopstick_end x = pt_table.x-25 * sin ( I * x+pi / 6 ) ;pt_chopstick_end y = pt_table.
y-25 * cos ( I * x+pi / 6 ) ;} / / painted chopsticks CPen pen (PS_SOLID, 4 ,RGB ( 0, 0, 0) ) ;pDC-> ;SelectObject ( & ;pen ) ;for ( I = 0 ;i< ;6 ;I + + ) {pDC-> ;MoveTo ( pt_chopstick_start ) ;pDC-> ;LineTo ( pt_chopstick_end ) ;} } in the code above related variables declared in CPhilosophy_EatingDlg :class CPhilosophy_EatingDlg :public CDialog { / / Constructionpublic: CPhilosophy_EatingDlg ( CWnd * pParent = NULL ) ;/ / standard constructorpublic: ImageInfo imageinfo ;CWinThread * pthread ;CPoint pt_table ;CPoint pt_chair double ;radius_table ;double radius_chair ;CPoint pt_chopstick_start ;CPoint pt_chopstick_end ;/ / CPoint pt_chopstick_start ;BOOL m_continue ;the process of run-time graphics rendering is already in the process of change in function Eat ,we can see ,is not very complicated .
Three ,two button code start and stop button code ,no content ,I put the code posted ,you can refer to the void CPhilosophy_EatingDlg: :OnButtonStart ( ) { / / TODO: Add your control notification handler code herefor ( int m = 0 ;m< ;6 ;m + + ) {chopsticks = 1 ;} for ( int i = 0 ;i< ;6 ;I + + ) {switch ( I ) {case 0: imageinfo .
Edit = GetDlgItem ( IDC_EDIT_STATE1 ) - > ;m_hWnd ;break ;case 1: imageinfo . Edit = GetDlgItem ( IDC_EDIT_STATE2 ) - > ;m_hWnd ;break ;case 2: imageinfo . Edit = GetDlgItem ( IDC_EDIT_STATE3 ) - > ;m_hWnd ;break ;case 3: imageinfo .
Edit = GetDlgItem ( IDC_EDIT_STATE4 ) - > ;m_hWnd ;break ;case 4: imageinfo . Edit = GetDlgItem ( IDC_EDIT_STATE5 ) - > ;m_hWnd ;break ;case 5: imageinfo . Edit = GetDlgItem ( IDC_EDIT_STATE6 ) - > ;m_hWnd ;break ;default: break ;} imageinfo i .
PWnd = this ;imageinfo . Index = I ;imageinfo . Pt_chair = pt_chair ;imageinfo the. Pt_chopstick_middle = pt_chopstick_start ;imageinfo pt_chopstick_middle = pt_chopstick_start ( i+5 ) ;imageinfo i .
Radius_chair = radius_chair ;imageinfo . Rad Ius_table = radius_table ;imageinfo pt_chopstick_end = pt_chopstick_end ;imageinfo pt_chopstick_end = pt_chopstick_end
( i+5 ) ;pthread = AfxBeginThread ( Eat ,& ;imageinfo ) ;} } void CPhilosophy_EatingDlg: :OnButtonStop ( ) { / / TODO: Add your control notification handler code
hereIf ( FALSE = = m_continue ) {GetDlgItem ( IDC_BUTTON_STOP ) - > ( " ;SetWindowText ;" ;for continued demonstration ;) ( int i = 0 ;i< ;6 ;I + + ) {pthread
> SuspendThread m_continue ( ) ;} ;= TRUE ;return ;} else {GetDlgItem ( IDC_BUTTON_STOP ) - > ( " ;SetWindowText ;" ;for pause presentations ;) ( int i = 0 ;i<
;6 ;I + + ) {pthread - > ;} ) ;ResumeThread ( m_continue = FALSE ;return ;} } is the main so much, we combine code have a look ,Coach Bags,there do not understand and I welcome the exchange
of ~ ~ ~ paste some pictures to download the source code program :!I put the code posted is the hope can help some people ,if there do not understand and I can discuss ,do not want the
Related articles:

没有评论:

发表评论