Threads synchronization by events

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

Moderators: RoccoMarco, lbednarz, utzig, tfAteba, barthess

plyatov
Posts: 25
Joined: Tue Feb 25, 2014 10:24 am
Been thanked: 2 times

Threads synchronization by events

Postby plyatov » Thu Apr 03, 2014 4:50 pm

Hello dear all!

Please help me, how to make a milti-threaded system, where two of threads should be synchronized, while other threads should be not disturbed.

I see 2 possible solutions (maybe I'm wrong):

1) Thread1 makes data filtering and send Event1 or Event2 (depends from data values) to Thread2. Where Thread2 sleeps until Event1 or Event2 and then executes something corresponding to Event number.

2) Thread1 parses data and send event and Message1 or Message2 (depends from data) to Thread2. Where Thread2 sleeps until event and then executes something corresponding to Message number.

Here is code sample for better understanding:

Code: Select all

static msg_t Thread1(void *arg)
{
   (void)arg;
   int data

   chRegSetThreadName("data_filter");

   while (TRUE) {
      data = get_data();
      if (data == TYPE1) {
         // Send Event1 only to Thread2.
         // Or send Event and Message1 to Thread2?
      } else if (data == TYPE2) {
         // Send Event2 only to Thread2.
         // Or send Event and Message2 only to Thread2?
      }
   }

   return 0;
}

static msg_t Thread2(void *arg)
{
   (void)arg;

   chRegSetThreadName("relay1_set");

   while (TRUE) {
      // Wait for Event1/Event2, which is specific only to this thread.
      // Or alternatively wait for specific Event with one of messages.

      if (Event1) // Message1 instead?
         relay_set();
      else if (Event2) // Message2 instead?
         relay_clear();
   }

   return 0;
}

Please edit this code sample to show how exactly events and/or messages should be registered, sent and dispatched.

Best wishes.
--
Igor

User avatar
Giovanni
Site Admin
Posts: 14457
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: Threads synchronization by events

Postby Giovanni » Thu Apr 03, 2014 5:05 pm

A question, why not use messages?

You may create a message server thread that processes the messages from the other thread. While the server is processing a message the sender is sleeping and continues to sleep until an answer is given (messages are always 2-ways) so mutual exclusion is ensured.

example:

Code: Select all

static msg_t thread1(void *p) {
  Thread *tp;
  msg_t msg;

  (void)p;
  do {
    tp = chMsgWait();
    msg = chMsgGet(tp);
    /* process message here */

    /* answering with the same message */
    chMsgRelease(tp, msg);
  } while (msg);
  return 0;
}


Giovanni

plyatov
Posts: 25
Joined: Tue Feb 25, 2014 10:24 am
Been thanked: 2 times

Re: Threads synchronization by events

Postby plyatov » Fri Apr 04, 2014 10:42 am

Dear Giovanni,

Giovanni wrote:A question, why not use messages?

You may create a message server thread that processes the messages from the other thread. While the server is processing a message the sender is sleeping and continues to sleep until an answer is given (messages are always 2-ways) so mutual exclusion is ensured.
...
Giovanni


The Thread1 should execute independently and with different priority from Thread2. Waiting for answer from Thread2 is harmful for my algorythm. So, I think Synchronous Messages is not applicable here.

Maybe topic name is incorrect regarding "synchronization" word...
Thread1 should send some kind of signals to Thread2 to indicate detection of specific data values at digitizer's input.

Can you please check example below and say does it correct or not?

Code: Select all

#define MAILBOX_SIZE      16
#define MSG_1         1
#define MSG_2         2

static Mailbox mb;
static msg_t b[MAILBOX_SIZE];

static msg_t Thread1(void *arg)
{
   (void)arg;
   int data

   chRegSetThreadName("data_filter");

   while (TRUE) {
      data = get_data();
      if (data == TYPE1) {
         msg = MSG_1;
         msg = chMBPost(mb, msg, TIME_IMMEDIATE);
         if (msg != RDY_OK)
            error_flag = TRUE;
      } else if (data == TYPE2) {
         msg = MSG_2;
         msg = chMBPost(mb, msg, TIME_IMMEDIATE);
         if (msg != RDY_OK)
            error_flag = TRUE;
      }
   }

   return 0;
}

static msg_t Thread2(void *arg)
{
   (void)arg;

   chRegSetThreadName("relay1_set");

   while (TRUE) {
      chMBFetch(mb, &msg, TIME_INFINITE);
      if (msg == MSG_1)
         relay_set();
      else if (msg == MSG_2)
         relay_clear();
   }

   return 0;
}

main()
{
   ...
   
   chMBInit(mb, b, MAILBOX_SIZE);
   chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO+2, Thread1, NULL);
   chThdCreateStatic(waThread2, sizeof(waThread2), NORMALPRIO+1, Thread2, NULL);
   ...
}


Best wishes.
--
Igor

User avatar
Giovanni
Site Admin
Posts: 14457
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: Threads synchronization by events

Postby Giovanni » Fri Apr 04, 2014 10:49 am

It looks OK, you got correctly how mailboxes are supposed to work.

While developing I recommend enabling debug options in chconf.h.

Giovanni

plyatov
Posts: 25
Joined: Tue Feb 25, 2014 10:24 am
Been thanked: 2 times

Re: Threads synchronization by events

Postby plyatov » Sat Apr 12, 2014 6:47 pm

Giovanni wrote:It looks OK, you got correctly how mailboxes are supposed to work.

While developing I recommend enabling debug options in chconf.h.

Giovanni


Dear Giovanny, thanks you for support!


Return to “General Support”

Who is online

Users browsing this forum: No registered users and 58 guests