mirror of
https://github.com/nim-lang/Nim.git
synced 2026-06-02 18:07:59 +00:00
preparations for 0.8.12
This commit is contained in:
44
tests/threads/threadex.nim
Executable file
44
tests/threads/threadex.nim
Executable file
@@ -0,0 +1,44 @@
|
||||
|
||||
type
|
||||
TMsgKind = enum
|
||||
mLine, mEof
|
||||
TMsg = object {.pure, final.}
|
||||
case k: TMsgKind
|
||||
of mEof: backTo: TThreadId[int]
|
||||
of mLine: data: string
|
||||
|
||||
var
|
||||
consumer: TThread[TMsg]
|
||||
producer: TThread[int]
|
||||
printedLines = 0
|
||||
|
||||
proc consume() {.thread.} =
|
||||
while true:
|
||||
var x = recv[TMsg]()
|
||||
if x.k == mEof:
|
||||
x.backTo.send(printedLines)
|
||||
break
|
||||
echo x.data
|
||||
discard atomicInc(printedLines)
|
||||
|
||||
proc produce() {.thread.} =
|
||||
var m: TMsg
|
||||
var input = open("readme.txt")
|
||||
while not endOfFile(input):
|
||||
if consumer.ready:
|
||||
m.data = input.readLine()
|
||||
consumer.send(m)
|
||||
close(input)
|
||||
m.k = mEof
|
||||
m.backTo = myThreadId[int]()
|
||||
consumer.send(m)
|
||||
var result = recv[int]()
|
||||
echo result
|
||||
|
||||
createThread(consumer, consume)
|
||||
createThread(producer, produce)
|
||||
joinThread(consumer)
|
||||
joinThread(producer)
|
||||
|
||||
echo printedLines
|
||||
|
||||
54
tests/threads/tthreadanalysis.nim
Executable file
54
tests/threads/tthreadanalysis.nim
Executable file
@@ -0,0 +1,54 @@
|
||||
discard """
|
||||
outputsub: "101"
|
||||
cmd: "nimrod cc --hints:on --threads:on $# $#"
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
const
|
||||
noDeadlocks = defined(system.deadlocksPrevented)
|
||||
|
||||
var
|
||||
thr: array [0..5, TThread[tuple[a, b: int]]]
|
||||
|
||||
proc doNothing() = nil
|
||||
|
||||
type
|
||||
PNode = ref TNode
|
||||
TNode = object {.pure.}
|
||||
le, ri: PNode
|
||||
data: string
|
||||
|
||||
var
|
||||
root: PNode
|
||||
|
||||
proc buildTree(depth: int): PNode =
|
||||
if depth == 3: return nil
|
||||
new(result)
|
||||
result.le = buildTree(depth-1)
|
||||
result.ri = buildTree(depth-1)
|
||||
result.data = $depth
|
||||
|
||||
proc echoLeTree(n: PNode) =
|
||||
var it: PNode
|
||||
it = nil
|
||||
it = n
|
||||
while it != nil:
|
||||
echo it.data
|
||||
it = it.le
|
||||
|
||||
proc threadFunc(interval: tuple[a, b: int]) {.thread.} =
|
||||
doNothing()
|
||||
for i in interval.a..interval.b:
|
||||
var r = buildTree(i)
|
||||
echoLeTree(r) # for local data
|
||||
echoLeTree(root) # and the same for foreign data :-)
|
||||
|
||||
proc main =
|
||||
root = buildTree(5)
|
||||
for i in 0..high(thr):
|
||||
createThread(thr[i], threadFunc, (i*3, i*3+2))
|
||||
joinThreads(thr)
|
||||
|
||||
main()
|
||||
|
||||
55
tests/threads/tthreadanalysis2.nim
Executable file
55
tests/threads/tthreadanalysis2.nim
Executable file
@@ -0,0 +1,55 @@
|
||||
discard """
|
||||
file: "tthreadanalysis2.nim"
|
||||
line: 45
|
||||
errormsg: "write to foreign heap"
|
||||
cmd: "nimrod cc --hints:on --threads:on $# $#"
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
const
|
||||
noDeadlocks = defined(system.deadlocksPrevented)
|
||||
|
||||
var
|
||||
thr: array [0..5, TThread[tuple[a, b: int]]]
|
||||
|
||||
proc doNothing() = nil
|
||||
|
||||
type
|
||||
PNode = ref TNode
|
||||
TNode = object {.pure.}
|
||||
le, ri: PNode
|
||||
data: string
|
||||
|
||||
var
|
||||
root: PNode
|
||||
|
||||
proc buildTree(depth: int): PNode =
|
||||
if depth == 3: return nil
|
||||
new(result)
|
||||
result.le = buildTree(depth-1)
|
||||
result.ri = buildTree(depth-1)
|
||||
result.data = $depth
|
||||
|
||||
proc echoLeTree(n: PNode) =
|
||||
var it = n
|
||||
while it != nil:
|
||||
echo it.data
|
||||
it = it.le
|
||||
|
||||
proc threadFunc(interval: tuple[a, b: int]) {.thread.} =
|
||||
doNothing()
|
||||
for i in interval.a..interval.b:
|
||||
var r = buildTree(i)
|
||||
echoLeTree(r) # for local data
|
||||
root = buildTree(2) # BAD!
|
||||
echoLeTree(root) # and the same for foreign data :-)
|
||||
|
||||
proc main =
|
||||
root = buildTree(5)
|
||||
for i in 0..high(thr):
|
||||
createThread(thr[i], threadFunc, (i*100, i*100+50))
|
||||
joinThreads(thr)
|
||||
|
||||
main()
|
||||
|
||||
14
tests/threads/tthreadheapviolation1.nim
Executable file
14
tests/threads/tthreadheapviolation1.nim
Executable file
@@ -0,0 +1,14 @@
|
||||
var
|
||||
global: string = "test string"
|
||||
t: TThread[string]
|
||||
|
||||
proc horrible() {.thread.} =
|
||||
global = "string in thread local heap!"
|
||||
var x = global
|
||||
var mydata = (x, "my string too")
|
||||
echo global
|
||||
|
||||
createThread(t, horrible)
|
||||
joinThread(t)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user