<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>FIFO and Dereferencing struct</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/111724/fifo-and-dereferencing-struct</link><description>I can&amp;#39;t seem to get the FIFO to work and I think it is because I&amp;#39;m not dereferencing the consumer thread properly. I have reviewed the nRF Connect SDK Intermediate, data passing and exercise2 FIFO already. 
 Within the data_item_t struct I am adding another</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Sat, 01 Jun 2024 12:26:50 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/111724/fifo-and-dereferencing-struct" /><item><title>RE: FIFO and Dereferencing struct</title><link>https://devzone.nordicsemi.com/thread/486977?ContentTypeID=1</link><pubDate>Sat, 01 Jun 2024 12:26:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c2650f30-a1ce-462a-9e6f-1de78372ec1d</guid><dc:creator>mej7000</dc:creator><description>&lt;p&gt;Thanks! I probably copied the consumer print from my serial window out of order.&lt;br /&gt;&lt;br /&gt;I made progress and got the output I expected. And, I get both the queue has member and queue is empty messages. Thanks for your reply!&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;void producer_thread(Device_t *_mygate)
{
    /* create data item to send */
    struct data_item_t *buf = k_malloc(sizeof(struct data_item_t));
    if (buf == NULL){
        LOG_INF(&amp;quot;Unable to locate memory from the heap&amp;quot;);
        return ;
    }

    /* Populate the data item. This is usually done using memcpy() */
    memcpy(&amp;amp;buf-&amp;gt;MYGATE, _mygate, sizeof(*_mygate));

    LOG_INF(&amp;quot;dcheck: producer thread: cmd: %d uuid: %s d0: %d&amp;quot;, buf-&amp;gt;MYGATE.cmd, buf-&amp;gt;MYGATE.uuid, buf-&amp;gt;MYGATE.d0);

    /* send data to consumers */
    int err = k_fifo_alloc_put(&amp;amp;my_fifo, buf);
    if(err){
        LOG_ERR(&amp;quot;ERROR, fifo alloc put failed: %d&amp;quot;, err);
    }

    // k_free(buf); data access violation
}

void consumer_thread()
{
    struct data_item_t  *rx_data;

    if(k_fifo_is_empty(&amp;amp;my_fifo)){
        LOG_ERR(&amp;quot;FIFO1 Queue is Empty&amp;quot;);
    }
    else{
        LOG_DBG(&amp;quot;FIFO1 Queue has member&amp;quot;);
    }
    
    rx_data = k_fifo_get(&amp;amp;my_fifo, K_NO_WAIT);

    /* process FIFO data item */
    Device_t temp = rx_data-&amp;gt;MYGATE;

    LOG_INF(&amp;quot;dcheck: consumer thread: cmd: %d uuid: %s d0: %d&amp;quot;, temp.cmd, temp.uuid, rx_data-&amp;gt;MYGATE.d0);

    k_free(rx_data);

    /*
        Returns Non-zero if the FIFO queue is empty.
        Returns 0 if data is available.
    */

    if(k_fifo_is_empty(&amp;amp;my_fifo)){
        LOG_ERR(&amp;quot;FIFO2 Queue is Empty\n&amp;quot;);
        k_sleep(K_MSEC(100));
        return;
    }
}&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: FIFO and Dereferencing struct</title><link>https://devzone.nordicsemi.com/thread/486974?ContentTypeID=1</link><pubDate>Sat, 01 Jun 2024 06:58:33 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:083c4823-f060-4956-8c55-a38634807219</guid><dc:creator>Turbo J</dc:creator><description>&lt;p&gt;The if() statement around the k_fifo_empty() call has its logic inversed. Check the rx_data pointer directly, it should be NULL with your code.&lt;/p&gt;
&lt;p&gt;The other clue would be that the consumer prints out &lt;em&gt;before&lt;/em&gt; the producer, which is &lt;strong&gt;obviously&lt;/strong&gt; wrong. Look at the time stamps in logs above.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>