-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy paththreading.html
122 lines (120 loc) · 3.99 KB
/
threading.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<html dir="ltr">
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/>
<title>Arc: Threading</title>
<link rel="shortcut icon" href="/assets/favicon.png">
<link rel="stylesheet" type="text/css" href="code.css">
<link href="/assets/bootstrap.css" rel="stylesheet">
</head>
<body style='margin:12px 50px 0'>
<div class="navbar navbar-inverse">
<div class="navbar-inner">
<ul class="nav navbar-nav">
<li><a href="/ref/">Arc 3.1</a>
<li><a href="http://tryarc.org">Try it</a></li>
<li><a href="http://github.com/arclanguage/anarki">Get it</a></li>
<li><a href="http://ycombinator.com/arc/tut.txt">Tutorial</a></li>
<li><a href="http://arclanguage.org/forum">Forum</a></li>
</ul>
</div>
</div> <!-- end of navbar -->
<div class="links">Previous: <a href="filesystem.html">Filesystem operations</a>
Up: <a href="index.html">Contents</a>
Next: <a href="time.html">Time operations</a>
</div>
<h1 class="links">Threading</h1>
Arc supports multiple threads of execution. One useful application of threads is to run a long-running process in the "background", for instance Arc's web server:
<pre class="repl">
arc> (thread (asv 8000))
#<thread>
arc>
ready to serve port 8000
</pre>
See <a
href='http://download.plt-scheme.org/doc/372/html/mzscheme/mzscheme-Z-H-7.html'>MzScheme's
Threads</a> for details of the threading model.
<h2>Threading</h2>
<p><table class='arc'>
<tr>
<td class='arc'><a name='thread'></a>
<img src='macro.gif' title='Macro'/>
<span class='op'>thread</span> <span class='args'>[body ...]</span>
<div class='desc'>Creates and returns a new thread, which will last until the body terminates.</div>
</td>
<td class='arc'><pre>
>(thread (+ 1 2) (+ 3 4))
<span class="return">#<thread:...t/private/kw.rkt:592:14>
</span></pre>
<pre>
>(type (thread (+ 1 2)))
<span class="return">thread
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='new-thread'></a>
<img src='foundation.gif' title='Foundation'/>
<span class='op'>new-thread</span> <span class='args'>procedure</span>
<div class='desc'>Creates and returns a new thread, which will last until <code>procedure</code>terminates. This was called <code>thread</code> in arc0.</div>
</td>
<td class='arc'><pre>
>(new-thread (fn () (+ 1 2)))
<span class="return">#<thread:...t/private/kw.rkt:592:14>
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='break-thread'></a>
<img src='foundation.gif' title='Foundation'/>
<span class='op'>break-thread</span> <span class='args'>thread</span>
<div class='desc'>Triggers a break exception on a thread.</div>
</td>
<td class='arc'><pre>
>(let th (thread (sleep 2))
(break-thread th))
user break
</pre>
</td></tr>
<tr>
<td class='arc'><a name='kill-thread'></a>
<img src='foundation.gif' title='Foundation'/>
<span class='op'>kill-thread</span> <span class='args'>thread</span>
<div class='desc'>Terminates the specified thread immediately.</div>
</td>
<td class='arc'><pre>
>(let th (thread (sleep 100))
(kill-thread th)
(dead th))
<span class="return">t
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='sleep'></a>
<img src='foundation.gif' title='Foundation'/>
<span class='op'>sleep</span> <span class='args'>secs</span>
<div class='desc'>Causes the current thread to sleep for at least the
specified time.</div>
</td>
<td class='arc'><pre>
>(sleep 0.1)
<span class="return">nil
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='dead'></a>
<img src='foundation.gif' title='Foundation'/>
<img src='predicate.gif' title='Predicate'/>
<span class='op'>dead</span> <span class='args'>thread</span>
<div class='desc'>Predicate to test if a thread has terminated.</div>
</td>
<td class='arc'><pre>
>(let th (thread (sleep 1))
(prn (dead th)) (sleep 2) (prn (dead th)))
<span class="stdout">nil
t
</span><span class="return">t
</span></pre>
</td></tr>
</table>
<p>
Copyright 2008 Ken Shirriff.
</body>
</html>