annotate Implab/Parallels/AsyncPool.cs @ 33:b255e4aeef17

removed the reference to the parent from the promise object this allows resolved promises to release parents and results they are holding. Added complete set of operations to IPromiseBase interface Subscribing to the cancellation event of the promise should not affect it's IsExclusive property More tests.
author cin
date Thu, 10 Apr 2014 02:39:29 +0400
parents 9bf5b23650c9
children 2880242f987a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
25
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
1 using System;
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
2 using System.Threading;
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
3
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
4 namespace Implab.Parallels {
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
5 /// <summary>
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
6 /// Класс для распаралеливания задач.
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
7 /// </summary>
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
8 /// <remarks>
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
9 /// Используя данный класс и лямда выражения можно распараллелить
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
10 /// вычисления, для этого используется концепция обещаний.
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
11 /// </remarks>
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
12 public static class AsyncPool {
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
13
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
14 public static Promise<T> Invoke<T>(Func<T> func) {
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
15 var p = new Promise<T>();
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
16
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
17 ThreadPool.QueueUserWorkItem(param => {
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
18 try {
14
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
19 p.Resolve(func());
25
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
20 } catch(Exception e) {
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
21 p.Reject(e);
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
22 }
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
23 });
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
24
9bf5b23650c9 refactoring
cin
parents: 16
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;
25
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
42 }
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
43 }
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
44 }