Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <unistd.h>
- #include <fcntl.h>
- #define NMax 40
- pthread_mutex_t Lee_mutex = PTHREAD_MUTEX_INITIALIZER;
- pthread_mutex_t AStar_mutex = PTHREAD_MUTEX_INITIALIZER;
- int LeeMatrix[NMax][NMax], AStarMatrix[NMax][NMax];
- void *Lee(void *arg)
- {
- int count = 0;
- for (int i = 0 ; i < NMax ; ++i)
- for (int j = 0 ; j < NMax ; ++j)
- {
- pthread_mutex_lock(&Lee_mutex);
- LeeMatrix[i][j] = ++count;
- pthread_mutex_unlock(&Lee_mutex);
- }
- return NULL;
- }
- void *AStar(void *arg)
- {
- int count = 0;
- for (int i = 0 ; i < NMax ; ++i)
- for (int j = 0 ; j < NMax ; ++j)
- {
- pthread_mutex_lock(&AStar_mutex);
- AStarMatrix[i][j] = ++count;
- pthread_mutex_unlock(&AStar_mutex);
- }
- return NULL;
- }
- void Draw()
- {
- system("cls");
- for (int i = 0 ; i < NMax ; ++i)
- {
- for (int j = 0 ; j < NMax ; ++j)
- printf("%d ", LeeMatrix[i][j]);
- printf("\n");
- }
- printf("\n");
- for (int i = 0 ; i < NMax ; ++i)
- {
- for (int j = 0 ; j < NMax ; ++j)
- printf("%d ", AStarMatrix[i][j]);
- printf("\n");
- }
- getch();
- }
- int main()
- {
- pthread_t lee_th, A_th;
- pthread_create(&lee_th, NULL, &Lee, NULL);
- pthread_create(&A_th, NULL, &A_th, NULL);
- while (1)
- {
- pthread_mutex_lock(&Lee_mutex);
- pthread_mutex_lock(&AStar_mutex);
- Draw();
- pthread_mutex_unlock(&Lee_mutex);
- pthread_mutex_unlock(&AStar_mutex);
- }
- pthread_join(lee_th, NULL);
- pthread_join(A_th, NULL);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement