Clojure中的Threadlocal计数器 [英] Threadlocal counter in Clojure
本文介绍了Clojure中的Threadlocal计数器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个网络应用程序,我想要能够跟踪一个给定的函数在一个请求(即线程)中被调用的次数。
I have a web app where i want to be able to track the number of times a given function is called in a request (i.e. thread).
我知道
推荐答案
这是一个非线程本地方法,
There's a tool for this in useful called thread-local
. You can write, for example, (def counter (thread-local (atom 0)))
. This will create a global variable which, when deref
ed, will yield a fresh atom per thread. So you could read the current value with @@counter
, or increment it with (swap! @counter inc)
. Of course, you could also get hold of the atom itself with @counter
and just treat it like a normal atom from then on.
这篇关于Clojure中的Threadlocal计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文