Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * thread2.cpp
- * Copyright (C) 2014 Crozz Cyborg <crozz@segv.sx>
- *
- * Distributed under terms of the GPLv3 license.
- */
- #include <unistd.h>
- #include <cstdio>
- #include <pthread.h>
- #include <semaphore.h>
- sem_t semaforo; // Declaramos el semaforo
- void *thr1(void *null){
- printf("[1] Terminando trabajo en\n");
- for(int i=3;i >= 1;i--){
- printf("%d...\n", i);
- sleep(2);
- }
- printf("[1] Trabajo terminado\n");
- sem_post(&semaforo); // Cambiamos el valor del semaforo
- pthread_exit(NULL);
- }
- void *thr2(void *null){
- printf("[2] Esperando que el thread1 termine su trabajo\n");
- sem_wait(&semaforo); // Hacemos el wait del semaforo
- printf("[2] thread1 ha terminado su trabajo\n");
- pthread_exit(NULL);
- }
- int main(){
- sem_init(&semaforo, 1, 0); // Iniciamos el semaforo en 0
- pthread_t thread1;
- pthread_t thread2;
- pthread_create(&thread1, NULL, thr1, NULL);
- pthread_create(&thread2, NULL, thr2, NULL);
- pthread_exit(NULL);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement