-
Notifications
You must be signed in to change notification settings - Fork 1
/
mutex1.c
55 lines (45 loc) · 1.17 KB
/
mutex1.c
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define THREAD_SAYISI 10
pthread_mutex_t lock;
int sayac;
void * giris_fonksiyonu(void * threadno)
{
pthread_mutex_lock(&lock);
sleep(rand()%5);
int i;
sayac += 1;
printf("%d. islem basladi...\n",sayac);
for(i=0; i<(0xFFFFF);i++);
printf("%d. islem bitti...\n",sayac);
pthread_mutex_unlock(&lock);
pthread_exit(NULL);
}
int main() {
pthread_t threadler[THREAD_SAYISI];
if(pthread_mutex_init(&lock,NULL) != 0) {
printf("mutex baslatilamadi\n");
return 1;
}
long i,donus_degeri;
srand(time(NULL));
for(i=0; i<THREAD_SAYISI; i++){
//printf("__________ %ld. thread olusturuluyor...\n", i);
donus_degeri = pthread_create(&threadler[i], NULL, giris_fonksiyonu, (void *)i);
if (donus_degeri){
printf("HATA : %ld\n", donus_degeri);
exit(-1);
}
}
// tum threadler bitene kadar bekle
for(i=0; i<THREAD_SAYISI;i++) {
donus_degeri = pthread_join(threadler[i], NULL);
if (donus_degeri){
printf("HATA : %ld. threadde bir sorun oldu\n", donus_degeri);
exit(-1);
}
}
return 0;
}