All pastes #12347 Raw Edit

Something

public text v1 · immutable
#12347 ·published 2005-05-21 18:15 UTC
rendered paste body
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

int a=1000; // Kontostand auf Konto A
int b=500;  // Kontostand auf Konto B
int summe=0;
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;

void *move(void *arg)
  {
   int status;
   pthread_detach(pthread_self());
   status=pthread_mutex_lock(&mutex);
   a=a-*((int *)(arg));
   sleep(1);
   b=b+*((int *)(arg));
   status=pthread_mutex_unlock(&mutex);
   printf("Betrag erfolgreich verschoben\n");
}

void *sum(void *arg)
{
   int status;
   pthread_detach(pthread_self());
   sleep(1);

   status=pthread_mutex_lock(&mutex);
   summe = a + b;
   status=pthread_mutex_unlock(&mutex);
   printf("Summe der Konten: %d\n",summe);
}

int main()
{
   pthread_t thread1, thread2;
   int x=500;

   printf("alter Kontostand: a:%d b:%d\n",a,b);
   pthread_create(&thread1, NULL, move, &x);
   pthread_create(&thread2, NULL, sum, NULL);
   printf("neuer Kontostand: a:%d b:%d\n",a,b);
   while (!summe) {sleep (1);};
   return 0;
}