aboutsummaryrefslogtreecommitdiff
path: root/philo_two/src/main.c
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2020-09-30 08:46:00 +0200
committerCharles Cabergs <me@cacharle.xyz>2020-09-30 08:46:00 +0200
commit608ae732eccfe50f2727823f9aebe1f32681edfb (patch)
tree01d62708d1c4b0aa444e272c7f19022b5cd70821 /philo_two/src/main.c
parent42360f381cebd6376c261b16038a7f12971f925e (diff)
downloadphilosophers-608ae732eccfe50f2727823f9aebe1f32681edfb.tar.gz
philosophers-608ae732eccfe50f2727823f9aebe1f32681edfb.tar.bz2
philosophers-608ae732eccfe50f2727823f9aebe1f32681edfb.zip
Refactoring philo_two to work with new common lib (not working)
Diffstat (limited to 'philo_two/src/main.c')
-rw-r--r--philo_two/src/main.c38
1 files changed, 20 insertions, 18 deletions
diff --git a/philo_two/src/main.c b/philo_two/src/main.c
index 5442d1a..27dce34 100644
--- a/philo_two/src/main.c
+++ b/philo_two/src/main.c
@@ -6,49 +6,51 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/14 22:45:23 by cacharle #+# #+# */
-/* Updated: 2020/09/29 15:27:05 by cacharle ### ########.fr */
+/* Updated: 2020/09/30 08:44:46 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo_two.h"
+#define PHILO_SEM_NAME "philo_two"
+
int main(int argc, char **argv)
{
int i;
- t_philo_args philo_args;
+ t_philo_conf conf;
t_routine_arg *routine_args;
sem_t *forks;
pthread_t *threads;
- if (!parse_args(&philo_args, argc, argv))
+ if (!parse_args((t_philo_args*)&conf, argc, argv))
return (1);
- sem_unlink("philo_two");
- forks = sem_open("philo_two", O_CREAT, 0700, philo_args.philo_num);
+ sem_unlink(PHILO_SEM_NAME);
+ forks = sem_open(PHILO_SEM_NAME, O_CREAT, 0700, conf.philo_num);
if (forks == SEM_FAILED)
return (1);
- if ((routine_args = routine_args_create(&philo_args, forks)) == NULL)
+ if ((routine_args = routine_args_create(&conf, forks)) == NULL)
return (1);
- if ((threads = malloc(sizeof(pthread_t) * philo_args.philo_num)) == NULL)
+ if ((threads = malloc(sizeof(pthread_t) * conf.philo_num)) == NULL)
return (1);
- philo_args.all_alive = TRUE;
- pthread_mutex_init(&philo_args.mutex_all_alive, NULL);
- pthread_mutex_init(&philo_args.mutex_stdout, NULL);
+ conf.all_alive = true;
+ pthread_mutex_init(&conf.mutex_all_alive, NULL);
+ pthread_mutex_init(&conf.mutex_stdout, NULL);
i = -1;
- while (++i < philo_args.philo_num)
- if (pthread_create(threads + i, NULL, routine_philo, routine_args + i) < 0)
+ while (++i < conf.philo_num)
+ if (pthread_create(threads + i, NULL, (t_routine)routine_philo, routine_args + i) < 0)
return (1);
- while (philo_args.all_alive)
+ while (conf.all_alive)
;
i = -1;
- while (++i < philo_args.philo_num)
- pthread_join(threads[i], NULL);
+ while (++i < conf.philo_num)
+ pthread_detach(threads[i]);
sem_close(forks);
- sem_unlink("philo_two");
+ sem_unlink(PHILO_SEM_NAME);
- pthread_mutex_destroy(&philo_args.mutex_stdout);
- pthread_mutex_destroy(&philo_args.mutex_all_alive);
+ pthread_mutex_destroy(&conf.mutex_stdout);
+ pthread_mutex_destroy(&conf.mutex_all_alive);
free(threads);
free(routine_args);
return (0);