Passing a socket to a thread

I'm writing an async-std application which is supposed to put some work onto worker threads (using threadpool).

I've run into [Solved] Async Read/Write using TcpStream and while there's a solution ([Solved] Async Read/Write using TcpStream - #2 by vitalyd) it does not work with async-std.

The problem is identical, except that async-std doesn't not have the try_clone function. I figure what I want to do is a very common pattern, so what's the appropriate way to handle it? I only need to pass the writer to the thread, not the reader.

Should I just move the stream object into an Arc and make sure the threads clone it?

split is the function your likely after.

The links are confusing as they mention async but are blocking. (Not sure how you intent to use thread as not directly suitable for async io; maybe channel back result to async code.)

1 Like

Thank you!

The main thread reads "hash file" requests (an algorithm, an identifier and a filename: sha2/256 foo /tmp/bar) from a connected client. The connection handler will launch a thread to hash the file (since the file can be very large). Once the hash operation is done the thread writes back the identifier and the hash (foo 3afbc5734..) to the connected client.

The writer will need to be shared and protected by a Mutex, but afaict there's no need, nor benefit, for the data to be returned to the async thread.

Any tips on how to accomplish this in a simpler manner is most welcome.

Later I intend to make an async-friendly file hasher future, but for now I need a quick solution for this.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.