I'm looking for an object like a ConcurrentQueue which will allow me to await the Dequeue operation if the queue is empty so I can do something like the following:  public static async Task ServiceLoop() {      var awaitedQueue = new AwaitedQueue&lt;int&gt;();      while (!cancelled) {         var item = await awaitableQueue.Dequeue();         Console.WriteLine(item);     }  }   I've written the following class, but if an item is added to the queue between the time Dequeue is called and a new awaiter is Enqueued, terrible things will happen.  public class AwaitedQueue&lt;T&gt; : IDisposable {      ConcurrentQueue&lt;TaskCompletionSource&lt;T&gt;&gt; awaiters = new ConcurrentQueue&lt;TaskCompletionSource&lt;T&gt;&gt;();      ConcurrentQueue&lt;T&gt; items = new ConcurrentQueue&lt;T&gt;();      public AwaitedQueue() { }      public void Enqueue(T item) {         if (!awaiters.TryDequeue(out TaskCompletionSource&lt;T&gt; awaiter)) {             this.items.Enqueue(item);         } else {             awaiter.SetResult(item);         }     }      public async Task&lt;T&gt; Dequeue() {         if (items.TryDequeue(out T item)) {             return item;         } else {             // If an item is enqueued between this call to create a new TaskCompletionSource.             var awaiter = new TaskCompletionSource&lt;T&gt;();             // And this call to actually enqueue, I believe it will cause me problems.             awaiters.Enqueue(awaiter);             return await awaiter.Task;         }     }      public void Dispose() {         while (awaiters.TryDequeue(out TaskCompletionSource&lt;T&gt; awaiter)) {             awaiter.SetCanceled();             awaiter.Task.Wait();         }     } }   I'm sure a robust and well-tested implementation of this concept already exists, but I don't know which combination of English words I need to type into Google to find it.
I have a scenario where I have multiple threads adding to a queue and multiple threads reading from the same queue. If the queue reaches a specific size all threads that are filling the queue will be blocked on add until an item is removed from the queue.  The solution below is what I am using right now and my question is: How can this be improved?  Is there an object that already enables this behavior in the BCL that I should be using?  internal class BlockingCollection&lt;T&gt; : CollectionBase, IEnumerable {     //todo: might be worth changing this into a proper QUEUE      private AutoResetEvent _FullEvent = new AutoResetEvent(false);      internal T this[int i]     {         get { return (T) List[i]; }     }      private int _MaxSize;     internal int MaxSize     {         get { return _MaxSize; }         set         {             _MaxSize = value;             checkSize();         }     }      internal BlockingCollection(int maxSize)     {         MaxSize = maxSize;     }      internal void Add(T item)     {         Trace.WriteLine(string.Format("BlockingCollection add waiting: {0}", Thread.CurrentThread.ManagedThreadId));          _FullEvent.WaitOne();          List.Add(item);          Trace.WriteLine(string.Format("BlockingCollection item added: {0}", Thread.CurrentThread.ManagedThreadId));          checkSize();     }      internal void Remove(T item)     {         lock (List)         {             List.Remove(item);         }          Trace.WriteLine(string.Format("BlockingCollection item removed: {0}", Thread.CurrentThread.ManagedThreadId));     }      protected override void OnRemoveComplete(int index, object value)     {         checkSize();         base.OnRemoveComplete(index, value);     }      internal new IEnumerator GetEnumerator()     {         return List.GetEnumerator();     }      private void checkSize()     {         if (Count &lt; MaxSize)         {             Trace.WriteLine(string.Format("BlockingCollection FullEvent set: {0}", Thread.CurrentThread.ManagedThreadId));             _FullEvent.Set();         }         else         {             Trace.WriteLine(string.Format("BlockingCollection FullEvent reset: {0}", Thread.CurrentThread.ManagedThreadId));             _FullEvent.Reset();         }     } }
How to Achieve this Requirement ???Can i use Jave Script , CSS and Visual force page? How to use host names rather than ip addresses on home network? I've a small home network up and running and managed to set up ssh. Can anyone tell me what I need to do to refer to one computer on the network from another by host name rather than IP address (which is what I'm doing now)? What is the acceptable similarity in a mathematics PhD dissertation when checking by Turnitin? I have checked the originality of my PhD thesis in mathematics using . The similarity was 31%. Is this percentage acceptable by most committees? How do I check if an integer is even or odd? Prove $| \sum_{i \leq n} \frac{\mu(i)}{i} | \leq 1$ So I am trying to prove the above stated formula. I am completely new to number theory so I thank you in advance for baring with me. I start off by using the definition:  $\frac{\phi(n)}{n} = \sum_{d|n} \frac{\mu(d)}{d}$  Where $\phi$ is Eulers function, and $\mu$ is the Mobius mu function. From here, I use the Mobius inversion formula to give:  $\frac{\mu(n)}{n} = \sum_{d|n}d\frac{\phi(\frac{n}{d})}{n}\mu(d)$  Now summing $n$ up to $x$:  $\sum_{n \leq x}\frac{\mu(n)}{n} = \sum_{n \leq x}\sum_{d|n}d\frac{\phi(\frac{n}{d})}{n}\mu(d)$  Now I'm not fully sure on how I can rearrange things inside the two summations on the right hand side, so leaving things as they are; using that $\sum_{d|n} \mu(d) = 1$, if and only if $n =1$. Hence:  $\sum_{n \leq x}\frac{\mu(n)}{n} = \sum_{d|1}d\phi(\frac{1}{d})\mu(d)$  Thus $d=1$. Subbing this in:  $\sum_{n \leq x}\frac{\mu(n)}{n} = \phi(1) = 1$  I am not sure what I have missed (or done wrong) as to have an equality and not a triangle relation. Any suggestions or ideas would be greatly appreciated How to use the Extended Euclidean Algorithm manually? I've only found a recursive algorithm of the extended Euclidean algorithm. I'd like to know how to use it by hand. Any idea? I'm using a common charger for both of my laptops.  The charger is specified at 20 Volts and 3.25 Amperes. My other laptop has a specification of 19 V and 3.42 A.  Is it safe to charge my laptop with chargers whose specifications exceed or are not equal to the mentioned specifications?  I have read somewhere that it may cause some damage if the voltage or amperage is not matching the specs. Adjusting font size with TikZ picture Taking from the PGF manual,  \begin{tikzpicture}   \draw [help lines] (0,0) grid (3,3);   \coordinate (a) at (rnd,rnd);   \coordinate (b) at (3-rnd,3-rnd);   \draw (a) -- (b);   \node (c) at (1,2) {x};   \draw let \p1 = ($ (a)!(c)!(b) - (c) $),             \n1 = {veclen(\x1,\y1)}         in circle [at=(c), radius=\n1]; \end{tikzpicture}   I'd like to increase the size of x. The problem is that I have multiple circles in the same diagram. Some of them require a larger sized font, while others are okay at default. How do I do this? What are matter waves made of and what is their speed? In Avatar: The Last Airbender, Aang's eyes glow when he goes into the Avatar State. This seems to be the case with all Avatars, but Aang and the other airbending Avatars tattoos glow too.      Is there any reason for why airbending Avatars tattoos glow as well as their eyes? Consistency of an order statistic in exponential distribution Methods to work around the problem of missing data in machine learning How to control RAM / swap usage Is there a way (application) to control RAM / swap usage policy at application level.  I have some applications that I use all the time and I want them to stay in RAM all the time and there are also application that I use quite seldom and they might be thrown to swap at demand. I know that default configured windows should give me best performance but I want to check whether I can get more :).  I use windows 7. I have a matrix which is kind of symmetrical with the other diagonal, i.e., something like  $$A = \left[ \begin{array}{c c c c}    a &amp; b &amp; c &amp; d \\    e &amp; f &amp; g &amp; c \\    h &amp; i &amp; f &amp; b \\    j &amp; h &amp; e &amp; a  \end{array} \right]$$  Does this matrix have a special name in literature? What are it's properties?  And a matrix that is symmetrical by both diagonals   $$A = \left[ \begin{array}{c c c c}    a &amp; b &amp; c &amp; d \\    b &amp; e &amp; f &amp; c \\    c &amp; f &amp; e &amp; b \\    d &amp; c &amp; b &amp; a  \end{array} \right]$$  What's the name of it? Any interesting properties? Is the harmonic oscillator potential unique in having equally spaced discrete energy levels? I'd like to know your opinion on citations in abstracts. At my university, there is no rule on that matter so it's basically a question of preference.  Do you think it's okay to have citations in an abstract or do you  personally prefer to have the abstract be a ``stand-alone'' piece of  work? How to generate the random default "gravatars" like on Stack Overflow? How does Stack Overflow auto-generate the gravatars for those users who do not upload a picture or who have not created a Gravatar icon?    Where do these random avatars come from, and how can I generate them for my own site? Can I get US sales taxes refunded for electronics purchased while on vacation? I'm visiting the US for vacations (specifically Manhattan) and wanted to buy a digital camera.  The guy at the shop told me that I can get back what I pay in NYC taxes for that camera if I present the ticket at the airport, when I'll be returning home. And that this is not applicable to clothes but it does to electronic stuff.   Is any of this true? First, this question is motivated by the imprecise question: Is there a sensible notion of parity (evenness and oddness) for real numbers?   Here are some properties a notion of parity for $\mathbb{R}$ should have:    It should be an equivalence relation on $\mathbb{R}$ with exactly two equivalence classes.  Each equivalence class should be dense in $\mathbb{R}$. There should be some kind of symmetry between the two equivalence classes. (This is intentionally imprecise.)   If we divide $\mathbb{R}$ into rationals and irrationals, this lacks symmetry, since the rationals have measure zero and are countable, while the rationals have infinite measure and are uncountable.   So we could require that each equivalence class be uncountable and/or have positive (or infinite) measure. Then take one equivalence class to be the rationals, union with countably many fat cantor sets with positive measure, and the other class its complement. Then both classes are now dense, uncountable, and have infinite measure. But again, symmetry is lacking. One of these classes is "uncountable everywhere" and has "positive measure everywhere" while the other does not.   I propose some definitions: A subset $A \subset \mathbb{R}$ is uncountable everywhere if for any open interval $(a,b)$, the intersection $A \cap (a,b)$ is uncountable. A subset $A \subset \mathbb{R}$ has positive measure everywhere if for every open interval $(a,b)$, the intersection $A \cap (a,b)$ has positive measure. We can see immediately that having positive measure everywhere implies being uncountable everywhere, since a positive measure set must be uncountable.   Finally, my questions:    Is there a partition of $\mathbb{R}$ into two sets that are both uncountable everywhere?  Is there a partition of $\mathbb{R}$ into two sets that both have positive measure everywhere?  Is there a partition of $\mathbb{R}$ into two sets that split each interval $(a,b)$ into two parts of equal measure?   Potential generalizations for extra credit: What about partitions of $\mathbb{R}$ with $n$ equivalence classes, where $n \in \mathbb{N}$, or even with countably many, or even uncountably many equivalence classes?  Note: If you've seen these definitions of uncountable everywhere or positive measure everywhere somewhere under a different name, please let me know. I've never found anything where other people were thinking about these notions.  EDIT: By "measure," I mean Lebesgue outer measure, so we don't have to worry about anything being measurable. Problem with sudo apt get update: The repository cdrom... does not have a Release file