Most basic nothrow, pure, @safe functions?

Steven Schveighoffer schveiguy at yahoo.com
Thu Mar 20 20:04:36 PDT 2014


On Thu, 20 Mar 2014 22:14:59 -0400, Steven Schveighoffer  
<schveiguy at yahoo.com> wrote:

> On Thu, 20 Mar 2014 22:00:15 -0400, Walter Bright  
> <newshound2 at digitalmars.com> wrote:
>
>> On 3/20/2014 6:40 PM, Steven Schveighoffer wrote:
>>> How do they affect global state?
>>
>> Mutexes implicitly share state. It's the reason they exist. They can't  
>> be pure, because pure functions don't share state.
>
> I view it differently. I feel like locking and unlocking a mutex is  
> pure. After calling lock, the same thing *always* happens. After  
> unlocking, the same thing *always* happens. It's a weird thing -- when  
> you lock a mutex, you own it after it's locked. It's no longer shared.  
> It can be thought of as pulling memory out of the heap to temporarily  
> own, and then putting it back, just like memory allocation (which is  
> considered pure).

Thinking about it some more, I see what you mean -- an unshared mutex is  
useless.

But at the same time, some "logically" pure functions cannot be so without  
mutexes. E.g. memory allocation.

-Steve


More information about the Digitalmars-d mailing list