On Thu, Dec 5, 2013 at 9:56 PM, Ugorji Nwoke <
ugo...@gmail.com> wrote:
> I have been thinking of an implementation that uses a set of worker
> goroutines all locked to their own OS threads. Channels are used to send
> tasks to, and receive results from the worker goroutines.
>
> Will this effectively make cgo interactions cheap? As the OS thread is
> exclusive to the goroutine, I assume there is no need to interact with the
> scheduler.
No, the scheduler interaction wouldn't go anywhere, when you enter cgo
it still needs to check if there are more goroutines scheduled and
whether or not additional threads are needed. What that would do is
simply create that many threads in advance which wouldn't be able to
do anything else, which you probably don't need as concurrent Go
programs that do syscalls tend to have spare threads lying around
anyway.
Moreover, using workers and channels would actually add complexity in
your case, for example if you need results of those cgo calls you
would need to create reply channels and communicate results over them.
It would make sense if you need e.g. buffering, but not otherwise, as
cross-thread communication is not free.
If you need to limit cgo concurrency you should probably use some form
of semaphore. Sadly runtime semaphores don't seem to be exposed in
standard library, but it's easy to do based on either a buffered
channel or sync.Mutex + sync.Cond + counter.
> Do I set GOMAXPROCS in this situation to 4, or 20, or otherwise?
GOMAXPROCS controls Go parallelism, so if you want 4 goroutines to
execute simultaneously, then it should be 4.