# Csnd:: vs std::

**URL:** <https://forum.csound.com/t/csnd-vs-std/1577>\
**Category:** Developers\
**Created:** [March 12, 2024, 7:26am UTC](https://forum.csound.com/t/csnd-vs-std/1577 "2024-03-12T07:26:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lovre](https://forum.csound.com/letter_avatar_proxy/v4/letter/l/bbce88/32.png) [@Lovre](https://forum.csound.com/u/Lovre)\
**Post date:** [March 12, 2024, 7:26am UTC](https://forum.csound.com/t/csnd-vs-std/1577/1 "2024-03-12T07:26:09Z")

</div>

Hello again dear Csounders!

When developing a csound opcode using c++ framework it seems that std::thread isn’t that welcome ☹ . Why is that?

Are there any other limitations that one should have in mind? E.g. regarding memory management (smart pointers, containers…) or something else like only c++11 should be used and so on…

---

<div class="post-metadata">

**Author:** ![rory](https://forum.csound.com/user_avatar/forum.csound.com/rory/32/7_2.png) [@rory](https://forum.csound.com/u/rory)\
**Post date:** [March 12, 2024, 1:36pm UTC](https://forum.csound.com/t/csnd-vs-std/1577/2 "2024-03-12T13:36:51Z")

</div>

std::thread should work Ok. I’m not sure about containers, I don’t think I’ve ever used a smart pointer in any of my cpof opcodes. Note there is a `deinit()` function that you can register that will be called when the opcode is finished. You can tidy memory there if you need to.

---

<div class="post-metadata">

**Author:** ![Lovre](https://forum.csound.com/letter_avatar_proxy/v4/letter/l/bbce88/32.png) [@Lovre](https://forum.csound.com/u/Lovre)\
**Post date:** [March 12, 2024, 2:30pm UTC](https://forum.csound.com/t/csnd-vs-std/1577/3 "2024-03-12T14:30:00Z")

</div>

Hi @rory! And thanks (again) for jumping in 🍻

when I want to use std::thread there is a conflict with a macro named \_CR I can make workaround like this

```csound
#ifdef _CR
#undef _CR
#include <thread>
#define _CR (0x0020)
#else
#include <thread>
#endif

```

but I don’t know if this is safe?

And if I make something like

```csound
void print_hello() {
    std::cout << "Hello from a worker thread!" << std::endl;
}

struct StdThreadTest : csnd::Plugin<0, 0> {
    std::thread t;
    int init() {
        t = std::thread(print_hello);
        t.join();
        return OK;
    }
};

#include <modload.h>

void csnd::on_load(Csound *csound) {
    csnd::plugin<StdThreadTest>(csound, "std_threading", "", "", csnd::thread::i);
}

```

opcode doesn’t run in csound (I get: “Unexpected untyped word std\_threading when expecting a variable Parsing failed due to invalid input!”). If I don’t create and run a thread opcode runs fine.

Am I doing something wrong here?

---

<div class="post-metadata">

**Author:** ![Lovre](https://forum.csound.com/letter_avatar_proxy/v4/letter/l/bbce88/32.png) [@Lovre](https://forum.csound.com/u/Lovre)\
**Post date:** [March 12, 2024, 2:40pm UTC](https://forum.csound.com/t/csnd-vs-std/1577/4 "2024-03-12T14:40:37Z")

</div>

And **pthread** seems to work fine:

```csound
void* print_hello(void* arg) {
    std::cout << "Hello from worker thread!" << std::endl;
    pthread_exit(NULL);
}

struct PthrdThreadTest : csnd::Plugin<0, 0> {
    pthread_t simple_thread;

    int init() {
        int rc = pthread_create(&simple_thread, NULL, print_hello, NULL);
        std::cout << "Hello from the main thread!" << std::endl;
        return OK;
    }
};

#include <modload.h>

void csnd::on_load(Csound *csound) {
    csnd::plugin<PthrdThreadTest>(csound, "pthrd_threading", "", "", csnd::thread::i);
}

```

---

<div class="post-metadata">

**Author:** ![rory](https://forum.csound.com/user_avatar/forum.csound.com/rory/32/7_2.png) [@rory](https://forum.csound.com/u/rory)\
**Post date:** [March 12, 2024, 3:31pm UTC](https://forum.csound.com/t/csnd-vs-std/1577/5 "2024-03-12T15:31:27Z")

</div>

> [@Lovre](#):
>
> but I don’t know if this is safe?

That’s Ok, there is a cinflict betwee the “\_CR” definition in the standard C++ library and in Csound, although it’s only an issue on Windows from what I can tell. If you undef \_CR before including any Csound headers it should be fine. With regards to the std::thread issue, I just checked an opcode I wrote that uses it, and this is how my include section looks:

#undef \_CR  
#include \<plugin.h\>  
#include

I don’t think you need to worry about redefining \_CR as it will be redefined elsewhere. I guess if pthread works ago, you might as well use that? But you have to link to the pthread library which is a little annoying.

---

<div class="post-metadata">

**Author:** ![Lovre](https://forum.csound.com/letter_avatar_proxy/v4/letter/l/bbce88/32.png) [@Lovre](https://forum.csound.com/u/Lovre)\
**Post date:** [March 13, 2024, 6:47am UTC](https://forum.csound.com/t/csnd-vs-std/1577/6 "2024-03-13T06:47:40Z")

</div>

Hi @rory !

There are few sentences in Victor’s paper to this topic (that I’ve overseen 🥴 ) :

```csound

4.3. Memory Allocation

Csound provides its own managed heap for dynamic memory allocation. .... 
It is not advisable for developers to employ any other memory allocation methods. ....

```

Well that couldn’t be clearer. So no std::vectors but csnd::AuxMem and csnd::Vector

```csound

6. Multithreading Support

The Csound API includes an interface for multithreading, which is implemented via pthreads [28] on POSIX systems, or other native threading libraries in non-POSIX platforms.
To allow opcodes an object-oriented access to this C interface, CPOF provides the Thread pure virtual class. ....

```

So no std::thread but csnd::Thread (or pthread)…

" I guess if pthread works ago, you might as well use that?" → Yes, it’s just that std::jthread is much more convenient but csnd::Thread and pthread will suffice 🍻
