Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <pthread.h>
- #include <stdio.h>
- pthread_mutex_t mutex[4];
- int var[4]={0,0,0,0};
- pthread_mutex_t lock=PTHREAD_MUTEX_INITIALIZER;
- void *Mult(void *arg)
- {
- int k = *((int*)arg);
- pthread_mutex_lock(&mutex[k]);
- k++;
- k*=250;
- int j = k-249;
- pthread_mutex_unlock(&lock);
- for(j;j<=k;j++)
- {
- if(j%3==0||j%5==0)var[(k/250)-1]+=j;
- }
- pthread_mutex_unlock(&mutex[(k/250)-1]);
- }
- int main()
- {
- int i;
- pthread_t threadid[4];
- for(i=0;i<4;i++)
- {
- pthread_mutex_init(&mutex[i],NULL);
- pthread_mutex_lock(&lock);
- pthread_create(&threadid[i],NULL,&Mult,&i);
- pthread_mutex_lock(&lock);
- pthread_mutex_unlock(&lock);
- }
- for(i=0;i<4;i++)pthread_mutex_lock(&mutex[i]);
- printf("resultado: %i\n",var[0]+var[1]+var[2]+var[3]);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement