1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| #include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <windows.h>
#include "pthread.h"
static pthread_mutex_t mutex_screen = PTHREAD_MUTEX_INITIALIZER;
void *task(void *p)
{
int i = 0;
while(1)
{
pthread_mutex_lock(&mutex_screen);
printf("%s = %d\n", p, i);
pthread_mutex_unlock(&mutex_screen);
i++;
Sleep(1000);
}
return NULL;
}
int main(void)
{
pthread_t thread_1;
pthread_t thread_2;
pthread_create(&thread_1, NULL, task, (void*)"T1");
pthread_create(&thread_2, NULL, task, (void*)"T2");
pthread_join(thread_1, NULL);
pthread_join(thread_2, NULL);
return 0;
} |
Partager