Contents
Is the queue in Stack Overflow thread safe?
It is not thread-safe since multiple threads may modify the pointers in the linked list at the same time, potentially corrupting it. There you can see how to make the queue thread-safe. Thanks for contributing an answer to Stack Overflow!
Is it safe to use enumeration on a thread?
The enumeration represents a moment-in-time snapshot of the contents of the queue. It does not reflect any updates to the collection after GetEnumerator was called. The enumerator is safe to use concurrently with reads from and writes to the queue.
Which is thread picks which alarm in the queue?
Using the TryQueue approach, you are guaranteed that each Alarm in the queue would only get processed once; however, which thread picks which alarm is determined non-deterministically.
Is the push function Thread safe in Java?
The push and pop functions are not thread safe. In the code, the push is only being executed by a single thread, so it doesn’t matter, but the pops are being executed by multiple threads. Imagine thread A executes up to and including line 2. Thread B then executes up to and including line 4. Thread A resumes execution.
Are there any thread safe collections in C + + 11?
BlockingCollection is a C++11 thread safe collection class that provides support for queue, stack and priority containers. It handles the “empty” queue scenario you described. As well as a “full” queue. You may like lfqueue, https://github.com/Taymindis/lfqueue .
Which is an example of a thread safe queue in GLib?
There is no real need to reinvent the wheel with such a queue though, since GAsyncQueue [7] is an implementation of a thead-safe queue already included in Glib, but this code is a good example of how to convert a standard queue into a thread-safe one.
Do you expect thread safe implementation to be fast?
I expect “thread-safe” implementation to be fast, probably using “lock-free code” for synchronization and it’s ok to have some restrictions if this is required for speed. If buffer is too small to store new (added) element it’s ok to silenty override existent element or raise exception.
Why did the concurrent queue in C not work?
The paper describes a concurrent non-blocking Queue using two CAS instructions per enqueue/dequeue operations. My implementation followed closely the design from the paper, but unfortunately it didn’t work. The main reason was an in-practical approach to memory allocation.
Is it possible to block a queue with pointers?
This is pretty much the biggest problem here: pointers don’t play nicely with CAS (unless you have a very special memory allocator and deal with memory allocation explicitly in the queue code). Fortunately the paper proposes also a blocking queue implementation with locks. Fortunately it’s quite trivial.
Can you write a lock free queue using CAS?
I decided to try to write a lock-free queue using CAS. Actually, writing lock-free algorithms is pretty hard. One has to know a hell lot about the internals of a CPU and memory ordering. Urlich Drepper’s “What every programmer should know about memory” provides some background for that.
How to multithread with pure C99 and POSIX?
I started a little weekend project to try and learn how to multithread with pure C99 and POSIX threads. The project is composed of three threads, input, processing, and output, which communicate with one another through FIFO queues. Namely we have IN->FIFO->PROC->FIFO->OUT.