Skip to content

Instantly share code, notes, and snippets.

@nuoxoxo
Last active July 17, 2022 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nuoxoxo/3e20924db778716dd40d14c020ebcbcb to your computer and use it in GitHub Desktop.
Save nuoxoxo/3e20924db778716dd40d14c020ebcbcb to your computer and use it in GitHub Desktop.
Quantum Spoons - The _NO_ Solution

Quantum Spoons - The NO Solution

malloc

pthread_mutex_t	*block_monitor;
block_monitor = (pthread_mutex_t *) malloc( sizeof(pthread_mutex_t) );

t_philo		*persons;
persons = (t_philo *) malloc( sizeof(t_philo) * 5 );

pthread_t	*threads;
threads = (pthread_t *) malloc( sizeof(pthread_t) * 5 );

int		*block_length;
block_length = (int *) malloc( sizeof(int) * 5 );

int		*block_start;
block_start = (int *) malloc( sizeof(int) * 5 );

mutex setup (STL function)

pthread_mutex_t	*block_monitor;
block_monitor = (pthread_mutex_t *) malloc( sizeof(pthread_mutex_t) );
pthread_mutex_init( block_monitor, NULL );

block setup

int		*block_length, *block_start;
block_length = (int *) malloc( sizeof(int) * 5 );
block_start = (int *) malloc( sizeof(int) * 5 );
i = -1;
while (++i < how_many_people)
{
	block_length[i] = 0;
	block_start[i] = -1;
}

table (ie. philosophers setup) . pthread_create (STL)

t0 = time(0);
i = -1;
while (++i < how_many_people)
{
	persons[i].id = i;
	persons[i].t0 = t0;
	persons[i].v = initialize_v( how_many_people );
	persons[i].ms = atoi(argv[2]);
	persons[i].block_length = block_length;
	persons[i].block_start = block_start;
	persons[i].block_monitor = block_monitor;
	persons[i].how_many_people = how_many_people;

	pthread_create( & threads[i], NULL, philosopher, (void *) (& persons[i]) );
}
int	pthread_create
(
	pthread_t	*restrict thread, ~ &threads[i]
	const pthread_attr_t	*restrict attr, ~ NULL
	void		*(*start_routine)(void*), ~ `philosopher`
	void		*restrict arg ~ (void*) (& persons[i])
);

* `philosophers` is `void *philosopher(void *v)`

/* both ways work the same */

pthread_create( & threads[i], NULL, philosopher, (void *) (& persons[i]) );
pthread_create( threads + i, NULL, philosopher, (void *) (persons + i) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment