Thanks to Jeffrey for the fantastic one,
http://msdn.microsoft.com/en-us/magazine/bb985010.aspx
"The garbage collector scans the finalization queue looking for pointers to these objects. When a pointer is found, the pointer is removed from the finalization queue and appended to the freachable queue (pronounced "F-reachable")."
From the above the objects J,I,E are moved from Finalization Queue to fReachable Queue.
Im not clear about the transition of Finalization Queue to fReachable Queue.
What about the FIFO logic of Queue. How dequeue of object E done without dequeuing F?
Whether it is a Finalization Queue or Finalization list?
Kindly shed some light.
Despite the word "queue" in the phrases "finalization queue" and "freachable queue", one should not make any assumptions about the sequence in which objects in those collections should be processed. I would think the term "finalizable list" would be more descriptive; the only reason I can think of for calling it a "queue" would be if the garbage collector, after identifying large objects, starts a new finalization queue and then visits each entry in the one one; each entry then gets discarded (if the finalizer has been suppressed), added to the new finalization queue (if the object had been tagged for retention), or added to the freachable queue (if the object had not yet been tagged for retention).
Related
I am dealing with a standard producer and consumer problem with finite array (or finitely many buffers ). I tried implementing it using semaphores and I have run into a problem. I want the producer to 'produce' only say 50 times. After that I want the producer thread to join the main thread. This part is easy, but what I am unable to do is to join the consumer threads. They are stuck on the semaphore signaling that there is no data. How do I solve this problem?
One possible option is to have a flag variable which becomes True when producer joins main and after that, the main thread would do post(semaphore) as many times as the number of worker threads. The worker threads would check the flag variable every time after waking up and if True, it would exit the function.
I think my method is pretty inefficient because of the many post semaphore calls. It would be great if I can unblock all threads at once!
Edit: I tried implementing whatever I said and it doesn't work due to deadlock
One option is the "poison pill" method. It assumes that you know how many consumer threads exist. Assuming there are N consumers, then after the producer has done it's thing, it puts N "poison pills" into the queue. A "poison pill" simply is an object/value that is type-compatible with whatever the producer normally produces, but which is distinguishable from a normal object/value.
When a consumer recognizes that it has eaten a poison pill, it dies. Problem solved.
I've done producer consumer structures in C++ in FreeRTOS operating system only, so keep that in mind. That has been my only experience so far with multitasking. I would say that I only used one producer in that program and one consumer. And I've done multitasking in LabView, but this is little bit different from what you might have, I think.
I think that one option could be to have a queue structure, so that the producer enqueues elements into the queue but if it's full of data, then you can hopefully implement it so that you can make some kind of queue policy as follows.
producer can either
block itself until space is available in the queue to enqueue,
block itself for certain time period, and continue elsewhere if time spent and didnt succeed in enqueuing data
immediately go elsewhere
So it looks like you have your enqueuing policy in order...
The queue readers are able to have similar three type of policies at least in FreeRTOS.
In general if you have a binary semaphore, then you have it so that the sender is sending it, and the receiver is waiting on it. It is used for synchronization or signalling.
In my opinion you have chosen the wrong approach with the "many semaphores" (???)
What you need to have is a queue structure where the producer inputs stuff...
Then, the consumers read from the queue whatever they must do...
If the queue is empty then you need a policy on what the queue reader threads should do.
Policy choice is needed also for those queue readers and semaphore readers on what they should do, when the queue is empty, or if they havent gotten the semaphore received. I would not use semaphores for this kind of problem...
I think the boolean variable idea could work, because you are only writing into that variable in the producer thread. Then the other threads should be able to read and poll that boolean variable if the producer is active...
But I think that you should provide more details what you are trying to do, especially with the consumer threads, how many threads of what kind you have, and what language you are programming in etc...
Solution traditional to producer-consumer
In Operating-Systems, as you see in the link above for producer consumer, two semaphores full and empty are used, why is it not possible to do this using only one quantity semaphore fullEmpty.
What I mean is, we have a binary semaphore mutex and another semaphore fullEmpty, which is initially 0 because there are no items in the buffer, so why do we need 2 semaphores (full, empty)?
The only thing is the order of wait and signal need to be changed so that the updating of fullEmpty is within the critical section.
Any thoughts or reasons?
The key statement in the description that relates to your answer is "We have a buffer of fixed size."
For the sake of answering your question, let's first assume that the buffer can expand to fit as many items as needed, or in other words the buffer can grow to an unlimited size. In this case, the only synchronization that would need to occur between producers and consumers (apart from locking the mutex to ensure that you don't corrupt items in the critical section) would be ensuring that consumers only consume items after they have been produced by a producer. You could solve this problem with just a mutex and one semaphore. Here is some code, which I borrowed and changed from the link you shared:
Producer
do {
//produce an item
wait(mutex);
//place in buffer
signal(mutex);
signal(full);
} while (true);
Consumer
do {
wait(full);
wait(mutex);
//remove item from buffer
signal(mutex);
//consume item
} while (true);
As you can see above, the producer is always able to add things to the queue (apart from when the mutex is being held) and doesn't need to wait for consumers to consume anything because the buffer will never fill, even if the consumers don't consume items. On the other hand, consumers can't consume anything until producers have produced items.
To answer your question, you need to focus on the statement, "We have a buffer of fixed size." This changes the problem. Since the buffer is no longer able to grow to an unlimited size, you need to get producers to wait when the buffer is full before they can add more things to the buffer. This is why you need a second semaphore. Not only do consumers need to wait for producers, but now producers need to wait for consumers. You get producers to wait for consumers by getting them to call wait on a semaphore that only consumers call signal on.
You can't do this with only one semaphore because the conditions when a producer has to wait are different from the conditions when a consumer has to wait. Since they should be able to decrement and advance past the semaphore in different conditions, you can't use the same semaphore for both.
This is because there 2 conditions you have to wait for: the queue is empty and the queue is full. But classic semaphore allows you to wait only for one condition - wait until semaphore is not 0.
You can solve this problem using a single synchronization object, but such object needs to be more feature-full than a semaphore. "Bounded semaphore" - semaphore that has a maximum value should be enough as it allows you to block waiting for both conditions.
How to get one is another question:
You can build one using mutex and condition variable.
Window's semaphore already has this functionality.
You can use futex on Linux (see FUTEX_WAIT, FUTEX_WAKE) or equivalents on other OSes: on FreeBSD use _umtx_op (see UMTX_OP_WAIT, UMTX_OP_WAKE), on Windows 8 and newer use WaitOnAddress, WakeByAddressSingle/WakeByAddressAll.
I suggest you to get familiar with futex interface - with it you can build more powerful and more efficient synchronization objects than regulars ones. Today most OSes provide an equivalent interface and even C++ might introduce something similar in the future (see std::synchronic<T>).
Few notes:
Linux has eventfd which can act as a semaphore when created with EFD_SEMAPHORE flag, but it has maximum value of 0xfffffffffffffffe and it cannot be changed. Maybe some day this syscall will be extended to support maximum value too.
Can someone help me to understand thread enqueuing while using GCD.
I want to understand thread enqueuing which we see while putting breakpoints.
How does it work?
Do every thread executes on either main or global queue? Is it the reason of enqueuing?
Thanks,
Can someone help me to understand thread enqueuing while using GCD. I want to understand thread enqueuing which we see while putting breakpoints.
I’d suggest you think of it the other way around. You don’t “enqueue” threads. You dispatch blocks of code to a queue (i.e. “enqueue”), and the dispatch queue will select the appropriate thread on which that code shall run.
For example, above, I create a queue, dispatched a block of code to that queue, and added a breakpoint. I can see that my queue spun up a thread (it’s “Thread 3” in this case) and I can see that this was “enqueued” from the the viewDidLoad method running on the “main thread”.
Do every thread executes on either main or global queue?
Again, it’s the other way around. Code that is dispatched to a particular queue will trigger that queue to run that block of code on a particular thread.
But there are three types of queues:
the “main” queue (which runs its code on a single, special, dedicated “main” thread);
one of the various, shared “global” queues (which will select a background thread from a pool of worker threads and run the code on that thread); or
a “custom” queue that you create to a custom queue, like above.
Is it the reason of enqueuing?
This “enqueuing” is merely the process of adding a block of code to a queue. Xcode will try to show you where the code was enqueued, to help you diagnose from where the code was dispatched.
Assume a producer-consumer scenario. I have used a lock-free implementation of a fixed-capacity queue, so that:
The number of items that can be held in the queue is limited.
Producers/consumers can push/pop items to/from the queue directly without holding any lock.
Now, I'm considering how to synchronize the producers and consumers, so that producers are not busy try-pushing to a full queue, and consumers are not busy try-popping from an empty queue.
I thought about condition variable at first. But it seems to destroy the whole purpose of using a lock-free queue implementation.
Is polling (with interval) the only sensible way to go with a lock-free queue?
Message queues might also be used internally in a program, in which case it's often just a facility to exchange/queue data from a producer thread to a consumer thread to do async processing.
now for async processing I can simply create threads ... if I am using queue internally isn't this a overhead for doing async processing ???
Please answer in yes or no with valid explaination.
Creating a thread takes much longer than a producer thread pushing a buffer, task or object pointer onto a queue and subsequent dequeueing by a consumer thread, so the inevitable overhead is much less than continually creating threads.
If you continually create threads, you have to continually terminate and destroy them. This is more overhead and, anyway, often goes wrong, resulting in thread runaway, shutdown failures and obscure memory leaks.
Thread pools, and app-lifetime threads, are safer, cleaner, easier to debug and less prone to disastrous failures/errors and, worse, intermittent 'deviations from specification':)
what are advantages of queue over multiple threads
I think the advantages are related to non-functional requirements such as Reliability:
if an error happens when a message is processed, it can remain in the queue until the process executes ok.
one step further is to persist the message in a database, which allows you to process the message later if the system suddenly fails.
if I am using queue internally isn't this a overhead for doing async processing
it depends whether the non-functional requirements listed above are important for your application, otherwise create a new thread could be enough.