# Printf thread safe

**URL:** <https://forum.amebaiot.com/t/printf-thread-safe/1839>\
**Category:** SDK\
**Created:** [May 5, 2023, 12:26pm UTC](https://forum.amebaiot.com/t/printf-thread-safe/1839 "2023-05-05T12:26:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tuxford](https://avatars.discourse-cdn.com/v4/letter/t/7feea3/32.png) [@Tuxford](https://forum.amebaiot.com/u/Tuxford)\
**Post date:** [May 5, 2023, 12:26pm UTC](https://forum.amebaiot.com/t/printf-thread-safe/1839/1 "2023-05-05T12:26:06Z")

</div>

Hi All,  
In fact \_rtl\_printf is not thread safe. But I would like to have linear output of logs without overlays like this:

```auto
Wifi::connect BS

Init:Wifi::thread:thread

 0

Wifi::th 2

start read 1 3

Serial::thserial reade
WIFI is noread goto war thread.

t runningitWebServer s result 0

Start

eriDir 0:/ opeal::thread ned

rtl.cfgstart sessi

rtl.cfg 0 on request 109

mkdir.hwait 0

```

I fired a little int \_rtl\_printf(const char \*fmt, … with adding next cide:

```auto
    if (mutex == NULL) {
        mutex = xSemaphoreCreateMutex();
    }
    xSemaphoreTake(mutex, portMAX_DELAY);

    if (ConfigDebugBuffer == 1 && ConfigDebugBufferGet != NULL) {
        buf = (log_buffer_t *)ConfigDebugBufferGet(fmt);
    }

    if (buf != NULL) {
        int rs = DiagVSprintf(buf->buffer, fmt, ((const int *)&fmt) + 1);
        xSemaphoreGive(mutex);
        return rs;
    } else {
        ret = DiagVSprintf(NULL, fmt, ((const int *)&fmt) + 1);
        xSemaphoreGive(mutex);

        return ret;
    }
}
```

But sometimes overlays still appear. Where else synchronization is needed?

---

<div class="post-metadata">

**Author:** ![dakamaster](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.amebaiot.com/dakamaster/32/1744_2.png) [@dakamaster](https://forum.amebaiot.com/u/dakamaster)\
**Post date:** [May 9, 2023, 3:33am UTC](https://forum.amebaiot.com/t/printf-thread-safe/1839/2 "2023-05-09T03:33:14Z")

</div>

Dear @Tuxford ,

Since Ameba D Standard SDK (ambiot/ambd\_sdk) has Wi-Fi and main 2 threads.

Would you like to try your code in Ameba D Arduino SDK (ambiot/ambd\_arduino) single-thread environment? Meanwhile, I will try to reproduce your bug in standard sdk.

Thank you
