How to write a function that supports asynchronous? For example, the following function, how not exclusive CPU.
proc Longtime() =
var x=0
for i in 1..500000000:
inc (x)
Take a look at the parallel and spawn section of the manual, as well as the threads and threadpool modules. Those should give you a good idea on how to implement parallel code in Nim.
For your example, the following works:
import threadpool
{.experimental.}
proc Longtime() =
var x=0
parallel:
for i in 1..500000000:
spawn inc(x)
Longtime()
Also, some clarification on what the parallel block does, since the manual isn't too clear: the parallel block allows using spawn to mutate the environment, as long as the mutations satisfy the checks that parallel introduces. Your example only works when the for loop is placed in a parallel block, as it creates the necessary checks and data needed to spawn inc(x) without data races.