You need to do this on your own. As you said, passing stuff around is the only approach viable here.If you feel the need to have goroutine-local data, maybe try to figure out a way to avoid that entirely. These kinds of requirements make for confusing code-bases.
Perhaps if you tell us the problem you're trying to solve, we can suggest an alternate route?
On 2012-06-26, at 1:39 PM, John Asmuth wrote:You need to do this on your own. As you said, passing stuff around is the only approach viable here.If you feel the need to have goroutine-local data, maybe try to figure out a way to avoid that entirely. These kinds of requirements make for confusing code-bases.I agree in general, but occasionally it would be the cleanest solution.Perhaps if you tell us the problem you're trying to solve, we can suggest an alternate route?Thanks, but at the moment it's mostly a theoretical thing. I'm working on something similar to some older Java and Ruby projects, and the Java solution used thread locals for among other things holding locks.
Are there cases when semaphore locks are needed any more (within this paradigm)?
On Wed, Jun 27, 2012 at 4:52 PM, John Asmuth wrote:
> On Wednesday, June 27, 2012 10:41:33 AM UTC-4, Øyvind Teig wrote:
>>
>> Are there cases when semaphore locks are needed any more (within this
>> paradigm)?
>
>
> The times I find that a lock is better than a goroutine/channel solution are
> things where you need an immediate response. For instance, if you want to
> have a shared map. Setting up the channel infrastructure to send the value
> back, and make sure the caller maps it to the appropriate key, is a waste of
> time (unless it fits into some larger picture). Much easier to just lock map
> accesses.
>
> I'm happy to be shown why I'm wrong here, and what an effective and simple
> channel/goroutine-based solution might look like.
You could replace the mutex with a channel with a buffer of 1 and use
it to transfer ownership of the resource. The pattern here is
identical to locking and unlocking (receiving and sending) and there's
no additional goroutine of overhead. Of course, you'd just be
emulating an explicit lock with a channel that is itself implemented
with explicit locks.
I think you're absolutely right.. channels are a
powerful model for thinking about concurrent software, but they will
not make sense in every program for the same reason that procedure
calls are still in-lined in many languages.
- Jim
Øyvind Teig writes:
> Really? Some at golang-nuts said there was no preemption, and scheduling is
> done at (channel) synchronization points and at "some system calls", so was
> still necessary to for Google to use locks to implement the channel
> primitive?
There is no preemption, and scheduling is done at synchronization
points, but Go programs run multi-threaded. Locks are required.
> Where is the code for this?
src/pkg/runtime
notably chan.c and proc.c.
Ian
In reference to those idioms, I liken one such pattern to volatiles in c#(which btw is a messy affair in c#), where a public method in go is implemented as a channel send to the "center" of a struct which has a state machine (or run loop)at its heart, and as a result is totally safe. And when a response is needed the request{chan response} idiom can hide behind the method interface. All very neatly.
Simon Watt
I think this has something to do with the first class nature of channels in go. If I remember correctly they are not in occam, in fact I don't think you can have multiple readers or writers in parallel to a channel.
Can more than one thread really run the same goroutine concurrently?
It seems weird.
/Henrik
On 29 June 2012 13:17, Henrik Johansson wrote:
> Can more than one thread really run the same goroutine concurrently?
>
> It seems weird.
>> I think the part you are missing is that goroutines are scheduled onto OS
>> threads, and that more than one thread can be running a goroutine at once.
Not the /same/ "a goroutine". Different threads may be running, so different
goroutines may be running at the same time.
Chris
--
Chris "allusive" Dollin
No, several goroutines are multiplexed onto a set of threads.
/Henrik
No, several goroutines are multiplexed onto a set of threads.
/Henrik
Thanks!- Øyvind
Ian