Giter Club home page Giter Club logo

42-philosophers's People

Contributors

zklim avatar

Watchers

 avatar

42-philosophers's Issues

Forks

It may be a good idea to define a fork data structure that contains just that.

pthread_mutex_t *fork_r;

Then define functions like fork_take and fork_release that deals with all these mutex. This is an abstraction that allows you to write your code in a much more readable fashion. So your eat function would call those instead.

Separate Actions

void unlock_print(t_philo *philo, int type)
{
if (type == FORK)
printf(RED "%d %d has taken a fork\n" RESET, philo->now, philo->id);
else if (type == FORK2)
printf(MAGENTA "%d %d has taken a fork\n" RESET, philo->now, philo->id);
else if (type == EAT)
{
printf(GREEN "%d %d is eating\n" RESET, philo->last_eat, philo->id);
philo->eat_count++;
}
else if (type == SLEEP)
printf(BLUE "%d %d is sleeping\n" RESET, philo->now, philo->id);
else if (type == THINK)
printf(CYAN "%d %d is thinking\n" RESET, philo->now, philo->id);
else if (type == DIED)
printf(YELLOW "%d %d died\n" RESET, philo->now, philo->id);
pthread_mutex_unlock(&philo->states->lock);
}

When I was writing this project, it makes more sense to me to have separate functions to be called when a philosopher take an action, i.e. eat, sleep, think, die. I find this approach to be quite nice, since overally, my code would be quite easily adapted to the bonus part of the project.

I suppose, the way I have written the project is more like an object oriented approach, where a philosopher is an object, and it can take actions.

Forks are also object. Whether they are implemented as mutex or semaphore is an implementation detail. The interface remains the same.

Give it some thought!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.