Is it thread-safe to share heap between timer callback and thread?2019 Community Moderator ElectionIs the C# static constructor thread safe?What is the difference between a process and a thread?What is meant by “thread-safe” code?What is thread safe or non-thread safe in PHP?Is errno thread-safe?What resources are shared between threads?What is the difference between task and thread?Are static variables shared between threads?Are lists thread-safe?Is the != check thread safe?
What is the significance behind "40 days" that often appears in the Bible?
World War I as a war of liberals against authoritarians?
Turning a hard to access nut?
Writing in a Christian voice
What does Deadpool mean by "left the house in that shirt"?
gerund and noun applications
What exactly term 'companion plants' means?
What can I do if I am asked to learn different programming languages very frequently?
In what cases must I use 了 and in what cases not?
Is honey really a supersaturated solution? Does heating to un-crystalize redissolve it or melt it?
Does the attack bonus from a Masterwork weapon stack with the attack bonus from Masterwork ammunition?
Is there a hypothetical scenario that would make Earth uninhabitable for humans, but not for (the majority of) other animals?
Calculate the frequency of characters in a string
Am I eligible for the Eurail Youth pass? I am 27.5 years old
I got the following comment from a reputed math journal. What does it mean?
Do native speakers use "ultima" and "proxima" frequently in spoken English?
Should I use acronyms in dialogues before telling the readers what it stands for in fiction?
Four married couples attend a party. Each person shakes hands with every other person, except their own spouse, exactly once. How many handshakes?
Comment Box for Substitution Method of Integrals
Describing a chess game in a novel
How to define limit operations in general topological spaces? Are nets able to do this?
Dual Irish/Britsh citizens
If "dar" means "to give", what does "daros" mean?
Help rendering a complicated sum/product formula
Is it thread-safe to share heap between timer callback and thread?
2019 Community Moderator ElectionIs the C# static constructor thread safe?What is the difference between a process and a thread?What is meant by “thread-safe” code?What is thread safe or non-thread safe in PHP?Is errno thread-safe?What resources are shared between threads?What is the difference between task and thread?Are static variables shared between threads?Are lists thread-safe?Is the != check thread safe?
I got 2 producers and 1 consumer utilizing a single fifo. Messages are enqueued using k_fifo_alloc_put as shown below:
#include <zephyr.h>
struct message_t
u32_t info;
;
K_FIFO_DEFINE(fifo);
void thread_func(void *arg1, void *arg2, void *arg3)
ARG_UNUSED(arg1);
ARG_UNUSED(arg2);
ARG_UNUSED(arg3);
while(1)
struct message_t msg =
.info = 1,
;
k_fifo_alloc_put(&fifo, &msg);
k_sleep(K_SECONDS(1));
K_THREAD_STACK_DEFINE(thread1_stack_area, 256);
static struct k_thread thread1_data;
void tick_handler(struct k_timer *timer)
struct message_t msg =
.info = 2,
;
k_thread_system_pool_assign(k_current_get());
k_fifo_alloc_put(&fifo, &msg);
void main(void)
k_thread_system_pool_assign(k_current_get());
k_thread_create(&thread1_data, thread1_stack_area,
K_THREAD_STACK_SIZEOF(thread1_stack_area),
thread_func,
NULL, NULL, NULL,
7, 0, K_NO_WAIT);
struct k_timer timer;
k_timer_init(&timer, tick_handler, NULL);
k_timer_start(&timer, K_SECONDS(1), K_SECONDS(1));
struct message_t *msg;
for(;;)
msg = k_fifo_get(&fifo, K_FOREVER);
if(msg) printk("Received message from fifo: %drn", msg->info);
Both producers are allocating from the same heap. Timer explicitily assigns system heap to itself using k_thread_system_pool_assign(k_current_get()); and the second thread inherits the same heap from main.
Is it safe to do it like this? If not, how should I implement such behavior?
What I really don't understand here is how k_fifo_alloc_put is performing the allocation and how this resource if freed on retrieval as stated in the interface docs:
k_fifo_alloc_put(fifo, data):
This routine adds a data item to @a fifo. There is an implicit
memory allocation from the calling thread's resource pool, which is
automatically freed when the item is removed.
multithreading zephyr-rtos
add a comment |
I got 2 producers and 1 consumer utilizing a single fifo. Messages are enqueued using k_fifo_alloc_put as shown below:
#include <zephyr.h>
struct message_t
u32_t info;
;
K_FIFO_DEFINE(fifo);
void thread_func(void *arg1, void *arg2, void *arg3)
ARG_UNUSED(arg1);
ARG_UNUSED(arg2);
ARG_UNUSED(arg3);
while(1)
struct message_t msg =
.info = 1,
;
k_fifo_alloc_put(&fifo, &msg);
k_sleep(K_SECONDS(1));
K_THREAD_STACK_DEFINE(thread1_stack_area, 256);
static struct k_thread thread1_data;
void tick_handler(struct k_timer *timer)
struct message_t msg =
.info = 2,
;
k_thread_system_pool_assign(k_current_get());
k_fifo_alloc_put(&fifo, &msg);
void main(void)
k_thread_system_pool_assign(k_current_get());
k_thread_create(&thread1_data, thread1_stack_area,
K_THREAD_STACK_SIZEOF(thread1_stack_area),
thread_func,
NULL, NULL, NULL,
7, 0, K_NO_WAIT);
struct k_timer timer;
k_timer_init(&timer, tick_handler, NULL);
k_timer_start(&timer, K_SECONDS(1), K_SECONDS(1));
struct message_t *msg;
for(;;)
msg = k_fifo_get(&fifo, K_FOREVER);
if(msg) printk("Received message from fifo: %drn", msg->info);
Both producers are allocating from the same heap. Timer explicitily assigns system heap to itself using k_thread_system_pool_assign(k_current_get()); and the second thread inherits the same heap from main.
Is it safe to do it like this? If not, how should I implement such behavior?
What I really don't understand here is how k_fifo_alloc_put is performing the allocation and how this resource if freed on retrieval as stated in the interface docs:
k_fifo_alloc_put(fifo, data):
This routine adds a data item to @a fifo. There is an implicit
memory allocation from the calling thread's resource pool, which is
automatically freed when the item is removed.
multithreading zephyr-rtos
add a comment |
I got 2 producers and 1 consumer utilizing a single fifo. Messages are enqueued using k_fifo_alloc_put as shown below:
#include <zephyr.h>
struct message_t
u32_t info;
;
K_FIFO_DEFINE(fifo);
void thread_func(void *arg1, void *arg2, void *arg3)
ARG_UNUSED(arg1);
ARG_UNUSED(arg2);
ARG_UNUSED(arg3);
while(1)
struct message_t msg =
.info = 1,
;
k_fifo_alloc_put(&fifo, &msg);
k_sleep(K_SECONDS(1));
K_THREAD_STACK_DEFINE(thread1_stack_area, 256);
static struct k_thread thread1_data;
void tick_handler(struct k_timer *timer)
struct message_t msg =
.info = 2,
;
k_thread_system_pool_assign(k_current_get());
k_fifo_alloc_put(&fifo, &msg);
void main(void)
k_thread_system_pool_assign(k_current_get());
k_thread_create(&thread1_data, thread1_stack_area,
K_THREAD_STACK_SIZEOF(thread1_stack_area),
thread_func,
NULL, NULL, NULL,
7, 0, K_NO_WAIT);
struct k_timer timer;
k_timer_init(&timer, tick_handler, NULL);
k_timer_start(&timer, K_SECONDS(1), K_SECONDS(1));
struct message_t *msg;
for(;;)
msg = k_fifo_get(&fifo, K_FOREVER);
if(msg) printk("Received message from fifo: %drn", msg->info);
Both producers are allocating from the same heap. Timer explicitily assigns system heap to itself using k_thread_system_pool_assign(k_current_get()); and the second thread inherits the same heap from main.
Is it safe to do it like this? If not, how should I implement such behavior?
What I really don't understand here is how k_fifo_alloc_put is performing the allocation and how this resource if freed on retrieval as stated in the interface docs:
k_fifo_alloc_put(fifo, data):
This routine adds a data item to @a fifo. There is an implicit
memory allocation from the calling thread's resource pool, which is
automatically freed when the item is removed.
multithreading zephyr-rtos
I got 2 producers and 1 consumer utilizing a single fifo. Messages are enqueued using k_fifo_alloc_put as shown below:
#include <zephyr.h>
struct message_t
u32_t info;
;
K_FIFO_DEFINE(fifo);
void thread_func(void *arg1, void *arg2, void *arg3)
ARG_UNUSED(arg1);
ARG_UNUSED(arg2);
ARG_UNUSED(arg3);
while(1)
struct message_t msg =
.info = 1,
;
k_fifo_alloc_put(&fifo, &msg);
k_sleep(K_SECONDS(1));
K_THREAD_STACK_DEFINE(thread1_stack_area, 256);
static struct k_thread thread1_data;
void tick_handler(struct k_timer *timer)
struct message_t msg =
.info = 2,
;
k_thread_system_pool_assign(k_current_get());
k_fifo_alloc_put(&fifo, &msg);
void main(void)
k_thread_system_pool_assign(k_current_get());
k_thread_create(&thread1_data, thread1_stack_area,
K_THREAD_STACK_SIZEOF(thread1_stack_area),
thread_func,
NULL, NULL, NULL,
7, 0, K_NO_WAIT);
struct k_timer timer;
k_timer_init(&timer, tick_handler, NULL);
k_timer_start(&timer, K_SECONDS(1), K_SECONDS(1));
struct message_t *msg;
for(;;)
msg = k_fifo_get(&fifo, K_FOREVER);
if(msg) printk("Received message from fifo: %drn", msg->info);
Both producers are allocating from the same heap. Timer explicitily assigns system heap to itself using k_thread_system_pool_assign(k_current_get()); and the second thread inherits the same heap from main.
Is it safe to do it like this? If not, how should I implement such behavior?
What I really don't understand here is how k_fifo_alloc_put is performing the allocation and how this resource if freed on retrieval as stated in the interface docs:
k_fifo_alloc_put(fifo, data):
This routine adds a data item to @a fifo. There is an implicit
memory allocation from the calling thread's resource pool, which is
automatically freed when the item is removed.
multithreading zephyr-rtos
multithreading zephyr-rtos
asked Mar 6 at 21:47
pbnpbn
671613
671613
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55032691%2fis-it-thread-safe-to-share-heap-between-timer-callback-and-thread%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55032691%2fis-it-thread-safe-to-share-heap-between-timer-callback-and-thread%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown