send data between 2 threads

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

nupagachi85
Posts: 8
Joined: Tue Sep 25, 2012 4:58 am

send data between 2 threads

Post 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
User avatar
Tectu
Posts: 1225
Joined: Thu May 10, 2012 9:50 am

Re: send data between 2 threads

Post 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
nupagachi85
Posts: 8
Joined: Tue Sep 25, 2012 4:58 am

Re: send data between 2 threads

Post 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
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: send data between 2 threads

Post 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
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: send data between 2 threads

Post 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
mabl
Posts: 417
Joined: Tue Dec 21, 2010 10:19 am
Been thanked: 1 time

Re: send data between 2 threads

Post 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.
nupagachi85
Posts: 8
Joined: Tue Sep 25, 2012 4:58 am

Re: send data between 2 threads

Post 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
User avatar
Tectu
Posts: 1225
Joined: Thu May 10, 2012 9:50 am

Re: send data between 2 threads

Post 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
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: send data between 2 threads

Post by Giovanni »

There is a thread in the development regarding a FatFs wrapper that exchanges messages, you could use that for example.

Giovanni
User avatar
Tectu
Posts: 1225
Joined: Thu May 10, 2012 9:50 am

Re: send data between 2 threads

Post by Tectu »

@Giovanni: I just got in mind that both, the FatFSWrapper and ChibiOS/GFX do use one-way message queues.


~ Tectu
Post Reply