Skip to content

Commit

Permalink
Add problematic example when OS feature is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
MaJerle committed Aug 30, 2020
1 parent 7cc8242 commit 48b656d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
20 changes: 20 additions & 0 deletions docs/examples_src/example_multi_thread_corrupted_text.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "lwprintf/lwprintf.h"

/* Assuming LwPRINTF has been initialized before */

void
task_1(void* arg) {
lwprintf_printf("Hello world\r\n");
}

void
task_2(void* arg) {
lwprintf_printf("This is Task 2\r\n");
}

/*
* If thread safety is not enabled,
* running above example may print:
*
* "Hello This is Task 2\r\nworld\r\n"
*/
7 changes: 6 additions & 1 deletion docs/user-manual/thread-safety.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ This is due to the fact that direct printing functions use same output function
to print single character. When called from multiple threads, one thread
may preempt another, causing strange output string.

.. literalinclude:: ../examples_src/example_multi_thread_corrupted_text.c
:language: c
:linenos:
:caption: Multiple threads printing at the same time without thread-safety enabled

LwPRINTF therefore comes with a solution that introduces mutexes to lock print functions
when in use from within single thread context.

.. tip::
.. note::
If application does not have any issues concerning mixed output,
it is safe to disable OS support in OS environment.
This will not have any negative effect on performance or memory corruption.
Expand Down

0 comments on commit 48b656d

Please sign in to comment.