zklim / 42-Philosophers

0 stars 0 forks source link

Separate Actions #3

Open nwhean opened 1 year ago

nwhean commented 1 year ago

https://github.com/zklim/42-Philosophers/blob/a9987e94f711269cbdda5af41e2cb518326db07b/philo/utils.c#L69-L87

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!

zklim commented 1 year ago

Actually did it this way so I can reduced mutex_lock(print) to only one place, which is here in the print function.

zklim commented 1 year ago

It may be working similar to oop too? Where actions object inherit parent object with print method.

nwhean commented 1 year ago

https://github.com/zklim/42-Philosophers/issues/3#issuecomment-1714231030 Actually, multiple printf calls may not be thread safe, but if you look at your code, there's essentially one printf call for any one condition. There's no reason to use a lock in this implementation.

zklim commented 1 year ago

#3 (comment) Actually, multiple printf calls may not be thread safe, but if you look at your code, there's essentially one printf call for any one condition. There's no reason to use a lock in this implementation.

I'm accessing philo->now in parent for checking death where this is also reading by print, also adding extra lock for print is because I have race when only using one lock for all place.

nwhean commented 1 year ago

It may be working similar to oop too? Where actions object inherit parent object with print method.

No, it's not easily done, if you are thinking about having an object that brings along all the its methods, i.e. object.method(). In C, you'll have to define some object_method function, and call it like this: object_method(object).

You may be able to find some references online in which people use macros to get around the problem, but I think if you are using C, it's best not to do that. If you want a different syntax, best to move to other language.

zklim commented 1 year ago

It may be working similar to oop too? Where actions object inherit parent object with print method.

No, it's not easily done, if you are thinking about having an object that brings along all the its methods, i.e. object.method(). In C, you'll have to define some object_method function, and call it like this: object_method(object).

You may be able to find some references online in which people use macros to get around the problem, but I think if you are using C, it's best not to do that. If you want a different syntax, best to move to other language.

True, C is not designed with this in mind.

archernero commented 6 months ago

我已收到您的邮件,谢谢!