public Writer(int id, int numberOfMessages, object padlock, SharedDataContainer container) { _id = id; _numberOfMessages = numberOfMessages; _padlock = padlock; _container = container; }
public ConcurrentBag <string> Process() { var container = new SharedDataContainer(); var padlock = new object(); var receivedMessages = new ConcurrentBag <string>(); for (int i = 0; i < NumberOfWriters; i++) { int id = i; var writer = new Writer(id, NumberOfMessages, padlock, container); var thread = new Thread(() => writer.Write()) { Name = "Writer" + id, Priority = Priority }; thread.Start(); } for (int i = 0; i < NumberOfReaders; i++) { var reader = new Reader(receivedMessages, padlock, container); var thread = new Thread(() => reader.Read()) { IsBackground = true, Name = "Reader" + i, Priority = Priority }; thread.Start(); } var totalMessages = NumberOfMessages * NumberOfWriters; while (container.WritersFinished < NumberOfWriters || receivedMessages.Count < totalMessages) { Thread.Sleep(10); } container.IsCancelled = true; Thread.Sleep(1); return(receivedMessages); }
public Reader(ConcurrentBag <string> receivedMessages, object padlock, SharedDataContainer container) { _receivedMessages = receivedMessages; _padlock = padlock; _container = container; }