# Is it OK to modify different histogram bins from different threads using \`AddBinContent\`?

**URL:** <https://root-forum.cern.ch/t/is-it-ok-to-modify-different-histogram-bins-from-different-threads-using-addbincontent/33442>\
**Category:** Newbie\
**Created:** [April 3, 2019, 1:11pm UTC](https://root-forum.cern.ch/t/is-it-ok-to-modify-different-histogram-bins-from-different-threads-using-addbincontent/33442 "2019-04-03T13:11:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![berserker](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/berserker/32/17693_2.png) [@berserker](https://root-forum.cern.ch/u/berserker)\
**Post date:** [April 3, 2019, 1:11pm UTC](https://root-forum.cern.ch/t/is-it-ok-to-modify-different-histogram-bins-from-different-threads-using-addbincontent/33442/1 "2019-04-03T13:11:52Z")

</div>

If I don’t care about mean/stddev/entries consistency.

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [April 3, 2019, 2:41pm UTC](https://root-forum.cern.ch/t/is-it-ok-to-modify-different-histogram-bins-from-different-threads-using-addbincontent/33442/2 "2019-04-03T14:41:34Z")

</div>

HI,  
I don’t think is safe because two updates of the same bin can happen at the same time and then the result might not be correct. I would create an histogram for each thread or lock when calling the AddBinContent function (but will kill all performances)

Lorenzo

---

<div class="post-metadata">

**Author:** ![berserker](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/berserker/32/17693_2.png) [@berserker](https://root-forum.cern.ch/u/berserker)\
**Post date:** [April 3, 2019, 2:43pm UTC](https://root-forum.cern.ch/t/is-it-ok-to-modify-different-histogram-bins-from-different-threads-using-addbincontent/33442/3 "2019-04-03T14:43:29Z")

</div>

> [@moneta](#):
>
> two updates of the same bin can happen at the same time

The sets of bins for each thread are not overlapping.

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [April 3, 2019, 3:11pm UTC](https://root-forum.cern.ch/t/is-it-ok-to-modify-different-histogram-bins-from-different-threads-using-addbincontent/33442/4 "2019-04-03T15:11:37Z")

</div>

The playground for disaster is greater than the size of a bin content storage - it’s a cache line. But in practice we are aware of people doing what you say, and they survive 😉 But it’s risky, there’s no guarantee that the result is guaranteed to be correct. As Lorenzo said, you could use `TThreadedObject` [https://root.cern/doc/master/classROOT\_1\_1TThreadedObject.html](https://root.cern/doc/master/classROOT_1_1TThreadedObject.html)

---

<div class="post-metadata">

**Author:** ![berserker](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/berserker/32/17693_2.png) [@berserker](https://root-forum.cern.ch/u/berserker)\
**Post date:** [April 3, 2019, 3:17pm UTC](https://root-forum.cern.ch/t/is-it-ok-to-modify-different-histogram-bins-from-different-threads-using-addbincontent/33442/5 "2019-04-03T15:17:11Z")

</div>

> [@Axel](#):
>
> The playground for disaster is greater than the size of a bin content storage - it’s a cache line

I can random shuffle the bin numbers to the threads won’t content for a cache line.

> [@Axel](#):
>
> `TThreadedObject`

is very slow.

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [April 3, 2019, 3:29pm UTC](https://root-forum.cern.ch/t/is-it-ok-to-modify-different-histogram-bins-from-different-threads-using-addbincontent/33442/6 "2019-04-03T15:29:03Z")

</div>

`TThreadedObject` is _much_ faster than locking a single histogram 🙂 It’s all relative…

I don’t understand the random shuffle comment - but that’s not needed for it to work! Glad you have a solution!
