Closed stepancheg closed 12 years ago
@jckarter you are right, thanks for pointing it out. Fixed with another hack of shared pointers.
Function cannot be stored in Thread object, because in certain situations thread needs to be detached. We cannot implement thread detach if thread contains a reference to currently executed code. Thread procedure must be responsible for destroying of Function
object (however, more generic code still can be moved from platform
module to core
module, and platform
module should work only with code pointers).
I think lib-clay needs something like lambda.Function
that can be parameterized by smart pointer type. So uniquepointers should be used instead of sharedpointers.
It's probably sufficient to have a UniqueFunction
counterpart to Function
. That would definitely make this easier to reason about—sole ownership of the Function
should be moved to the thread procedure once it's started.
@jckarter uploaded a better and simpler version with FunctionBody
made public. UniqueFunction
(as well as parameterized Function
) seems to be overcomplication for this task.
That looks much better indeed. Making the memory management explicit in this case is easier to follow than the implicit memory management of the smarter types.
This looks good now, aside from the lack of synchronization in join you noted in a comment, though having multiple threads trying to join the same thread in a program sounds like a problematic design anyway.
Linux test failures:
[lib-clay/threads/future]
Failure: compiler error
Error:
/tmp/clayobj-69edae33.obj:/home/jeremy/dev/clay/clay/test/lib-clay/threads/future/test.clay:function threads.core.platform.startThreadImpl_28_Pointer_5B_FunctionBody_5B__5B__5D__2C__20__5B__5D__5D__5D__29__20_UInt64_20_clay: error: undefined reference to 'pthread_create'
/tmp/clayobj-69edae33.obj:/home/jeremy/dev/clay/clay/test/lib-clay/threads/future/test.clay:function threads.core.joinThread_28_Thread_29__20_clay: error: undefined reference to 'pthread_join'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
[lib-clay/threads/core/simple]
Failure: compiler error
Error:
/tmp/clayobj-d8890559.obj:/home/jeremy/dev/clay/clay/test/lib-clay/threads/core/simple/main.clay:function main: error: undefined reference to 'pthread_create'
/tmp/clayobj-d8890559.obj:/home/jeremy/dev/clay/clay/test/lib-clay/threads/core/simple/main.clay:function main: error: undefined reference to 'pthread_join'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
[lib-clay/threads/core/unhandledexception]
Failure: compiler error
Error:
/tmp/clayobj-ca95d797.obj:/home/jeremy/dev/clay/clay/test/lib-clay/threads/core/unhandledexception/main.clay:function main: error: undefined reference to 'pthread_create'
/tmp/clayobj-ca95d797.obj:/home/jeremy/dev/clay/clay/test/lib-clay/threads/core/unhandledexception/main.clay:function main: error: undefined reference to 'pthread_join'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
In Linux we need to link with -lpthread
. How to fix it properly?
You could add a buildflags.linux.txt
to the tests containing -lpthread
.