c - Weird issue with returning empty struct instead of NULL -
c - Weird issue with returning empty struct instead of NULL -
context
i working on consumer & producer multi-threaded program. programme has shared variable num_elem
incremented in producer , decremented in consumer.
num elem
providing info circular buffer using struct queue
, such queue
stores arrays of words beingness read file.
if increment changed beingness within of producer beingness function called producer, communication between producer , consumer fixed. else, if code producer called function (as consumer is), then used
boolean changed true, when in fact not beingness used.
the code each below.
problemif increment called outside of producer, values used
changed true. why beingness changed when beingness called within of producer, , not when called function within of producer?
code producer
item_t *item = null; (i = 0; < queue_size; i++) { if (queue[(next_index + i) % queue_size].used == false) { item = &queue[(next_index + i) % queue_size]; item->used = true; num_elem++; next_index = (i + 1) % queue_size; } }
code consumer
item_t *consume_item() { (i = 0; < queue_size; i++) { if (queue[i].used == true) { item_t *item = &queue[i]; item->used = false; num_elem--; homecoming item; } } homecoming null;
here github repo, if see whole code
in bad version, need add together break;
after line 150.
c multithreading producer-consumer
Comments
Post a Comment