Skip to content

Instantly share code, notes, and snippets.

@enthus1ast
Last active November 5, 2018 22:03
Show Gist options
  • Save enthus1ast/c3b3b0af48144d0a198d6bd0907c9640 to your computer and use it in GitHub Desktop.
Save enthus1ast/c3b3b0af48144d0a198d6bd0907c9640 to your computer and use it in GitHub Desktop.
# test with: `while true; do curl 127.0.0.1:8080; done`
# import threadpool
import locks
import os
import asyncdispatch
import asynchttpserver, asyncdispatch
var L: Lock
type Context = ref object {.pure.}
num: int
str: string
proc th(ctxP: pointer) {.thread.} =
# initLock(L)
# GC_ref ctxP
var server = newAsyncHttpServer()
proc cb(req: Request) {.async.} =
var idx: int = 0
if not tryAcquire(L): return
var ctx = cast[ref Context](ctxP)
GC_ref ctx # ????
ctx[].num.inc()
idx = ctx[].num
GC_unref ctx
release(L)
await req.respond(Http200, "Hello World " & $idx)
proc aprint(): Future[void] {.async.} =
while true:
echo "hello"
await sleepAsync 5000
asyncCheck aprint()
asyncCheck server.serve(Port(8080), cb)
runForever()
# return 1
initLock(L)
var ctx = Context(num: 0, str: "")
var ctxP = ctx.addr
GC_ref ctx
var networkThread: Thread[pointer]
networkThread.createThread(th, ctxP)
while true:
if not tryAcquire(L):
echo "could not aquire lock"
sleep(50)
continue
ctx.num.inc
echo(ctx.num)
release(L)
sleep(2500)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment