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.
core :: union_find
union–find algorithm using an efficient disjoint-set data structurepthreads :: concurrent_collections
Introduces thread-safe concurrent collections
# 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 is example
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"
lib/pthreads/examples/concurrent_array_and_barrier.nit:17,1--72,39