Re: Writing audiogames in Nim?
@9
I managed to make a shared memory example.
You need to allocate an object on the shared heap and pass ptr T to your other threads, directly or through channels.
Main sends thread1 a pointer to a square struct that has 4 ints over a channel, stored as a global variable.
Thread1 prints a message when it recieves the pointer, then waits for main to modify square.x, printing a message when it happens..
Main waits 6 seconds and sets square.x to 1000, note that both threads are using a mutex.
Code:
import strformat, os, locks, segfaults, random
type square = object # Ref object won't work.
w, x, y, z : int
mutex: Lock
var c: Channel[ptr square]
proc allocSharedObject(kind: typedesc): ptr kind= return cast[ptr kind](allocShared0(sizeof(kind)))
proc getValue() {.thread.} =
var res=c.recv()
echo fmt"Recieved square {res.w}, {res.x}, {res.y}, {res.z}"
while true:
withLock(res.mutex):
if res.x==1000:
echo "res.x just changed to 1000!"
break
sleep(1000)
proc main() =
randomize()
var sq=allocSharedObject(square)
sq.w=rand(1000)
sq.x=rand(2000)
sq.y=rand(2000)
sq.z=rand(100)
sq.mutex.initLock()
var thr: Thread[void]
c.open()
c.send(sq)
echo fmt"Sent square {sq.w}, {sq.x}, {sq.y}, {sq.z}"
thr.createThread(getValue)
sleep(6000)
withLock(sq.mutex): sq.x=1000
thr.joinThread()
deallocShared(sq)
main()
-- Audiogames-reflector mailing list Audiogames-reflector@sabahattin-gucukoglu.com https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector