public IReadOnlyDictionary <char, int> GetCharCounts()
        {
            while (textLineQueue.Count > 0 || producerTasks.Count > 0)
            {
                emptyEvent.WaitOne();
            }

            return(totaliser.GetCharCounts());
        }
 public IReadOnlyDictionary <char, int> GetCharCounts()
 {
     // This will throw if someone adds a line to the totaliser while it's executing.
     // We need to make it thread-safe and we want to allow multiple simultaneous reads.
     //
     // Try at least the following approaches: -
     // - ReaderWriterLock
     // - ReaderWriterLockSlim
     return(totaliser.GetCharCounts());
 }
        public void Add_ShouldGiveCharCountsOfCharacters()
        {
            var totaliser = new CharacterTotaliser();

            totaliser.Add("abca.");
            totaliser.Add("foo bar wibble!");
            var counts = totaliser.GetCharCounts();

            Assert.That(counts['a'], Is.EqualTo(3));
            Assert.That(counts['b'], Is.EqualTo(4));
        }
 public IReadOnlyDictionary <char, int> GetCharCounts()
 {
     readerWriterLock.EnterReadLock();
     try
     {
         return(totaliser.GetCharCounts());
     }
     finally
     {
         readerWriterLock.ExitReadLock();
     }
 }
Beispiel #5
0
        private IReadOnlyDictionary <char, int> ComputeCharCounts(IReadOnlyCollection <string> allLines)
        {
            using (SystemMode.Counter) // don't remove this - that's cheating!
            {
                var totaliser = new CharacterTotaliser();
                foreach (string line in allLines)
                {
                    totaliser.Add(line);
                }

                return(totaliser.GetCharCounts());
            }
        }
 public IReadOnlyDictionary <char, int> GetCharCounts()
 {
     // This will return the result at the time of call, but processing might be in progress.
     // How can we make it block until processing is complete?
     return(totaliser.GetCharCounts());
 }