Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save UNIcodeX/b209b2118e3206acb96557d7d7b64310 to your computer and use it in GitHub Desktop.

Select an option

Save UNIcodeX/b209b2118e3206acb96557d7d7b64310 to your computer and use it in GitHub Desktop.

Revisions

  1. UNIcodeX revised this gist Apr 13, 2020. 1 changed file with 3 additions and 2 deletions.
    Original file line number Diff line number Diff line change
    @@ -8,10 +8,11 @@ var
    a: array[20, string]

    initLock lock

    # Can do this explicitly with:
    proc process(b: ptr array[20, string], s: string) =
    # proc process(b: ptr array[20, string], s: string) =
    # or lazily with:
    # proc process(b: ptr, s: string) =
    proc process(b: ptr, s: string) =
    withLock lock:
    b[][parseInt(s)] = s

  2. UNIcodeX revised this gist Apr 13, 2020. 1 changed file with 8 additions and 3 deletions.
    Original file line number Diff line number Diff line change
    @@ -8,15 +8,20 @@ var
    a: array[20, string]

    initLock lock

    # Can do this explicitly with:
    proc process(b: ptr array[20, string], s: string) =
    # or lazily with:
    # proc process(b: ptr, s: string) =
    withLock lock:
    b[][parseInt(s)] = s

    when isMainModule:
    var b = addr(a)
    # could either explicitly define the pointer type
    # var ptra: ptr array[20, string] = addr(a)
    # or lazily with:
    var ptra = addr(a)
    parallel:
    for i in 0 ..< a.len:
    spawn process(b, $i)
    spawn process(ptra, $i)
    sync()
    echo a
  3. UNIcodeX revised this gist Apr 13, 2020. 1 changed file with 1 addition and 1 deletion.
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    import locks, threadpool, strutils
    # compile with:
    # nim c -r --threads:on -d:danger --threadanalysis:off --gc:arc
    # nim c -r --threads:on -d:danger --gc:arc
    {.experimental: "parallel".}

    var
  4. UNIcodeX created this gist Apr 13, 2020.
    22 changes: 22 additions & 0 deletions parallelProcessingGlobalVarWithExplicitPointerThreadanalysisOn.nim
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    import locks, threadpool, strutils
    # compile with:
    # nim c -r --threads:on -d:danger --threadanalysis:off --gc:arc
    {.experimental: "parallel".}

    var
    lock: Lock
    a: array[20, string]

    initLock lock

    proc process(b: ptr array[20, string], s: string) =
    withLock lock:
    b[][parseInt(s)] = s

    when isMainModule:
    var b = addr(a)
    parallel:
    for i in 0 ..< a.len:
    spawn process(b, $i)
    sync()
    echo a