> I am working with a C librairy which is not completely thread-safe. I would
If the only constraint you have is that the C library is not
_thread-safe_, and as long as the C library doesn't have local
thread-storage, you don't really have to care about which OS-thread is
running it. You just must ensure it's not being called concurrently,
which is a much nicer constraint to work with than restricting logic
to a named thread.
One straightforward way to handle that is to have a global lock,
managed in the Go side of your API, that you acquire and release as
you touch the C library internals. This will avoid reserving an OS
thread, will enable multiple goroutines to interact with the Go
interface of your package, and will also be very fast if it's not
being used concurrently, since the locks are generally optimized away
with atomic operations if you have a single client.
If you have other constraints, then there are different approaches
that may be used.
--
Gustavo Niemeyer
http://niemeyer.net
http://niemeyer.net/plus
http://niemeyer.net/twitter
http://niemeyer.net/blog
-- I never filed a patent.
GOMAXPROCS limits the number of threads running
Go code, but cgo calls do not count as Go code, so
you can still see multiple threads with GOMAXPROCS=1.
If the library is squirreling away OS-thread-local data
then you need runtime.LockOSThread. If not, then
you just need a mutex.
Russ