Page 1 of 1

pass msg back to main

Posted: Sat Nov 15, 2014 10:11 am
by Tabulous
how can one pass a msg back to the main thread.

i.e. how do i get the pointer to main thread so i can post this to the thread doing the work and then wait for the response ?

Code: Select all


int main(void)
{

    dongmsg.key = &key;
    dongmsg.tp = *******calling thread pointer********;
    dongmsg.id = DONG_KEY_REQUEST;
    dongmsg.imei = md->manu.imei;

    chMBPost( mbox,
              ( msg_t )&dongmsg,
              TIME_INFINITE );

    p = chMsgWait();

    if( p != NULL )
    {
    }
}

Re: pass msg back to main

Posted: Sat Nov 15, 2014 11:45 am
by Giovanni
Hi,

You are mixing mailboxes and synchronous messages.

Code: Select all

static THD_FUNCTION(ServerThread, arg) {
 
  while (true) {
    /* Waiting for a queued message then retrieving it.*/
    thread_t *tp = chMsgWait();
    msg_t msg = chMsgGet(tp);
 
    /* Processing the message.*/
    ...;
 
    /* Sending back an acknowledge.*/
    chMsgRelease(tp, MSG_OK);
  }
}


You send messages to the server using chMsgSend(), it returns the answer, MSG_OK in the example.

Giovanni

Re: pass msg back to main

Posted: Sat Nov 15, 2014 3:29 pm
by Tabulous
forget about my code it is misleading, all i want is how do i get a pointer to the main thread structure ?

normally when you create a thread you can assign it like this

pThread = chThdCreateStatic( ETICKETWA, sizeof(ETICKETWA),
NORMALPRIO, ThreadEticket,
NULL );

but what about int main(void) ? or does this not have a thread structure ?

Re: pass msg back to main

Posted: Sat Nov 15, 2014 4:00 pm
by Giovanni
You can get the pointer to the current thread from the main (chThdSelf() in 2.6, chThdGetSelfX() in 3.0) and then store it somewhere for other threads to use.

Giovanni