Thursday, 15 September 2011

c# - Zipping a list in thread-safe manner -



c# - Zipping a list in thread-safe manner -

at moment, using next pseudo-code:

list<mymail> m = new list<mymail>(); foreach(var user in users) { var mails = maillibrary.getallmails(user); // synchronously; 100ms waiting time foreach(var mail service in mails) m.add(mail); } homecoming m.toarray();

now want fetch mail service of users in parallel, since maillibrary written synchronously, , multiple mail service servers involved. still, list should sorted same way before. try

list<mymail> m = new list<mymail>(); parallel.foreach(users,user => { list<mymail> innerm = new list<mymail>(); var mails = maillibrary.getallmails(user); // synchronously; 100ms waiting time foreach(var mail service in mails) innerm.add(mail); // how zip innerm list content m such content @ right position? }); homecoming m.toarray();

how can zip innerm m @ right position?

have considered using parallel linq instead?

return users.asparallel().asordered() .selectmany(user => maillibrary.getallmails(user)) .toarray();

i believe right thing, in terms of maintaining ordering, according the documentation.

c# multithreading list

No comments:

Post a Comment