On Mon, Oct 26, 2009 at 11:25 AM, David L. Rager <ragerdl@gmail.com> wrote:
Please excuse me.  I had a major typo.  Corrected:

But if you read the variable in one particular thread, there is no
promise that its value will persist (unaltered by another thread)
while you execute the branch of a conditional that choses a path based
on the variable's value.

On Mon, Oct 26, 2009 at 1:22 PM, David L. Rager <ragerdl@gmail.com> wrote:
> Pretty sure the answer to your question is "no."
>
> I think common practice (in CCL) is that a single setf is assumed to
> write a value to an address "atomically" at the assembly level (it
> should just be a move and it should be the case that if two threads
> write to a pointer, that you end up with either the first or second
> value, not a mashup of the two values), but if you read the variable
> in a thread and there is no promise that its value to persist while
> you execute a branch of an if (or other conditional that choses a path
> based on a variable's value).
>

OK thanks.  In my current example, the new value does not depend on the old one, so it sounds like, since the pointer is guaranteed never to be seen in an intermediate state, I'm ok to not use locks.

Btw, I assume you meant SBCL, not CCL?

- Bhaskara


 
> (defvar *val* 0)
>
> (run-thread (lambda () (setf * val* (1+ * val*)))
> (run-thread (labmda () (setf * val* (1+ * val*)))
>
> After the threads finish, *tmp* will be either 1 or 2 at the end of
> the execution, depending on whether the race conditions triggered.
>
>
> Instead you'd need, the following, which results in *val* always containing 2.
>
> (defvar * val* 0)
> (defvar *lock* (make-lock))
> (run-thread (lambda () (with-lock *lock* (setf * val* (1+ * val*))))
> (run-thread (lambda () (with-lock *lock* (setf * val* (1+ * val*))))
>
>
>
> If you just want an atomic counter, you could consider the function
> sb-ext:atomic-incf.
>
> BTW, the notation is a bit abstract, because I have my own wrappers
> for the SBCL and CCL multi-threading libraries and prefer to use said
> abstractions.
>
>
> On Mon, Oct 26, 2009 at 1:07 PM, Bhaskara Marthi
> <bhaskara@willowgarage.com> wrote:
>> In other words, is it safe to have multiple threads reading a special
>> variable and/or using setq to change it, without any locking?
>> - Bhaskara
>>
>> --
>> Bhaskara Marthi
>> Research Scientist
>> Willow Garage Inc.
>> 650-475-2856
>>
>> ------------------------------------------------------------------------------
>> Come build with us! The BlackBerry(R) Developer Conference in SF, CA
>> is the only developer event you need to attend this year. Jumpstart your
>> developing skills, take BlackBerry mobile applications to market and stay
>> ahead of the curve. Join us from November 9 - 12, 2009. Register now!
>> http://p.sf.net/sfu/devconference
>> _______________________________________________
>> Sbcl-help mailing list
>> Sbcl-help@lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/sbcl-help
>>
>>
>



--
Bhaskara Marthi
Research Scientist
Willow Garage Inc.
650-475-2856