Simple producer-consumer example C# Dictionary -
i make first steps in parallel programming. wrote simplest code, result confused. code takes top 10 recent items producer-consumer pattern. 1 consumer , 1 producer. 2 consumers in works better, incorrect too.
public static void producerconsumer(string path) { var capacity = 50000; var collection = new blockingcollection<string>(capacity); var dict = new dictionary<string, int>(); var tasks = new list<task<dictionary<string, int>>>(); var producer = task.factory.startnew(() => { parallel.foreach(file.readlines(path), (line) => { collection.add(line); }); collection.completeadding(); }); (int = 0; < 1; i++) { var consumer = task.factory.startnew<dictionary<string, int>>(() => { var localdict = new dictionary<string, int>(); while (!collection.iscompleted) { string line; if (collection.trytake(out line)) { map(line, localdict); } } return localdict; }); tasks.add(consumer); } int count = 0; while (tasks.count > 0) { var id = task.waitany(tasks.toarray()); var res = tasks[id].result; count += res.sum(k => k.value); aggregate(res, dict); tasks.removeat(id); } console.writeline($"values sum : {count}"); showresult(dict); showtotal(dict, "end"); } public static void map(string line, dictionary<string, int> dict) { var parts = line.split(new string[] { ";" }, stringsplitoptions.removeemptyentries); var streetname = parts[3]; if (dict.keys.contains(streetname)) { dict[streetname]++; } else { dict.add(streetname, 1); } } public static void showresult(dictionary<string, int> dict) { var res = dict.orderbydescending(r => r.value).take(10).tolist(); foreach (var key in res) { console.writeline($"{key.key} - {key.value}"); } } public static void aggregate(dictionary<string, int> part, dictionary<string, int> main) { foreach (var key in part.keys) { if (main.keys.contains(key)) { main[key] += part[key]; } else { main.add(key, 1); } } } public static void showtotal(dictionary<string, int> dict, string mark = null) { console.writeline($"{mark ?? ""} keys: {dict.keys.count} - values:{dict.sum(s => s.value)}"); }
while debugging shows correct steps, result shows 1 hit per item , incorrect total.
if understand algorithm, should be:
main.add(key, part[key])
Comments
Post a Comment