Fix 2 bugs introduced by refactoring
authorLukas Jiriste <ljiriste@student.42prague.com>
Fri, 22 Mar 2024 08:24:05 +0000 (09:24 +0100)
committerLukas Jiriste <ljiriste@student.42prague.com>
Fri, 22 Mar 2024 10:03:58 +0000 (11:03 +0100)
The bugs where introduced in commit f41587f.

One bug was that not setting the optional argument
number_of_times_each_philosopher_must_eat behaved the same
as setting it to 0.

The other bug was caused by not initializing a value.
This caused the non-philosopher thread to sleep for much too long.

philo/check_death.c

index 35b92eb097eee1e035bd6e1184c06b3c997e1d11..9cca50a24211acf78bb2073ab3ff4b3e8deca122 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/03/19 15:53:52 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/03/21 10:49:08 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/03/22 09:22:19 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -45,6 +45,8 @@ static int    all_full(t_philo *philos, t_settings *set)
        size_t  i;
 
        i = 0;
+       if (set->min_eats_num == 0)
+               return (0);
        while (i < set->philo_count)
        {
                if (philos[i].times_eaten < set->min_eats_num)
@@ -59,7 +61,8 @@ static suseconds_t    time_to_starve(t_philo *philos, t_settings *set)
        suseconds_t     furthest_eaten;
        size_t          i;
 
-       i = 0;
+       furthest_eaten = philos[0].last_eaten;
+       i = 1;
        while (i < set->philo_count)
        {
                if (furthest_eaten > philos[i].last_eaten)