Skip to content

Instantly share code, notes, and snippets.

@pasali
Created November 26, 2013 22:26
Show Gist options
  • Save pasali/7667434 to your computer and use it in GitHub Desktop.
Save pasali/7667434 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <pthread.h>
#define LIMIT 1000
#define THREAD_SAYISI 4
static pthread_mutex_t kilit;
void *dongu(void *p) {
FILE *file = fopen("sayilar.txt", "a");
int basla, bitir, i, kat_sayi;
kat_sayi = (int) p;
basla = kat_sayi * (LIMIT / THREAD_SAYISI);
bitir = basla + (LIMIT / THREAD_SAYISI);
//dosyaya yaz
pthread_mutex_lock(&kilit);
for (i = basla; i < bitir; i++) {
fprintf(file,"%d\n", i);
}
pthread_mutex_unlock(&kilit);
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
pthread_t thread_dizisi[THREAD_SAYISI];
pthread_mutex_init(&kilit, NULL);
int i;
//threadleri oluştur
for (i = 0; i < THREAD_SAYISI; i++)
pthread_create(&thread_dizisi[i], NULL, dongu, (void *) i);
//threadleri çalıştır
for (i = 0; i < THREAD_SAYISI; i++)
pthread_join(thread_dizisi[i], NULL);
pthread_mutex_destroy(&kilit);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment