5

Do I need a mutex if I have only one reader and one writer? The reader takes the next command (food.front()) from the queue and executes a task based on the command. After the command is executed, it pops off the command. The writer to the queue pushes commands onto the queue (food.push()).

Do I need a mutex? My reader (consumer) only executes if food.size() > 0. I am using a reader thread and send thread.

5 Answers5

11

A mutex is used in multi-threaded environments. I don't see mention of threads in your question, so I don't see a need for a mutex.

However, if we assume by reader and writer you mean you have two threads, you need to protect mutual data with a mutex (or other multi-threaded protection scheme.)

What happens when the queue has items, and the reader thread pops something off while the writer thread puts something on? Disaster! With a mutex, you'll be sure only one thread is operating on the queue at a time.

Another method is a lock-free thread-safe queue. It would use atomic operations to ensure the data isn't manipulated incorrectly.

GManNickG
  • 494,350
  • 52
  • 494
  • 543
  • 1
    @John: Ah, I don't usually observe tags, my bad. – GManNickG Jan 19 '10 at 20:05
  • @GManNickG: What do you mean by "lock-free thread-safe queue". One that I build according to my necessities? Or is there such thing in the STL? – steffen May 19 '12 at 20:59
  • @steffen: I wouldn't recommend building one, it can be difficult to get right. Intel's TBB has one as does MSVC's PPL library, both under the name `concurrent_queue`. However, if you don't need the lock-free property (a property that's typically only useful in high-load situation), you can implement a blocking concurrent queue quite easily: http://www.justsoftwaresolutions.co.uk/threading/implementing-a-thread-safe-queue-using-condition-variables.html – GManNickG May 19 '12 at 21:56
1

What happens if the reader sees the size is greater than zero, but the structure isn't yet completely updated?

That can be avoided by very carefully coding the updates, but the way to make the code resistant to future tampering updates is to use a mutex.

wallyk
  • 56,922
  • 16
  • 83
  • 148
  • 1
    And if we are talking about execution on a multi-processor/multi-core architecture, "very carefully coding the updates" means using the right memory barriers in the right places, which is a headache. – Pascal Cuoq Jan 19 '10 at 18:41
1

Assuming the "writer" and the "reader" are in separate threads:

Most probably yes: you could have a "metastable" state between the "writing" event and a "reading" event where the pointers to the structures are consistent.

Of course this depends on the implementation: if an atomic operation is used to update the pointers, you might be good without a mutex.

jldupont
  • 93,734
  • 56
  • 203
  • 318
1

Depends entirely on the implementation, if you have two different threads accessing the same variables you will need a mutex. Otherwise you may for example end up with an inconsistent count.

Say in write you do ++count and in read you do --count and say the current value is 2. Now note that these statements do not need to be atomic, the ++count may consist of reading variable count, incrementing it and then writing it back again. No a write and read are simultaneously executed and say the first bit of the write is executed is executed (i.e. it loads value 2. then the whole read is executed decrementing the count, but the other thread still had value 2 loaded, which it increments and subsequently writes back to the variable. Now you just lost a read action.

wich
  • 16,709
  • 6
  • 47
  • 72
0

your question depends on two conditions:

  1. there are only two threads, one is producer, the other is consumer
  2. the structure is designed for lock-free

if satisfy both, you can drop the lock, or you need to use a lock to protect the queue structure.
for dropping the lock, must remember to update the header or tailer pointer in the end of steps.

Finaldie
  • 61
  • 3