September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -0,0 +1,12 @@
fun main():
let words = ['Enjoy', 'Rosetta', 'Code']
random.seed(time.now())
let q = Chan[Str]()
for w in words:
run (w:) ->
time.sleep(random())
q << w
for i in [:words.size]:
print q

View file

@ -0,0 +1,18 @@
' Concurrent computing using the OpenMP extension in GCC. Requires BaCon 3.6 or higher.
' Specify compiler flag
PRAGMA OPTIONS -fopenmp
' Sepcify linker flag
PRAGMA LDFLAGS -lgomp
' Declare array with text
DECLARE str$[] = { "Enjoy", "Rosetta", "Code" }
' Indicate MP optimization for FOR loop
PRAGMA omp parallel for num_threads(3)
' The actual FOR loop
FOR i = 0 TO 2
PRINT str$[i]
NEXT

View file

@ -0,0 +1,14 @@
fun parprint -> text, return
(
fork() -> return, throw
println(text, return)
| x
return()
)
| parprint
parprint("Enjoy") ->
parprint("Rosetta") ->
parprint("Code") ->
exit()

View file

@ -1,9 +1,13 @@
defmodule ConcurrentComputing do
def print(xs) do
Enum.map(xs, fn x ->
spawn(fn -> IO.puts x end)
defmodule Concurrent do
def computing(xs) do
Enum.each(xs, fn x ->
spawn(fn ->
Process.sleep(:rand.uniform(1000))
IO.puts x
end)
end)
Process.sleep(1000)
end
end
ConcurrentComputing.print ["Enjoy", "Rosetta", "Code"]
Concurrent.computing ["Enjoy", "Rosetta", "Code"]

View file

@ -0,0 +1,16 @@
// version 1.1.2
import java.util.concurrent.CyclicBarrier
class DelayedMessagePrinter(val barrier: CyclicBarrier, val msg: String) : Runnable {
override fun run() {
barrier.await()
println(msg)
}
}
fun main(args: Array<String>) {
val msgs = listOf("Enjoy", "Rosetta", "Code")
val barrier = CyclicBarrier(msgs.size)
for (msg in msgs) Thread(DelayedMessagePrinter(barrier, msg)).start()
}

View file

@ -1,8 +1,8 @@
const str = ["Enjoy", "Rosetta", "Code"]
var thr: array[3, TThread[int32]]
var thr: array[3, Thread[int32]]
proc f(i) {.thread.} =
proc f(i:int32) {.thread.} =
echo str[i]
for i in 0..thr.high:

View file

@ -0,0 +1,35 @@
-- this will launch 3 threads, with each thread given a message to print out.
-- I've added a stoplight to make each thread wait until given a go signal,
-- plus some sleeps to give the threads a chance to randomize the execution
-- order a little.
launcher = .launcher~new
launcher~launch
::class launcher
-- the launcher method. Guarded is the default, but let's make this
-- explicit here
::method launch guarded
runner1 = .runner~new(self, "Enjoy")
runner2 = .runner~new(self, "Rosetta")
runner3 = .runner~new(self, "Code")
-- let's give the threads a chance to settle in to the
-- starting line
call syssleep 1
guard off -- release the launcher lock. This is the starter's gun
-- this is a guarded method that the runners will call. They
-- will block until the launch method releases the object guard
::method block guarded
::class runner
::method init
use arg launcher, text
reply -- this creates the new thread
call syssleep .5 -- try to mix things up by sleeping
launcher~block -- wait for the go signal
call syssleep .5 -- add another sleep here
say text

View file

@ -0,0 +1,3 @@
fcn{println("Enjoy")}.launch(); // thread
fcn{println("Rosetta")}.strand(); // co-op thread
fcn{println("Code")}.future(); // another thread type