最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

multithreading - Nim language - createThread type mismatch at compile time - Stack Overflow

programmeradmin2浏览0评论

I am writing some test code to see if I can manage threads (which will be useful later to write a backup program that does lots of backups, compression and encryption, so it needs plenty of data transfer speed and computing speed. Parallel processing would reduce execution time a lot).

As at this stage I don't care about mixing console output and there's only one line of code where shared variables are modified, I think I can safely ignore locks, for now. I just want to be able to execute lengthy operations in parallel and I am focussing on that alone, for now. Here's my code.

import std/[os, osproc, strutils, strformat, times, terminal]

var thrlist: array[0..11, Thread[(string, string, string)]]
var filename, content, prefix: string

proc onetask(fn, txt, src: string) {.thread.} =
  var x = 0
  var mycmd : string
  mycmd = fmt"cat {src} > {fn} && echo '{txt}' >> {fn} && cat {src} >> {fn}"
  x = execCmd(mycmd)
  if x != 0 :
    echo fmt"Error {x} with {fn} \nI am so very sorry for any inconvenience!"

eraseScreen()
setCursorPos(0, 0)
echo "Please, enter the content of the new file, at your earliest convenience:"
content = readLine(stdin)
echo "Thank you very much, indeed."
echo "Now, please, enter the prefix, at your earliest convenience:"
prefix = readLine(stdin)
echo "Again, thank you so much for that."
for i in 3..14:
  filename = fmt"{prefix}{i}.txt"
  thrlist[i - 3].createThread(onetask, (filename, content, "src.txt"))
joinThreads(thrlist)
echo "Sounds like everything is done. Thank you for your collaboration. See you again soon. Bye now."
quit(0)

When I try to compile, I get:

paratest.nim(24, 17) Error: type mismatch: got <Thread[(string, string, string)], proc (fn: string, txt: string, src: string){.gcsafe, locks: 0.}, (string, string, string)> but expected one of: proc createThread[TArg](t: var Thread[TArg]; tp: proc (arg: TArg) {.thread, nimcall.}; param: TArg) first type mismatch at position: 2 required type for tp: proc (arg: TArg){.nimcall, gcsafe.} but expression 'onetask' is of type: proc (fn: string, txt: string, src: string){.gcsafe, locks: 0.} 1 other mismatching symbols have been suppressed; compile with --showAllMismatches:on to see them

expression: createThread(thrlist[i - 3], onetask, (filename, content, "src.txt"))

My code looks to me equivalent to all examples I've been able to find. What am I missing, please? Thank you in advance for any help. PS: I am less than an amateur beginner programmer, as I only write code when I can't find a software that satisfy my personal needs in my computer use, so, please, five all my rookie mistakes in my code, my way of going about programming, research, finding answers and using Internet and the tools it provides to solve issues. Thanks.

PS: I tried with both nim compiler version 1.6.10 and 2.2.2 with identical results. That was on a Debian 12 with Kernel 6.12. Since, I have decided to migrate the onetask procedure into a separate source code and use the main code to call it (as a separate program, via execCMD()) in the background, letting bash sort the multitasking. I'll leave the question here, for the time being, just in case someone finds out what the issue was or for anyone who would find it useful.

发布评论

评论列表(0)

  1. 暂无评论