Does Racket support multithreading?
Asked Answered
R

1

6

I want to write a multithreading program in Racket that actually utilizes multiple processes with shared memory space like pthread in C. Racket provides "thread", but it only uses one process to execute multiple threads. It also provides "subprocess" for executing new programs via command line that runs on multiple processes, but those programs cannot share the same memory space.

Runway answered 26/6, 2014 at 1:48 Comment(0)
S
4

Don't do that.

Racket does provide parallelism via futures and places, but they do not provide (unrestricted) shared memory spaces. If you want to send data from one thread to another, use a place channel.

As Greg Hendershott points out, you can send a shared vector via a place channel, which provides a shared space to use. (But that's not the same thing as sharing all the memory references, which is what someone familiar with, say, Java-style threading might expect. And the latter is what my "don't do that" refers to.)

If you really want to use pthread-like threading, Guile does provide them, but then you won't be using Racket any more. ;-)

Scarborough answered 26/6, 2014 at 2:28 Comment(5)
Although I haven't tried doing it myself, it looks like Racket places can use shared memory spaces.Pohai
@GregHendershott Yes, but it's a much more restricted form than what pthreads provides, which is what the OP is asking for (whether they really need it, is another story; XY problem and all).Scarborough
Yes. Simply wanted to point out that one thing you can send over a channel is a shared-memory object. Good idea to use? Often not. Avail if you truly need? Yes. IOW I agree re XY problem.Pohai
@GregHendershott Indeed. Other than shared vectors, though, you generally cannot send mutable objects over. (You can send strings, vectors, and hash tables, but they will be converted to an immutable version at the other end.) This is distinctly different from Java-style memory sharing, which is what Racket doesn't support (for good reasons, in my opinion, since it can be dangerous).Scarborough
Now let's say I'm okay with using communication channel. It seems like using multiple threads in Racket is time multiplexing and not actually using multiple processes. I only see one core running when I execute multiple threads. Is there a way to use multiple cores?Runway

© 2022 - 2024 — McMap. All rights reserved.