If you attended https://event.afup.org/afup-day-2022/programme/#3875, you might know that I am currently working on a PHP module for web servers written in Go. While testing my upcoming library, I encountered strange memory access issues related to the threads created by the Go runtime (https://dave.cheney.net/2016/01/18/cgo-is-not-go…
This behavior is incompatible with C code that relies on https://en.wikipedia.org/wiki/Thread-local_storage, such as http://blog.jpauli.tech/2017-01-12-threads-and-php-html/.
Let’s write a test program to understand how runtime.LockOSThread() works, and if the system really reuses thread IDs: // This Go program demonstrates the behavior of runtime.LockOSThread() and runtime.UnlockOSThread().
// To prove this, we store used thread IDs in a global variable, and some data in each local thread using pthread_setspecific().