views:

143

answers:

3

I really want to be able to have a function pass some responsiblity to another object and then return on a different thread. I know this means the caller would not know that it had switched threads, but I REALLY want to know if there is ANY way to have the function return on a different thread than it was called on.

+3  A: 

You seem to have some misunderstanding of how threads work. You cannot enter a function in one thread and exit it in another (Threads have separate stacks and the stack maintains what function you are in). What you can do is use Asynchronous api's which invoke a function which returns immediately and allows retrival of the result later.

Options include:

Returning some value which will allow querying of the state of the asynchronous operation ('are you done yet') and allowing retrieval of the result once it indicates it is done ('give the result') These will often allow usage on multiple threads.

Providing a function to the original function which will be invoked later when the result finishes (often the thread on which this callback occurs is not well defined since it is likely to come from some thread pool.

Asynchronous api's are often complex.

Something often required is for one function call to synchronously invoke some other function on a different thread. Common examples are smuggling some call onto the event loop dispatch thread (in windows by the Invoke() method).

It is possible that a system could provide a thread equivalent of fork() where by the entire thread is cloned (including the stack thus far) then both threads return from the function. This behaviour would likely be extremely bug prone.

Continuations may provide a controlled way to do what you are trying to achieve but they are very complex and may damage the readability of what you are trying to achieve.

ShuggyCoUk
Shuggy's answer is good. If you want to look at an interesting model for F# message passing, see: http://strangelights.com/blog/archive/2007/10/24/1601.aspxThis emulates the Erlang communication infrastructure and gives a good demonstration of thread use.
Godeke
A: 

Using .NET asynchronous delegates, you can pass an object to a function that executes on a separate thread, and have it execute a callback function when it's finished. That callback function will be called on a different thread than the main thread.

If you're familiar with C# or Visual Basic, you can find a lot of information about using asynchronous delegates in .NET. I haven't seen a good treatment of asynchronous delegates in F#.

Jim Mischel
+2  A: 

Async workflows give the illusion of something like this, e.g.

async {
    printfn "on thread 1..."
    let! r = stream.ReadAsync()
    printfn "on thread 2 now..."
    ...
}

but it's just an illusion to make the code easy to read, write, and reason about; under the hood it's doing all the normal async stuff with BeginRead/EndRead/callbacks/etc.

Brian