Untitled
unknown
plain_text
5 months ago
934 B
15
Indexable
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <pthread.h> pthread_mutex_t bilangan_lock = PTHREAD_MUTEX_INITIALIZER; int bilangan = 0; pthread_t T0; void *tambah(void *a) { int i, j; pthread_mutex_lock(&bilangan_lock); for (i = 0; i < 20; i++) { j = bilangan; j++; sleep(1); bilangan = j; } pthread_mutex_unlock(&bilangan_lock); return NULL; } int main() { int i, j; printf("nilai bilangan Awal = %i\n", bilangan); if (pthread_create(&T0, NULL, tambah, NULL) == -1) perror("thread tidak bisa dibuat"); pthread_mutex_lock(&bilangan_lock); for (i = 0; i < 20; i++) { j = bilangan; j++; sleep(1); bilangan = j; } pthread_mutex_unlock(&bilangan_lock); void* result; pthread_join(T0, &result); printf("nilai bilangan Akhir = %i\n", bilangan); return 0; }
Editor is loading...
Leave a Comment