Skip to content

Instantly share code, notes, and snippets.

@guilherme
Created September 26, 2012 23:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guilherme/3791364 to your computer and use it in GitHub Desktop.
Save guilherme/3791364 to your computer and use it in GitHub Desktop.
pthread_mutex
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
void funcao1(int *id);
void funcao2(int *id);
int x=10;
int i;
pthread_mutex_t lock;
int main()
{
system("clear");
pthread_t thread1;
pthread_t thread2;
int id_thread1;
int id_thread2;
id_thread1 = 1;
id_thread2 = 2;
pthread_create(&thread1,NULL,(void* )funcao1,&id_thread1);
pthread_create(&thread2,NULL,(void* )funcao2,&id_thread2);
pthread_join(thread1,NULL);
pthread_join(thread1,NULL);
}
void funcao1(int *id)
{
printf("PAI RODANDO...\n");
}
void funcao2(int *id)
{
pthread_mutex_lock(&lock);
for(i=0;i<x;i++)
{
printf("filho processando...\n");
}
pthread_mutex_unlock(&lock);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment