[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: question about atomics
From: |
Chris Vine |
Subject: |
Re: question about atomics |
Date: |
Fri, 26 May 2017 12:05:40 +0100 |
On Fri, 26 May 2017 12:37:47 +0200
address@hidden wrote:
> Hello,
>
> I'm making a simple program that processes a lot of mail in separate
> guile threads.
>
> I would like a counter of how many messages have been processed so I
> made an attempt with atomics.
>
> first:
> (define *msgcount* (make-atomic-box 0))
>
> and later:
> (atomic-box-swap! *msgcount* (+ 1 (atomic-box-ref *msgcount*)))
>
> This of course doesn't work very well, and I didnt expect it to
> either. (usually the count is low by a couple of hundred messages)
>
> So basically, how do I get something similar to the swap! function in
> clojure? Did I miss something in the documentation for guiles atomics?
>
> I think I expected to be able to do the following:
> (atomic-box-swap! *msgcount* (lambda (atom) (+ 1 (atomic-box-ref
> atom))))
That won't work. You have to use atomic-box-compare-and-swap!, in order
to detect interleaving by other threads. For more on this, google
"compare and swap".