On Thu, Aug 29, 2013 at 12:58 PM, Luke Scott <
lu...@visionlaunchers.com> wrote:
> I'm using a C++ library, wrapped in C, in Go. The size of the library is
> very large, and it's impractical to rewrite it in Go (500k+ lines). The
> library is particular sensitive to changing threads. You have an opening
> ->Enter() function and a closing ->Exit() function. The calls in-between
> expect that the thread is the same. I've wrapped each of these calls in C
> for Go to call (enter, exit, etc...). Because of the sensitivity to threads,
> I'm currently doing runtime.LockOSThread() before ->Enter(), and
> runtime.UnlockOSThread() after ->Exit().
>
> Is the called C (cgo) function inside the same thread as the Go routine?
Yes.
> From what I understand there are some ways to call C more directly without
> cgo. What are they, and what are the implications?
The other available option is SWIG. It has the same limitations as cgo.
> How does any of the above effect GOMAXPROCS, and given that the C++ library
> is picky about threads, does it matter anyway?
GOMAXPROCS will still limit the number of threads that the Go runtime
will run concurrently, regardless of your calls to LockOSThread. But
the Go runtime will always create at least one unlocked thread as
needed. And non-Go code will not count against the GOMAXPROCS count.
> One of the things I have to do with this library is create a data-object
> from a Go struct/map. In order to do this, I have to do something like this:
>
> runtime.LockOSThread()
> C.enter()
> obj := C.object_new()
> C.object_set(obj, C.CString("key"), C.CString("value")) // probably need a
> free() for CString()...
> // build a nested object...
> C.exit()
>
> What would be the optimal way to do this, where potentially hundreds of C
> calls are being made from Go?
Sorry, I'm not sure what you are asking.
Ian