"c pthreads - issues with thread-safe queue implementation" Code Answer
Answers related to “c pthreads - issues with thread-safe queue implementation”
- What does thread safe mean in a PHP context?
- Is the PHP Intl extension thread safe?
- Are global variables thread-safe in Flask? How do I share data between requests?
- Is local static variable initialization thread-safe in C++11?
- Is Meyers' implementation of the Singleton pattern thread safe?
- Is cout synchronized/thread-safe?
- Is C# Random Number Generator thread safe?
- Is HttpSession thread safe, are set/get Attribute thread safe operations?
- How to use non-thread-safe async/await APIs and patterns with ASP.NET Web API?
- Thread safe multitons in Java
- Why are local variables thread safe in Java
- stdout thread-safe in C on Linux?
- Is RestTemplate thread safe?
- Which concurrent Queue implementation should I use in Java?
- Where do I get a thread-safe CollectionView?
- Which Swing component methods are thread safe?
- Are urllib2 and httplib thread safe?
- I've heard i++ isn't thread safe, is ++i thread-safe?
- Must Spring MVC Classes be Thread-Safe
- Is the C# static constructor thread safe?
- What's the best way of implementing a thread-safe Dictionary?
- Does const mean thread-safe in C++11?
- Is java.sql.Connection thread safe?
- Is DbContext thread safe?
- Is malloc thread-safe?
- Is iterating ConcurrentHashMap values thread safe?
- Is a HashMap thread-safe for different keys?
- Are C# arrays thread safe?
- Thread Safe C# Singleton Pattern
- Thread-safe List<T> property
- Fast performing and thread safe observable collection
- Are function static variables thread-safe in GCC?
- Is a volatile int in Java thread-safe?
- Is the += operator thread-safe in Python?
- Are primitive datatypes thread-safe in Java
- Are IEnumerable Linq methods thread-safe?
- Are non-synchronised static methods thread safe if they don't modify static class variables?
- Check to see if a pthread mutex is locked or unlocked (After a thread has locked itself)
- Is accept() thread-safe?
- What Makes a Method Thread-safe? What are the rules?
- Thread safe lazy construction of a singleton in C++
- How to implement a unmanaged thread-safe collection when I get this error: <mutex> is not supported when compiling...
- C++ Thread-Safe Map
- Is .NET's StringBuilder thread-safe
- Is boost shared_ptr <XXX> thread safe?
- Is C++ static member variable initialization thread-safe?
- python dictionary is thread safe?
- Is stl vector concurrent read thread-safe?
- pthread synchronized blocking queue
- Is Cipher thread-safe?
- How much overhead is there when creating a thread?
- Pattern for lazy thread-safe singleton instantiation in java
- How to access c# WPF control in thread safe way?
- Not thread-safe Object publishing
- Are static variables thread-safe? C#
- Is Sqlite Database instance thread safe
- Are arrays thread-safe in Java?
- Thread-safe in delphi
- Delphi: Why VCL is not thread-safe? How can be?
- Is int? thread safe?
- Are C# structs thread safe?
- Is it thread safe to set Active Resource HTTP authentication on a per-user basis?
- Is static init thread-safe with VC2010?
- Thread safe instantiation of a singleton
- Is Objective-C's NSMutableArray thread-safe?
- C# Thread safe fast(est) counter
- Why is List<T> not thread-safe?
- Are redis operations on data structures thread safe
- How to make a class Thread Safe [closed]
- Writing to file in a thread safe manner
- Best way to start a thread as a member of a C++ class?
- Thread-safe iteration over a collection
- Is List.iterator() thread-safe?
- Is C#'s using statement abort-safe?
- Is this use of Parallel.ForEach() thread safe?
- Is Hibernate's session thread safe?
- Is using std::deque or std::priority_queue thread-safe?
- SqlConnection Thread-Safe?
- Is returning local static object thread safe for pre-c++11 compilers
- Is it always safe to convert an integer value to void* and back again in POSIX?
- Are C# auto-implemented static properties thread-safe?
- Are primitive data types in c# atomic (thread safe)?
- How to pass multiple parameters to a thread in C
- how to make an application thread safe?
- C Programming: Debugging with pthreads
- Thread-safe memoization
- Atomic properties vs thread-safe in Objective-C
- Multiple-writer thread-safe queue in C
- Why is std::queue not thread-safe?
- Is LinkedList thread-safe when I'm accessing it with offer and poll exclusively?
- Is it possible to use Go's buffered channel as a thread-safe queue?
- Is this use of a static queue thread-safe?
- Is SqlConnection / SqlCommand thread safe?
- Can I make a thread-safe std::atomic<vector<int>>?
- C#: How can I make an IEnumerable<T> thread safe?
- Which features make a class to be thread-safe?
- How to utilize a thread pool with pthreads?
- Is MemoryCache.Set() thread-safe?
- How is BackgroundWorker.CancellationPending thread-safe?
- Designing a thread-safe copyable class
- Thread Specific Data vs Thread Local Storage
- Linux synchronization with FIFO waiting queue
- Is this Singleton implementation correct and thread-safe?
- C++ pthread blocking queue deadlock (I think)
- How to implement thread safe reference counting in C++
- Is a readonly field in C# thread safe?
- Are std::signal and std::raise thread-safe?
- Is it guaranteed that pthread_cond_signal will wake up a waiting thread?
- Thread-safe lock-free array
- What is a thread-safe way to read/write a C# property in a class?
- Thread safe vector
- c# events execution are thread safe?
- Is there a queue implementation?
- Thread safe logging class implementation
- job queue implementation for python
- A very simple thread pool using pthreads in C++
- Any single-consumer single-producer lock free queue implementation in C?
- non-blocking thread-safe queue in C++?
- Are function-local static mutexes thread-safe?
- CircularBuffer highly efficient implementation (both thread-safe and not thread-safe) [closed]
- How to iterate over a container in a thread-safe way?
- Thread safe queue - Enqueue / Dequeue
- Are functions in the C standard library thread safe?
- Is using `std::get<I>` on a `std::tuple` guaranteed to be thread-safe for different values of `I`?
- Is this non-locked TryGetValue() dictionary access thread-safe?
- python threading Queue producer-consumer with thread-safe
- Is yield return in C# thread-safe?
- How do I make a function thread safe in C++? [closed]
- Does an optimistic lock-free FIFO queue implementation exist?
- undefined reference to `pthread_create' Error when making C++11 application with ASIO and std::thread
- Is glibc's implementation of fprintf() thread-safe?
- A line-based thread-safe std::cerr for C++
- Thread safe queue in Java
- Why is reading not thread-safe?
- is std::queue thread safe with producer and multiple consumers
- Is std::regex thread safe?
- istream and ostream with shared streambuf mutually thread-safe for duplex I/O?
- How to use an AppDomain to limit a static class' scope for thread-safe use?
- GCC's TSAN reports a data race with a thread safe static local
- Are Static classes thread safe
- Are .NET ref parameters thread-safe, or vulnerable to unsafe multithreaded access?
- Realm thread safe object with singleton
- Thread-safe updates of a WinForm control from other classes
- Is C read() Thread Safe?
- Thread-safe Cached Enumerator - lock with yield
- Is it safe to place definition of specialization of template member function (withOUT default body) in source file?
- Thread not printing out in correct order
- Thread-safe singleton in C++11
- Are bitset individual bits thread safe for per individual thread to write into at a single time?
- Is it thread-safe to read an instance variable while calling a setter from another thread?
- How to pass more than one value as an argument to a thread in C?
- What is the definition of a thread safe function according to the C++11 (Language/Library) Standard?
- What “thread safe” really means…In Practical terms
- Thread-safe C++ stack
- How to write a thread-safe and efficient, lock-free memory allocator in C?
- Is NSUserDefault thread safe?
- Why aren't classes like BindingList or ObservableCollection thread-safe?
- C , how to create thread using pthread_create function
- Thread-Safe collection with no order and no duplicates
- Is working with the Session thread-safe?
- is boost::property_tree::ptree thread safe?
- Fully thread-safe shared_ptr implementation
- Queue implementation in Swift language
- Is the “switch” statement evaluation thread-safe?
- Designing a Thread Safe Class
- How to make C (P/invoke) code called from C# “Thread-safe”
- Should I use Helgrind or DRD for thread error detection?
- C++ Thread Safe Integer
- Is the List<T>.AddRange() thread safe?
- Is CreateDirectory() in C# thread-safe?
- Portability of pthreads-win32 over various windows compilers
- Is it thread safe to reset and copy shared_ptr simultaneously?
- Thread safe programming
- pthreads with real time priority
- Is returning an IEnumerable<> thread-safe?
- Are constructors thread safe in C++ and/or C++11?
- Are BinaryFormatter Serialize and Deserialize thread safe?
- Thread safe singleton implementation in C++
- How to implement thread safe queues
- Is the C# '??' operator thread safe?
- Is there an open source thread safe C++ object pool implementation? [closed]
- Is this a valid, lazy, thread-safe Singleton implementation for C#?
- Heartbeat implementation with Thread.Sleep()
- Can't provoke Priority Inversion in C++
- is this GCD implemented getter setter thread safe and work better than @synchronized? objc
- pthread thread state
- Why is the standard C# event invocation pattern thread-safe without a memory barrier or cache invalidation? What...
- How to easy make this counter property thread safe?
- Is Parallel.ForEach in ConcurrentBag<T> thread safe
- Am I over-engineering per-thread signal blocking?
- Is a C# Constructor thread safe?
- Is std::call_once reentrant and thread safe?
- Is this use of the generic List thread safe
- C# Is it thread safe to subscribe Same event handler for all Objects
- C++ thread-safe uniform distribution random number generation
- Implementing a thread-safe, generic stack in C++ on linux
- Making datasets thread safe in C#
- timer in a thread with pthread in C?
- thread stack pointer
- Thread-safe way to build mutex protection into a C++ class?
- Is it thread safe to call async_send and async_receive at the same time?
- Lock-free thread-safe queue - need advice
- Singleton in Objective-C, compatible with ARC and thread safe
- Is the null coalescing operator (??) in C# thread-safe?
- Will getting the current date/time be thread-safe in C++20?
- Are Asynchronous writes to a socket thread safe?
- Does calling sleep() from pthread put thread to sleep or process?
- Thread-safe execution using System.Threading.Timer and Monitor
- Getting random numbers in a thread-safe way
- Is the ternary operator (?:) thread safe in C#?
- Naming (general purpose) thread-safe data structures?
- Priority queue implementation in C
- Making a "modify-while-enumerating" collection thread-safe
- Function local static function-object that's initialized by lambda, thread-safe or not?
- Why isn't Darwin's strtod thread safe?
- Instance constructor sets a static member, is it thread safe?
- Thread-safe get (accessor method)
- Cost of thread-safe local static variable initialization in C++11?
- Is this example thread safe?
- Pthread Queue System
- C - Problem with message passing between threads using queues
- Local Static variable initialization is thread safe
- How to make reading this instance primitive thread-safe without locking?
- Is it enough to declare a function as transaction_safe, so they can be used thread-safe?
- When creating thread safe reads in Swift, why is a variable create outside the concurrent queue?
- multithreading with mutexes in c and running one thread at a time
- can some one confirm if this is a thread safe implementation of singleton
- Attemption to create thread safe std::map
- Creating Thread Safe List using Reader Writer Lock
- Thread safe singleton in C++
- C++ Multithreading: is initialization of a local static lambda thread safe?
- thread safe Collection with upper bound
- How to increment (add value to) decimal in a thread-safe way?
- Is this a thread safe way to initialize a [ThreadStatic]?
- Is this combination of ConcurrentDictionary and ConcurrentQueue thread-safe?
- Is this html helper thread safe?
- Are static methods in ASP.NET code-behind classes non-thread-safe?
- Is closesocket thread safe?
- Which STL container has a thread-safe insertion process?
- How can I know if C++ compiler make thread-safe static object code?
- Is C#'s null-conditional delegate invocation thread safe?
- Thread-Safe C/C++ queue optimized for push
- Delayed Queue implementation in Storm – Kafka, Cassandra, Redis or Beanstalk?
- Java "Tiered Queue" implementation for fast Producers, slow Consumers
- C# thread safe static member
- Microsoft Enterprise Library Caching Application Block not thread safe?
- Is this using of dictionary thread-safe or not?
- Is implementation of double checked singleton thread-safe?
- How to thread safe call to PictureBox.Image in c# , currently gives one of 3 errors
- priority queue implementation explanation
Only authorized users can answer the Search term. Please sign in first, or register a free account.
i found a few more bugs.
i've annotated your code. i took a bit from your first posting and your second. i've fixed the code, showing before and after [please pardon the gratuitous style cleanup]:
update:
a few things to keep in mind.
htop
probably won't show much on programs that have such a short running time. even with 10,000 queue entries this program executes in 20ms.it's better to have the program itself print the information [see below]. note that
printf
does thread locking onstdin
so it may contribute to the "serial" nature of the program. it also contributes a significant amount to the execution time of the program (i.e. theprintf
is much slower than thedequeue
)also, one thread (i.e. the first one) could monopolize the queue and drain all entries before the others have a chance to run.
the os may [is at liberty to] schedule all threads on a single core. it may then "migrate" them later (e.g. within a second or so).
i've enhanced the program to include some timing information in the output print that may help show more of what you'd like to see. also, i've added command line options to control the number of threads and number of items queued. this is similar to what i do for some of my own programs. divert the program output to a log file and examine it. play around with the options on multiple runs
update #2:
a few things.
pthread_create
takes a bit of time, allowing thread 1 to go while the others are still being created. a way to ameliorate this is to create all threads, each thread sets an "i am running" flag (in its thread control block). the main thread waits for all threads to set this flag. then, the main thread sets a global volatile "you_may_now_all_run" flag that each thread spins on before entering its primary thread loop. in my experience, they all start running within microseconds of each other [or better].i didn't implement this in the updated code below, so you can experiment with it yourself [along with the
nanosleep
].mutexes are pretty fair overall [under linux, at least] because a blocked thread will get queued, waiting on the mutex. as i mentioned in the comments, a
nanosleep
can also be used, but this [somewhat] defeats the purpose as the threads will slow down.the antidote to thread starvation is "fairness". as i mentioned, there is an elaborate algorithm for fairness without waiting. it is the kogan/petrank algorithm: http://www.cs.technion.ac.il/~erez/papers/wf-methodology-ppopp12.pdf this is really a bit involved/advanced, so caveat emptor ...
however, a compromise may be a ticket lock: https://en.wikipedia.org/wiki/ticket_lock
i've reworked the program again. it has options for pooled allocation, ticket vs. mutex lock, and deferred printing of log entries. it also cross-checks the results between threads to ensure none of them got duplicate entries.
of course, the key to all this is accurate, high precision logging (i.e. if you can't measure it, you can't tune it).
for example, one would think that doing
free
insidedequeue
would be slower than simply releasing the cell to a resuable pool (similar to a slab allocator), but, the performance boost wasn't as great as expected. this could be that glibc'smalloc/free
is just blazing fast [which is what they claim].these various versions should give you some ideas of how to build up your own performance measurement suite.
anyway, here's the code: