annotate Implab/Parallels/AsyncPool.cs @ 17:7cd4a843b4e4 promises

Improved worker pool
author cin
date Fri, 08 Nov 2013 01:25:42 +0400
parents 5a4b735ba669
children 9bf5b23650c9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11
6ec82bf68c8e refactoring
cin
parents:
diff changeset
1 using System;
6ec82bf68c8e refactoring
cin
parents:
diff changeset
2 using System.Threading;
6ec82bf68c8e refactoring
cin
parents:
diff changeset
3
6ec82bf68c8e refactoring
cin
parents:
diff changeset
4 namespace Implab.Parallels {
6ec82bf68c8e refactoring
cin
parents:
diff changeset
5 /// <summary>
6ec82bf68c8e refactoring
cin
parents:
diff changeset
6 /// Класс для распаралеливания задач.
6ec82bf68c8e refactoring
cin
parents:
diff changeset
7 /// </summary>
6ec82bf68c8e refactoring
cin
parents:
diff changeset
8 /// <remarks>
6ec82bf68c8e refactoring
cin
parents:
diff changeset
9 /// Используя данный класс и лямда выражения можно распараллелить
6ec82bf68c8e refactoring
cin
parents:
diff changeset
10 /// вычисления, для этого используется концепция обещаний.
6ec82bf68c8e refactoring
cin
parents:
diff changeset
11 /// </remarks>
6ec82bf68c8e refactoring
cin
parents:
diff changeset
12 public static class AsyncPool {
6ec82bf68c8e refactoring
cin
parents:
diff changeset
13
6ec82bf68c8e refactoring
cin
parents:
diff changeset
14 public static Promise<T> Invoke<T>(Func<T> func) {
6ec82bf68c8e refactoring
cin
parents:
diff changeset
15 var p = new Promise<T>();
6ec82bf68c8e refactoring
cin
parents:
diff changeset
16
6ec82bf68c8e refactoring
cin
parents:
diff changeset
17 ThreadPool.QueueUserWorkItem(param => {
6ec82bf68c8e refactoring
cin
parents:
diff changeset
18 try {
14
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
19 p.Resolve(func());
11
6ec82bf68c8e refactoring
cin
parents:
diff changeset
20 } catch(Exception e) {
6ec82bf68c8e refactoring
cin
parents:
diff changeset
21 p.Reject(e);
6ec82bf68c8e refactoring
cin
parents:
diff changeset
22 }
6ec82bf68c8e refactoring
cin
parents:
diff changeset
23 });
6ec82bf68c8e refactoring
cin
parents:
diff changeset
24
6ec82bf68c8e refactoring
cin
parents:
diff changeset
25 return p;
14
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
26 }
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
27
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
28 public static Promise<T> InvokeNewThread<T>(Func<T> func) {
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
29 var p = new Promise<T>();
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
30
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
31 var worker = new Thread(() => {
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
32 try {
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
33 p.Resolve(func());
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
34 } catch (Exception e) {
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
35 p.Reject(e);
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
36 }
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
37 });
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
38 worker.IsBackground = true;
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
39 worker.Start();
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
40
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
41 return p;
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
42 }
11
6ec82bf68c8e refactoring
cin
parents:
diff changeset
43 }
6ec82bf68c8e refactoring
cin
parents:
diff changeset
44 }