send data between 2 threads

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

Moderators: utzig, lbednarz, tfAteba, barthess, RoccoMarco

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

send data between 2 threads

Postby nupagachi85 » Mon Feb 04, 2013 8:52 am

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: 1226
Joined: Thu May 10, 2012 9:50 am
Location: Switzerland
Contact:

Re: send data between 2 threads

Postby Tectu » Mon Feb 04, 2013 9:00 am

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

Postby nupagachi85 » Mon Feb 04, 2013 9:10 am

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: 13087
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 759 times
Been thanked: 638 times
Contact:

Re: send data between 2 threads

Postby Giovanni » Mon Feb 04, 2013 9:11 am

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: 13087
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 759 times
Been thanked: 638 times
Contact:

Re: send data between 2 threads

Postby Giovanni » Mon Feb 04, 2013 9:14 am

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
Location: Karlsruhe, Germany
Been thanked: 1 time
Contact:

Re: send data between 2 threads

Postby mabl » Mon Feb 04, 2013 9:16 am

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

Postby nupagachi85 » Mon Feb 04, 2013 9:56 am

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: 1226
Joined: Thu May 10, 2012 9:50 am
Location: Switzerland
Contact:

Re: send data between 2 threads

Postby Tectu » Mon Feb 04, 2013 10:05 am

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: 13087
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 759 times
Been thanked: 638 times
Contact:

Re: send data between 2 threads

Postby Giovanni » Mon Feb 04, 2013 10:11 am

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: 1226
Joined: Thu May 10, 2012 9:50 am
Location: Switzerland
Contact:

Re: send data between 2 threads

Postby Tectu » Mon Feb 04, 2013 10:13 am

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


~ Tectu


Return to “General Support”

Who is online

Users browsing this forum: No registered users and 7 guests