In the documentation (https://nim-lang.org/docs/threads.html) I can see that there is no return value from the joining thread available. This exist for the most common OS, like Windows and all the POSIX variants. Shouldn't joinThread return a return value from the exiting thread?
Which also brings us the question if Thread type must be changed in that case in order to accommodate the return type.
Is there any reason why we wouldn't want to support return values from threads?
Windows threads return a cint, pthreads return a pointer, so you could say, ok, they return an exit code that's int32.
Ive done this but I'm not sure it was worth the trouble, it's just separating the discriminant from a discriminated union.
Ive done this but I'm not sure it was worth the trouble, it's just separating the discriminant from a >discriminated union.
Even in the pointer case, what's the thread going to give you? A pointer to something on it's stack or >on its local heap? No thanks.
I was thinking more an alignment with the "old ways" of threading. There might be where someone wants to use that, like porting an old library.
Also threadpools drags in a lot of other modules.
What's the "old ways" of threading? Haven't you see benchmarks, where creating and joining threads >was slower than single threaded? None ever done it that way. If you have a long running thread and >don't want to use a threadpool then the solution is to use a synchronization primitive. There are a few >in https://github.com/planetis-m/sync/
Maybe I was unclear by the term "old ways". My thought was the threads module should provide a platform independent interface for threading that use the OS primitives like pthreads, Windows CreateThread and such. In order to align the functionality, the join primitives should provide a return value if the implementer wishes to use them.
I understand that spawn use return values but with an abstraction above the threads module. Another way to support this in the threads module is just to add a getReturnValue function.
Oh, I am interested to how do I return value from a thread? My guess is that I have to create some kind of object with field result: T then pass a reference to it to thread and when thread finishes it's work I have to set this result variable. And I have to have a lock around this result field. And I try to get the result I have to acquire the lock.
I think I end up with FlowVar or Promise or Future kind of object. But I don't know how to pass it safely to the thread? Do I have to pass it as ref argument of the thread, or maybe I have to pass it as unsafe ptr ?
... to add a getReturnValue function.
Why don't just take the return value, compareing it with zero (or any other code that indicates error occurence) and raise a ValueError on error? The only bad about not checking the return code is unable to know if the joining succeeds.