Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Threadpool awaitAny() problem on macOS #7638

Closed
allochi opened this issue Apr 17, 2018 · 7 comments
Closed

Threadpool awaitAny() problem on macOS #7638

allochi opened this issue Apr 17, 2018 · 7 comments

Comments

@allochi
Copy link

allochi commented Apr 17, 2018

Hi,

I'm having a problem running the following code on macOS 10.13 Intel i7, although, it runs perfectly on a Linux server. I also tried to run it on a different macOS machine with the same problem.

The problem:
The code runs, threads are done with their tasks, but awaitAny() doesn't report back all the finished threads/tasks, and the while loop never finishs, since awaitAny() never returns -1.

I talked about this problem in Gitter, at the time the problem in the sample code was solved by using setMinPoolSize(), but then on a real world program (or extended sample), even setting setMinPoolSize(MaxThreadPoolSize) causes the same problem.

import threadpool, os

proc timer(d: int): int =
  echo("sleep: ", d)
  sleep(d)
  echo("done: ", d)
  return d

var durations = [4000, 2000, 3000, 4000, 5000, 6000]
var tasks: seq[FlowVarBase] = @[]
var results: seq[int] = @[]

for i in 0 .. durations.high:
  tasks.add(spawn timer(durations[i]))

var index = awaitAny(tasks)
while index != -1:
  results.add(^cast[FlowVar[int]](tasks[index])) # A. cast FlowVarBase to get the value
  tasks.del(index)
  echo($results)
  index = awaitAny(tasks)

sync()
@allochi
Copy link
Author

allochi commented Apr 18, 2018

Hi, I'm trying to do all possible tests, so I spent some time today testing this on Docker and Vagrant, I get the same problem with both on the same machine.

I can understand why the problem persist on Docker, but Vagrant suppose to be a full VM.

Anyway, I'm happy to do any required tests, and help as much as possible.

@dom96
Copy link
Contributor

dom96 commented Apr 18, 2018

That cast looks dangerous, can't you use a safe cast there?

@allochi
Copy link
Author

allochi commented Apr 18, 2018

@dom96 I really don't care about the cast since it's just a sample code, even if you remove it the problem remains, it won't work on macOS (I tried)

Regarding the cast, to be honest I wish if there is another way to return a result within FlowVarBase , I don't see anything in the documentation, your advice is appreciated.

@Araq Araq closed this as completed in 0dc4d6d Apr 19, 2018
@Araq
Copy link
Member

Araq commented Apr 19, 2018

Regarding the cast, to be honest I wish if there is another way to return a result within FlowVarBase, I don't see anything in the documentation, your advice is appreciated.

Use a type conversion like so FlowVar[int](myFlowVarBase)

@allochi
Copy link
Author

allochi commented Apr 19, 2018

Thanks a lot @Araq, I confirm, the code works now.

Regarding casting, what you suggest is what I tried originally, but I get this error:

Error: type mismatch: got <FlowVarBase> but expected 'FlowVar[system.int]'

@Araq
Copy link
Member

Araq commented Apr 19, 2018

but I get this error

That would be another bug. :-)

@allochi
Copy link
Author

allochi commented Apr 19, 2018

Yeah, sorry, and thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants