0

In an interview, I was being asked to implement a simple Queue and protect for multi-threading. My implementation had a class variable that contained the currentSize and I only protected the push and pop functions but not the isFull and isEmpty functions.

Interviewer asked me why I didn't protect the isFull and isEmpty functions. I said that it is unnecessary for two reasons,

  • It is not changing the currentSize
  • Regardless of whether the isFull and isEmpty functions yields correct returns, you can never have any meaningful use of it as in a multithreaded environment, it will change instantaneously.

The interviewer didn't agree but I didn't push it further and cleared the interview.

Is my reasoning valid?

sgowd
  • 946
  • 3
  • 10
  • 27
  • The interview was for c++ but the question is generic and applies to most languages. Also, i do not have any info on who is using this Queue as it is an interview question. – sgowd Jun 16 '21 at 16:01
  • What where the counterarguments of the interviewer? – Theodor Zoulias Jun 16 '21 at 16:53
  • Same as what Andrew Henle commented below. – sgowd Jun 16 '21 at 18:15
  • 2
    I disagree with whoever voted to close this question and said it was "opinion based." The question of whether or not some kind of locking or synchronization is needed to ensure predictable behavior may have different answers depending on programming language, etc., but it is not a matter of opinion. Either you need the synchronization or you don't. – Solomon Slow Jun 16 '21 at 18:19
  • @SolomonSlow The question states their opinion on how they feel the structure should be designed, and asks whether people agree with it. That's opinion based. The question doesn't ask whether a given operation is atomic, it *asserts that it doesn't matter*, as an opinion. "Either you need the synchronization or you don't." The whole premise of the question is that what I quoted is a false dichotomy. The OP is asserting that even an atomic operation is of no value. Whether you agree with them or not is a matter of opinion, not fact. – Servy Jun 16 '21 at 19:01

2 Answers2

1

Yes, in general you need to protect functions such as isFull() and isEmpty() unless whatever language you are using guarantees that all accesses to the currentSize value are atomic.

Especially for a C++ implementation. Accesses to values such as int are not atomic in general. See Are Reads and Writes of an int in C++ Atomic on x86-64 multi-core machine for just one example.

Andrew Henle
  • 32,625
  • 3
  • 24
  • 56
0

The interviewer is right. A thread-safe structure should ensure that all its public members are safe for multithreaded usage. So you should either implement appropriately the isFull and isEmpty members, or remove them from the structure's API altogether.

For reference you could study the source code of the ConcurrentQueue<T>.IsEmpty property (C#). The implementers have taken great care in order to ensure that the returned boolean value is correct and up-to-date, using volatile fields and spinning if necessary.

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104