Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<math.h>
- #include<pthread.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <string.h>
- #include <errno.h>
- volatile int beers = 2000000;
- pthread_mutex_t p_thread_lock = PTHREAD_MUTEX_INITIALIZER;
- struct {
- char a;
- char b;
- char c;
- char d;
- char e;
- }data;
- void* drink(void*a){
- int i;
- pthread_mutex_lock(&p_thread_lock);
- for(i=0; i<100000; i++){
- beers--;
- }
- pthread_mutex_unlock(&p_thread_lock);
- long thread_no = (long)a;
- return (void*)(thread_no +1);
- }
- int main(){
- pthread_t threads[20];
- int t = 0;
- for(t=0; t<20; t++){
- pthread_create(&threads[t],NULL,drink,(void*)t);
- }
- void* result;
- for(t=0; t<20; t++){
- pthread_join(threads[t], &result);
- printf("Thread %ld returned %ld\n",t,(long)result);
- }
- printf("There are now %i bottles of beer on the wall\n", beers);
- }
Advertisement
Add Comment
Please, Sign In to add comment