-
Notifications
You must be signed in to change notification settings - Fork 205
/
Copy pathpthreads.t
47 lines (37 loc) · 952 Bytes
/
pthreads.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
local ffi = require("ffi")
if ffi.os == "Windows" then
return
end
C = terralib.includecstring [[
#include <pthread.h>
#include <stdio.h>
]]
acc = global(int[4])
terra forkedFn(args : &opaque) : &opaque
var threadid = @[&int](args)
C.printf("threadid %d\n",threadid)
acc[threadid] = threadid
return nil
end
terra foo()
var thread0 : C.pthread_t
var thread1 : C.pthread_t
var thread2 : C.pthread_t
var thread3 : C.pthread_t
acc[0]=-42
acc[1]=-42
acc[2]=-42
acc[3]=-42
var args = arrayof(int,0,1,2,3)
C.pthread_create(&thread0,nil,forkedFn,&args[0])
C.pthread_create(&thread1,nil,forkedFn,&args[1])
C.pthread_create(&thread2,nil,forkedFn,&args[2])
C.pthread_create(&thread3,nil,forkedFn,&args[3])
C.pthread_join(thread0,nil)
C.pthread_join(thread1,nil)
C.pthread_join(thread2,nil)
C.pthread_join(thread3,nil)
return acc[0]+acc[1]+acc[2]+acc[3]
end
local test = require("test")
test.eq(foo(),0+1+2+3)