Advertisement
detective1711

bakery.cpp

Apr 6th, 2014
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.11 KB | None | 0 0
  1. /* AP   18-apr-05 bakery.c          */
  2. /*  an implementation of critical       */
  3. /*  sections based on bakery algorithm  */
  4.  
  5. # include "bakery.h"
  6. # include <stdio.h>
  7. # include <conio.h>
  8. # include <windows.h>
  9.  
  10. # define MAX_THREAD 1024
  11.  
  12. MY_LPCRITICAL_SECTION *sec;
  13. int count=0;
  14. struct _crit_sec {
  15.     volatile int choosing[MAX_THREAD];
  16.     volatile int number[MAX_THREAD];
  17. };
  18.  
  19. void My_InitializeCriticalSection( MY_LPCRITICAL_SECTION * sec ) {
  20.     int i;
  21.  
  22.     *sec = (MY_LPCRITICAL_SECTION) malloc (sizeof(struct _crit_sec));
  23.     for(i=0; i<MAX_THREAD; i++) {
  24.         (*sec)->choosing[i] = 0;
  25.         (*sec)->number[i] = 0;
  26.     }
  27. }
  28.  
  29. void My_DeleteCriticalSection( MY_LPCRITICAL_SECTION sec ) {
  30.     if (sec) free( sec );
  31. }
  32.  
  33. void My_EnterCriticalSection( int myId, MY_LPCRITICAL_SECTION sec ) {
  34.     int max, j;
  35.     sec->choosing[myId] = 1;
  36.     max = 0;
  37.     for (j=0; j<MAX_THREAD; j++) {
  38.         if(max < sec->number[j]) max = sec->number[j];
  39.     }
  40.     sec->number[myId] = max + 1;
  41.     sec->choosing[myId] = 0;
  42.     for (j = 0; j < MAX_THREAD; j++) {
  43.         while (sec->choosing[j]);
  44.         while (sec->number[j] != 0 &&
  45.             (sec->number[j] < sec->number[myId] ||
  46.             (sec->number[j] == sec->number[myId] && j < myId)));
  47.     }
  48. }
  49.  
  50. void My_LeaveCriticalSection( int myId, MY_LPCRITICAL_SECTION sec ) {
  51.     sec->number[myId] = 0;
  52. }
  53. DWORD WINAPI ThreadFunction(LPVOID n)
  54. {
  55.     do{
  56.         My_EnterCriticalSection((int)n, sec);//Xin vào vùng tranh chấp
  57.         if((int)n==0)//kiểm tra: là thread thứ nhất?
  58.         {
  59.             count++;//nếu đúng thì tăng biến count
  60.             Sleep(2000);
  61.         }
  62.         else
  63.         {
  64.             count--;//Nếu không phải thì giảm biến count
  65.             Sleep(2000);
  66.         }
  67.         printf("%d\n",count);//Hiển thị biến count
  68.         My_LeaveCriticalSection((int)n, sec);//Ra khỏi vùng tranh chấp
  69.     }while(1);
  70.     return 0;
  71. }
  72.  
  73. void main(void)
  74. {
  75.     HANDLE hThread[3];
  76.     DWORD dwThreadID;
  77.     int i;
  78.     My_InitializeCriticalSection(&sec);//Khởi tạo vùng tranh chấp
  79.     for(i=0;i<3;i++)
  80.     {
  81.         hThread[i]=CreateThread(NULL,0, ThreadFunction,(LPVOID)i,0, &dwThreadID);//Tạo Thread
  82.         printf("Successful, ID=%d, hThread=%d\n", dwThreadID, hThread[i]);//Hiển thị thông tin Thread
  83.     }
  84.     Sleep(1000000);
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement