Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is ThreadID consistent when shuffling Haskell threads around OS threads?

In Haskell forkIO creates an unbound (Haskell) thread, and forkOS creates a bound (native) thread. The answer to a previous question here that I had mentioned that Haskell threads are not guaranteed to stay on the same OS thread, which seems to be supported by the documentation for the Control.Concurrent module. My question is, if a running Haskell thread gets swapped to another OS thread, will its ThreadID remain the same?

like image 692
Dwilson Avatar asked Feb 19 '23 20:02

Dwilson


1 Answers

Yes.

A ThreadId is an abstract type representing a handle to a thread.

This is how you send asynchronous signals to specific threads: with the ThreadId. It does not matter which OS thread is involved, and it is often quite likely that the targeted thread is not bound to any OS thread at all (e.g., it is sleeping).

The existence of "OS threads" is somewhat an implementation detail, although you'll need to manage them if you use the FFI with certain libraries. Otherwise, you can mostly ignore OS threads in your code.

like image 169
Dietrich Epp Avatar answered Mar 08 '23 11:03

Dietrich Epp