123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- /*
- * File: delay.c
- * Author: Arthur Brandao
- *
- * Created on 4 décembre 2018
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <pthread.h>
- #include "error.h"
- #include "delay.h"
- /* --- Fonctions privées --- */
- /**
- * Thread d'attente de la fonction timer
- * @param void* La structure timer_data cast en void*
- * @return NULL
- */
- void* delay_thread(void* data){
- delay_t* d;
- //Recup données
- d = (delay_t*) data;
- //Detache le thread
- if (pthread_detach(pthread_self()) != 0) {
- adderror("Impossible de détacher le thread Delay");
- return NULL;
- }
- //Attend le temps indiqué
- sleep(d->second);
- //Appel callback
- if(d->callback(&game[d->game], d->player) != SUCCESS){
- adderror("Erreur callback Delay");
- }
- return NULL;
- }
- /* --- Fonctions publiques --- */
- void delay(int second, int game, int player, int(*callback)(Game*, int)){
- pthread_t thread;
- delay_t* d = malloc(sizeof(timer_t));
- d->second = second;
- d->game = game;
- d->player = player;
- d->callback = callback;
- //Creation thread
- if (pthread_create(&thread, NULL, delay_thread, d) != 0) {
- adderror("Impossible de créer le thread Delay");
- }
- }
|