# How to define STAN\_THREADS?

**URL:** <https://discourse.mc-stan.org/t/how-to-define-stan-threads/5216>\
**Category:** Developers\
**Created:** [August 14, 2018, 6:37pm UTC](https://discourse.mc-stan.org/t/how-to-define-stan-threads/5216 "2018-08-14T18:37:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![stanuser](https://avatars.discourse-cdn.com/v4/letter/s/90db22/32.png) [@stanuser](https://discourse.mc-stan.org/u/stanuser)\
**Post date:** [August 14, 2018, 6:37pm UTC](https://discourse.mc-stan.org/t/how-to-define-stan-threads/5216/1 "2018-08-14T18:37:32Z")

</div>

I’m running sampling with pure C++ and I’m having issues when trying to run sampling in parallel. I read another post there it says you need to define STAN\_THREADS. I’m not sure how or where to do that. Everything works fine with one chain. When I use two chains, it gives various errors (i.e. malloc: \*\*\* error for object 0x7f8a04f00738: pointer being freed was not allocated). Here’s what I’m trying to do (some code omitted for clarity).

```
std::thread *threadArray = new std::thread[chains];
for (int i = 0; i < chains; i++) {
        threadArray[i] = std::thread([i]() {
             ...
             linear_model *model = new linear_model(varContext, 123456789, &amp;pstream);
             ...
             int returnCode = stan::services::sample::hmc_nuts_diag_e_adapt<linear_model>(
                                *model, context, 123456789, i, 2,
                                500, 500, 1,
                                false, 100,
                                1, 0, 10,
                                0.8, 0.05, 0.75, 10,
                                75, 50, 25,
                                interrupt, logger, init,
                                *parameter, diagnostic);
        ...
        });

}

for (int i = 0; i < chains; ++i) {
    threadArray[i].join();
}
```

---

<div class="post-metadata">

**Author:** ![wds15](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/wds15/32/908_2.png) [@wds15](https://discourse.mc-stan.org/u/wds15)\
**Post date:** [August 14, 2018, 6:40pm UTC](https://discourse.mc-stan.org/t/how-to-define-stan-threads/5216/2 "2018-08-14T18:40:38Z")

</div>

You need to add -DSTAN\_THREADS to the compiler flags… or use a define for the same effect in your code.

---

<div class="post-metadata">

**Author:** ![stanuser](https://avatars.discourse-cdn.com/v4/letter/s/90db22/32.png) [@stanuser](https://discourse.mc-stan.org/u/stanuser)\
**Post date:** [August 14, 2018, 8:22pm UTC](https://discourse.mc-stan.org/t/how-to-define-stan-threads/5216/3 "2018-08-14T20:22:50Z")

</div>

Thanks wds15. Looks like that was it. I had some other issue.

---

<div class="post-metadata">

**Author:** ![Nesta\_Bentum](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/nesta_bentum/32/5541_2.png) [@Nesta\_Bentum](https://discourse.mc-stan.org/u/Nesta_Bentum)\
**Post date:** [July 31, 2019, 10:52am UTC](https://discourse.mc-stan.org/t/how-to-define-stan-threads/5216/4 "2019-07-31T10:52:36Z")

</div>

Hey @stanuser! How exactly did you add -DSTAN\_THREADS to the compiler flags? I’ve tried multiple times and it just doesn’t work for me…

---

<div class="post-metadata">

**Author:** ![syclik](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/syclik/32/6_2.png) [@syclik](https://discourse.mc-stan.org/u/syclik)\
**Post date:** [August 1, 2019, 8:16am UTC](https://discourse.mc-stan.org/t/how-to-define-stan-threads/5216/5 "2019-08-01T08:16:10Z")

</div>

It’ll depend on what interface you’re using… are you on CmdStan?

---

<div class="post-metadata">

**Author:** ![Nesta\_Bentum](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/nesta_bentum/32/5541_2.png) [@Nesta\_Bentum](https://discourse.mc-stan.org/u/Nesta_Bentum)\
**Post date:** [August 1, 2019, 8:50am UTC](https://discourse.mc-stan.org/t/how-to-define-stan-threads/5216/6 "2019-08-01T08:50:17Z")

</div>

I’m using the Stan Math Library in C++ via the `#include <stan/math.hpp>` include and I need the auto differentiation to be thread-safe.

---

<div class="post-metadata">

**Author:** ![Ziaeemehr](https://avatars.discourse-cdn.com/v4/letter/z/a698b9/32.png) [@Ziaeemehr](https://discourse.mc-stan.org/u/Ziaeemehr)\
**Post date:** [November 21, 2021, 8:08pm UTC](https://discourse.mc-stan.org/t/how-to-define-stan-threads/5216/7 "2021-11-21T20:08:24Z")

</div>

How do you add it in cmdstan?

---

<div class="post-metadata">

**Author:** ![Ziaeemehr](https://avatars.discourse-cdn.com/v4/letter/z/a698b9/32.png) [@Ziaeemehr](https://discourse.mc-stan.org/u/Ziaeemehr)\
**Post date:** [November 21, 2021, 8:19pm UTC](https://discourse.mc-stan.org/t/how-to-define-stan-threads/5216/8 "2021-11-21T20:19:12Z")

</div>

I found it:

```
make STAN_THREADS=TRUE model_name 
./model_name sample num_threads=4  

```
