0

I want to implement a multithreaded queue as in this example

But my problem is to understand the complete model.

In order to let every single thread push/pop into the same queue, i have to make the SafeQueue a Singleton. Is this correct?

And than i need some thread-safe singleton initialisation?

It should look like this:

____________
|          |
| Thread 1 |  ---------
|__________|          |
                      |
____________          ----------> ____________
|          |                      |          |
| Thread 2 |  ------------------> |   Queue  |
|__________|                      |__________|
                       --------->
____________          |
|          |          |
| Thread 3 |-----------
|__________|

Or should i initiate the queue before creating the threads and pass the Queue-Object into every single Thread?

Peter Ruderman
  • 12,241
  • 1
  • 36
  • 58
user2622344
  • 956
  • 1
  • 10
  • 26
  • 7
    No need for singletons, the threads can share the same queue object anyway. The important thing is that you protect pushes and pops from simultaneous access. – Some programmer dude Oct 23 '18 at 11:54
  • the push/pop will be protected by the mutex. "...the threads can share the same queue object anyway..." -> how should i do this? – user2622344 Oct 23 '18 at 11:56
  • Have a look [here](https://stackoverflow.com/questions/15278343/c11-thread-safe-queue) – rustyx Oct 23 '18 at 11:57
  • 6
    Threads aren't like processes. All threads in a process share memory. You can pass a pointer or a reference to one object to all your threads, and as long as the object is alive all the threads can dereference that pointer or reference. – Some programmer dude Oct 23 '18 at 11:58
  • 1
    ok, so that should be the same as my last sentence (Or should i initiate the queue before creating the threads and pass the Queue-Object into every single Thread?) correct? – user2622344 Oct 23 '18 at 11:59
  • You could totally eliminate the the word "queue" from this question. All you really are asking is whether the Singleton pattern is a smart way to share an object reference between two or more threads. The fact that the object you want to share happens to be a queue doesn't change the answer. – Solomon Slow Oct 23 '18 at 14:41
  • "Singleton" is a short way of saying, "global variable that always holds a reference to the same object." As a rule of thumb: Code that uses global variables is harder to re-use/re-purpose and harder to test than code that avoids them. – Solomon Slow Oct 23 '18 at 14:43
  • One reason not to use singleton: sooner or later you will want to use two queues simultaneously. Singleton pattern would prevent you from doing so. – Jeremy Friesner Oct 23 '18 at 14:51

1 Answers1

3

As it was pointed out by @some-programmer-dude, you are not enforced to use singleton, just share the same instance of the queue across all interested threads. So, create your queue first, then create your threads that should access the queue.

Then, to concurrency. Containers thread safety is two-fold, and queue is a container for sure:

  • Container-level integrity. Any push/pop should be done with cross-thread synchronization so we do not have a situation where two threads see two versions (or one broken version) of the queue.
  • Element-level integrity. It is not a primary case for queues (unlike vectors or maps, for instance), however you should not have a scenario where two different threads are competing to modify the same queue element (if your queue supports a reference to very first and very last items).
Yury Schkatula
  • 5,291
  • 2
  • 18
  • 42