Page 1 of 1

Threads synchronization by events

Posted: Thu Apr 03, 2014 4:50 pm
by plyatov
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

Re: Threads synchronization by events

Posted: Thu Apr 03, 2014 5:05 pm
by Giovanni
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

Re: Threads synchronization by events

Posted: Fri Apr 04, 2014 10:42 am
by plyatov
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

Re: Threads synchronization by events

Posted: Fri Apr 04, 2014 10:49 am
by Giovanni
It looks OK, you got correctly how mailboxes are supposed to work.

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

Giovanni

Re: Threads synchronization by events

Posted: Sat Apr 12, 2014 6:47 pm
by plyatov
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!