Pthread_setname_np(3)

Let me preface this with the fact that I’m not an expert in pthreads by any measure. Looking around I’ve found that Ruby’s second thread is named:

$ cat /proc/30193/task/*/comm
ruby
ruby-timer-thr

Maybe it would make sense for Crystal to do the same?

Another suggestion would be to expose a way to name the main thread (and hopefully any thread past 1.0) other than writing to /proc.

I am not an expert on pthreads but why would we want to match that pattern? Are their tools or something that expect that pattern?

It would make sense to name runtime threads in order to be able to identify them.

1 Like

But we only have one thread at the moment.

1 Like

So, if I’m mistaken here please set me straight.

Underneath each of the /proc/[pid] directories, a task subdirectory contains subdirectories of the form task/[tid], which contain corresponding information about each of the threads in the process, where tid is the kernel thread ID of the thread.

$ grep -c processor /proc/cpuinfo
2
$ cat ./test.cr
#!/usr/bin/env crystal
sleep 1
system  "ls -lA /proc/#{Process.pid}/task"
sleep 1
spawn do
        sleep
end
system  "ls -lA /proc/#{Process.pid}/task"
sleep 1
$ ./test.cr
total 0
dr-xr-xr-x 7 x x 0 May 21 19:28 29680
dr-xr-xr-x 7 x x 0 May 21 19:28 29681
total 0
dr-xr-xr-x 7 x x 0 May 21 19:28 29680
dr-xr-xr-x 7 x x 0 May 21 19:28 29681

The real program has 12 tasks, running on a 12-vcore CPU, so I guess this is a thread pool?

I guess my first hunch that the second thread was the GC/scheduler was plain wrong.

This could be part of Crystal’s concurrency goals.

The non-main (tid != pid) threads are the GC, but we don’t have control over those threads to name them.

I’m curious as to why there are ncpu - 1 of them though.