Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4. #include <unistd.h>
  5. #include <fcntl.h>
  6. #define NMax 40
  7.  
  8. pthread_mutex_t Lee_mutex = PTHREAD_MUTEX_INITIALIZER;
  9. pthread_mutex_t AStar_mutex = PTHREAD_MUTEX_INITIALIZER;
  10.  
  11.  
  12. int LeeMatrix[NMax][NMax], AStarMatrix[NMax][NMax];
  13.  
  14. void *Lee(void *arg)
  15. {
  16. int count = 0;
  17. for (int i = 0 ; i < NMax ; ++i)
  18. for (int j = 0 ; j < NMax ; ++j)
  19. {
  20. pthread_mutex_lock(&Lee_mutex);
  21. LeeMatrix[i][j] = ++count;
  22. pthread_mutex_unlock(&Lee_mutex);
  23. }
  24. return NULL;
  25. }
  26.  
  27. void *AStar(void *arg)
  28. {
  29. int count = 0;
  30. for (int i = 0 ; i < NMax ; ++i)
  31. for (int j = 0 ; j < NMax ; ++j)
  32. {
  33. pthread_mutex_lock(&AStar_mutex);
  34. AStarMatrix[i][j] = ++count;
  35. pthread_mutex_unlock(&AStar_mutex);
  36. }
  37. return NULL;
  38. }
  39.  
  40. void Draw()
  41. {
  42. system("cls");
  43. for (int i = 0 ; i < NMax ; ++i)
  44. {
  45. for (int j = 0 ; j < NMax ; ++j)
  46. printf("%d ", LeeMatrix[i][j]);
  47. printf("\n");
  48. }
  49.  
  50. printf("\n");
  51.  
  52. for (int i = 0 ; i < NMax ; ++i)
  53. {
  54. for (int j = 0 ; j < NMax ; ++j)
  55. printf("%d ", AStarMatrix[i][j]);
  56. printf("\n");
  57. }
  58. getch();
  59. }
  60.  
  61. int main()
  62. {
  63. pthread_t lee_th, A_th;
  64.  
  65. pthread_create(&lee_th, NULL, &Lee, NULL);
  66. pthread_create(&A_th, NULL, &A_th, NULL);
  67. while (1)
  68. {
  69. pthread_mutex_lock(&Lee_mutex);
  70. pthread_mutex_lock(&AStar_mutex);
  71.  
  72. Draw();
  73.  
  74. pthread_mutex_unlock(&Lee_mutex);
  75. pthread_mutex_unlock(&AStar_mutex);
  76. }
  77.  
  78. pthread_join(lee_th, NULL);
  79. pthread_join(A_th, NULL);
  80.  
  81. return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement