lib/threads: intro an example `concurrent_array_and_barrier`
authorAlexis Laferrière <alexis.laf@xymus.net>
Mon, 27 Oct 2014 14:40:51 +0000 (10:40 -0400)
committerAlexis Laferrière <alexis.laf@xymus.net>
Wed, 29 Oct 2014 17:17:52 +0000 (13:17 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

lib/pthreads/examples/concurrent_array_and_barrier.nit [new file with mode: 0644]
tests/sav/concurrent_array_and_barrier.res [new file with mode: 0644]

diff --git a/lib/pthreads/examples/concurrent_array_and_barrier.nit b/lib/pthreads/examples/concurrent_array_and_barrier.nit
new file mode 100644 (file)
index 0000000..750f976
--- /dev/null
@@ -0,0 +1,72 @@
+# This file is part of NIT (http://www.nitlanguage.org).
+#
+# Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# A basic usage example of the modules `pthreads` and `pthreads::cocurrent_collections`
+#
+# 20 threads share an array and a barrier. They each insert 1000 strings into
+# the array and wait at a barrier before finishing.
+module concurrent_array_and_barrier
+
+import pthreads::concurrent_collections
+
+private class MyThread
+       super Thread
+
+       # This `ConcurrentArray` has its own `Mutex`
+       var array: ConcurrentArray[String]
+
+       # Use an explicit `Barrier`
+       var barrier: Barrier
+
+       var id: Int
+
+       redef fun main
+       do
+               # Print and add to Array 1000 times
+               for i in 1000.times do
+                       var str = "thread {id}: {i}"
+                       array.add str
+               end
+
+               # Wait at the `barrier`
+               barrier.wait
+
+               return id
+       end
+end
+
+var n_threads = 20
+
+# This `ConcurrentArray` has its own `Mutex`
+var array = new ConcurrentArray[String]
+
+# Use an explicit `Barrier`
+var barrier = new Barrier(n_threads)
+
+# Create all our threads
+var threads = new Array[Thread]
+for t in n_threads.times do
+       var thread = new MyThread(array, barrier, t)
+       threads.add thread
+       thread.start
+end
+
+# Wait for the threads to complete
+for thread in threads do
+       print "Thread {thread.join or else "null"} is done"
+end
+
+print "{array.length} strings inserted"
diff --git a/tests/sav/concurrent_array_and_barrier.res b/tests/sav/concurrent_array_and_barrier.res
new file mode 100644 (file)
index 0000000..ccef6ba
--- /dev/null
@@ -0,0 +1,21 @@
+Thread 0 is done
+Thread 1 is done
+Thread 2 is done
+Thread 3 is done
+Thread 4 is done
+Thread 5 is done
+Thread 6 is done
+Thread 7 is done
+Thread 8 is done
+Thread 9 is done
+Thread 10 is done
+Thread 11 is done
+Thread 12 is done
+Thread 13 is done
+Thread 14 is done
+Thread 15 is done
+Thread 16 is done
+Thread 17 is done
+Thread 18 is done
+Thread 19 is done
+20000 strings inserted