Page 1 of 2

send data between 2 threads

Posted: Mon Feb 04, 2013 8:52 am
by nupagachi85
Dear all,

Now I have 1 data like that:


typedef struct _MESSAGE
{
BYTE Type;
BYTE MsgId;
BYTE MsgIndex;
BYTE MsgSize;
} MESSAGE;

MESSAGE data;

How I send and recieve between 2 threads?

Thanks and best regards

Re: send data between 2 threads

Posted: Mon Feb 04, 2013 9:00 am
by Tectu
A common way to do this is using a mailbox / message queue. You might want to read this thread from the other day.


~ Tectu

Re: send data between 2 threads

Posted: Mon Feb 04, 2013 9:10 am
by nupagachi85
Hi,

But when I using maibox : msg_t chMBPost(Mailbox *mbp, msg_t msg, systime_t time);
It just support send msg_t = int32_t, but I want send struct, how can I do?

Best regards

Re: send data between 2 threads

Posted: Mon Feb 04, 2013 9:11 am
by Giovanni
Hi,

In ChibiOS there are two mechanisms usable for this kind of tasks:
1) Mailboxes as suggested by Tectu.
2) Messages.

The Mailboxes are mono-directional queues of messages, best suited when you just want to send a message and do not wait for reception.
Messages are synchronous instead, you send a message and then the thread wait until the message is processed and an answer returned to the sender.

Giovanni

Re: send data between 2 threads

Posted: Mon Feb 04, 2013 9:14 am
by Giovanni
nupagachi85 wrote:Hi,

But when I using maibox : msg_t chMBPost(Mailbox *mbp, msg_t msg, systime_t time);
It just support send msg_t = int32_t, but I want send struct, how can I do?

Best regards


I suggest you use messages, this way you can allocate your struct into an automatic variable of the sender. In case you want to use mailboxes you could use two mailboxes:

1) Mailbox 1 is used to store pointers to unused message structures.
2) Mailbox 2 is used to send messages.

The sender takes a structure from #1, fills it and then sends it on #2. The receiver takes the message from #2, processes it and then returns it on #1. You need to pre-fill #1 on initialization.

Giovanni

Re: send data between 2 threads

Posted: Mon Feb 04, 2013 9:16 am
by mabl
nupagachi85 wrote:Hi,

But when I using maibox : msg_t chMBPost(Mailbox *mbp, msg_t msg, systime_t time);
It just support send msg_t = int32_t, but I want send struct, how can I do?

Best regards


Hi nupagachi85,
msg_t is guaranteed to be large enough to contain a pointer. So what you typically do for mailboxes, is to use a memory pool for your struct, and send only a pointer via messages. For synchronous messages, you do not even need the pool.

Re: send data between 2 threads

Posted: Mon Feb 04, 2013 9:56 am
by nupagachi85
thanks for reply.
My app have a data like that. It is a struct. I don't find any example show me do that. I just a Newbie with chibios.
Do you help me a short code how to send and recieve it?

Thanks and best regards

Re: send data between 2 threads

Posted: Mon Feb 04, 2013 10:05 am
by Tectu
Did you take a look at the thread I linked? There is a discussion and another link about a module using this technique.


~ Tectu

Re: send data between 2 threads

Posted: Mon Feb 04, 2013 10:11 am
by Giovanni
There is a thread in the development regarding a FatFs wrapper that exchanges messages, you could use that for example.

Giovanni

Re: send data between 2 threads

Posted: Mon Feb 04, 2013 10:13 am
by Tectu
@Giovanni: I just got in mind that both, the FatFSWrapper and ChibiOS/GFX do use one-way message queues.


~ Tectu