diff options
author | Adam Chlipala <adamc@hcoop.net> | 2009-04-05 10:48:11 -0400 |
---|---|---|
committer | Adam Chlipala <adamc@hcoop.net> | 2009-04-05 10:48:11 -0400 |
commit | 18b7332e738d4b97bf3bb9dfa6600277222aab33 (patch) | |
tree | 0a616d813984767dcc70ab5fe7b6409237851fc5 /demo | |
parent | c042b1bb94be89cc99fe4226edc3ce920d99f56b (diff) |
Threads demo
Diffstat (limited to 'demo')
-rw-r--r-- | demo/buffer.ur | 25 | ||||
-rw-r--r-- | demo/buffer.urs | 5 | ||||
-rw-r--r-- | demo/prose | 12 | ||||
-rw-r--r-- | demo/threads.ur | 17 | ||||
-rw-r--r-- | demo/threads.urp | 3 | ||||
-rw-r--r-- | demo/threads.urs | 1 |
6 files changed, 63 insertions, 0 deletions
diff --git a/demo/buffer.ur b/demo/buffer.ur new file mode 100644 index 00000000..27e2b805 --- /dev/null +++ b/demo/buffer.ur @@ -0,0 +1,25 @@ +datatype lines = End | Line of string * source lines + +type t = { Head : source lines, Tail : source (source lines) } + +val create = + head <- source End; + tail <- source head; + return {Head = head, Tail = tail} + +fun renderL lines = + case lines of + End => <xml/> + | Line (line, linesS) => <xml>{[line]}<br/><dyn signal={renderS linesS}/></xml> + +and renderS linesS = + lines <- signal linesS; + return (renderL lines) + +fun render t = renderS t.Head + +fun write t s = + oldTail <- get t.Tail; + newTail <- source End; + set oldTail (Line (s, newTail)); + set t.Tail newTail diff --git a/demo/buffer.urs b/demo/buffer.urs new file mode 100644 index 00000000..58312bbd --- /dev/null +++ b/demo/buffer.urs @@ -0,0 +1,5 @@ +type t + +val create : transaction t +val render : t -> signal xbody +val write : t -> string -> transaction unit @@ -234,3 +234,15 @@ batchG.urp <p><tt>BatchFun.Make</tt> handles the plumbing of allocating the local state, using it to create widgets, and reading the state values when the user clicks "Batch it."</p> <p><tt>batchG.ur</tt> contains an example instantiation, which is just as easy to write as in the <tt>Crud1</tt> example.</p> + +threads.urp + +<p>Ur/Web makes it easy to write multi-threaded client-side code. This example demonstrates two threads writing to a page at once.</p> + +<p>First, we define a useful component for sections of pages that can have lines of text added to them dynamically. This is the <tt>Buffer</tt> module. It contains an abstract type of writable regions, along with functions to create a region, retrieve a signal representing its HTML rendering, and add a new line to it.</p> + +<p>The entry point to the main module <tt>Threads</tt> begins by creating a buffer. The function <tt>loop</tt> implements writing to that buffer periodically, incrementing a counter each time. The arguments to <tt>loop</tt> specify a prefix for the messages and the number of milliseconds to wait between writes.</p> + +<p>We specify some client-side code to run on page load using the <tt>onload</tt> attribute of <tt><body></tt>. The <tt>onload</tt> code in this example spawns two separate threads running the <tt>loop</tt> code with different prefixes, update intervals, and starting counters.</p> + +<p>Old hands at concurrent programming may be worried at the lack of synchronization in this program. Ur/Web uses <i>cooperative multi-threading</i>, not the more common <i>preemptive</i> multi-threading. Only one thread runs at a time, and only particular function calls can trigger context switches. In this example, <tt>sleep</tt> is the only such function that appears.</p> diff --git a/demo/threads.ur b/demo/threads.ur new file mode 100644 index 00000000..ac6d8cee --- /dev/null +++ b/demo/threads.ur @@ -0,0 +1,17 @@ +fun main () = + buf <- Buffer.create; + let + fun loop prefix delay = + let + fun loop' n = + Buffer.write buf (prefix ^ ": Message #" ^ show n); + sleep delay; + loop' (n + 1) + in + loop' + end + in + return <xml><body onload={spawn (loop "A" 5000 0); spawn (loop "B" 3000 100)}> + <dyn signal={Buffer.render buf}/> + </body></xml> + end diff --git a/demo/threads.urp b/demo/threads.urp new file mode 100644 index 00000000..153e09a9 --- /dev/null +++ b/demo/threads.urp @@ -0,0 +1,3 @@ + +buffer +threads diff --git a/demo/threads.urs b/demo/threads.urs new file mode 100644 index 00000000..6ac44e0b --- /dev/null +++ b/demo/threads.urs @@ -0,0 +1 @@ +val main : unit -> transaction page |