1
2
3
4
5
6 using System;
7
8 public extern cdecl nothrow int RtGetHardwareConcurrency();
9 public extern cdecl int RtStartThreadFunction(System.Threading.ThreadStartFunction function);
10 public extern cdecl int RtStartThreadFunctionWithParam(System.Threading.ParameterizedThreadStartFunction function, void* param);
11 public extern cdecl int RtStartThreadMethod(System.Threading.ThreadStartMethod method);
12 public extern cdecl int RtStartThreadMethodWithParam(System.Threading.ParameterizedThreadStartMethod method, void* param);
13 public extern cdecl bool RtJoinThread(int threadId);
14 public extern cdecl nothrow int RtThisThreadId();
15
16 namespace System.Threading
17 {
18 public delegate void ThreadStartFunction();
19 public delegate void ParameterizedThreadStartFunction(void* param);
20 public class delegate void ThreadStartMethod();
21 public class delegate void ParameterizedThreadStartMethod(void* param);
22
23 public class ThreadingException : Exception
24 {
25 public ThreadingException(const string& message_) : base(message_)
26 {
27 }
28 }
29
30 public nothrow int HardwareConcurrency()
31 {
32 return RtGetHardwareConcurrency();
33 }
34
35 public class Thread
36 {
37 public static Thread StartFunction(ThreadStartFunction function)
38 {
39 Thread thread(RtStartThreadFunction(function));
40 return thread;
41 }
42 public static Thread StartFunction(ParameterizedThreadStartFunction function, void* param)
43 {
44 Thread thread(RtStartThreadFunctionWithParam(function, param));
45 return thread;
46 }
47 public static Thread StartMethod(ThreadStartMethod method)
48 {
49 Thread thread(RtStartThreadMethod(method));
50 return thread;
51 }
52 public static Thread StartMethod(ParameterizedThreadStartMethod method, void* param)
53 {
54 Thread thread(RtStartThreadMethodWithParam(method, param));
55 return thread;
56 }
57 public nothrow Thread() : id(0)
58 {
59 }
60 private nothrow Thread(int id_) : id(id_)
61 {
62 }
63 public ~Thread()
64 {
65 try
66 {
67 if (id != 0)
68 {
69 Join();
70 }
71 }
72 catch (const Exception&)
73 {
74 }
75 }
76 suppress Thread(const Thread&);
77 suppress void operator=(const Thread&);
78 public nothrow Thread(Thread&& that) : id(that.id)
79 {
80 that.id = 0;
81 }
82 public default nothrow void operator=(Thread&&);
83 public inline nothrow int Id() const
84 {
85 return id;
86 }
87 public void Join()
88 {
89 int jid = id;
90 id = 0;
91 if (!RtJoinThread(jid))
92 {
93 throw ThreadingException("thread " + ToString(id) + " not joinable");
94 }
95 }
96 private int id;
97 }
98 }