Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8473527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:22:32+00:00 2026-06-10T17:22:32+00:00

I am implementing FIFO in C. One thread is writing in FIFO and other

  • 0

I am implementing FIFO in C. One thread is writing in FIFO and other is reading from it.

#define BUFFER_LENGTH   1000
struct Frame
{
    char data[1024];
    unsigned int data_len;
    struct Frame* frame;
};

struct Frame * get_from_fifo ()
{
    if (!fifo_length)
    {
        first = last = NULL;
        return NULL;
    }
    struct Frame* frame = first;
    first = first->frame;
    fifo_length--;
    return frame;
}


int add_to_fifo (const char* data, unsigned int frame_size)
{
    if (fifo_length >= BUFFER_LENGTH)
    {
        ast_log(LOG_ERROR, "Buffer full\n");
        return SURESH_ERROR;
    }

    struct Frame* frame = malloc(sizeof (struct Frame));
    frame->data_len = frame_size;
    memcpy(frame->data, data, frame_size);

    if (last)
    {
        last->frame = frame;
        last = frame;
    }

    if (!first)
    {
        first = last = frame;
    }
    fifo_length++;
    return SURESH_SUCCESS;
}

how can I prevent functions *add_to_fifo* and *get_from_fifo* to be called at the same time by different threads. i.e. *get_from_fifo* should only be called when the other thread is not executing *add_to_fifo* and vice verca.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T17:22:34+00:00Added an answer on June 10, 2026 at 5:22 pm

    As you are implementing FIFO stack the only really concurrent operation you have is changing the stack size (fifo_length).
    You are adding entries to the tail of the stack and removing entries from the head of the stack so these two operation will never interfere with each other. So the only part you will need to worry about is changing the stack size (fifo_length), I would put it into separate function synchronised by mutex or flag (as mentioned by “Joey” above) and call it from both add_to_fifo() and get_from_fifo() functions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While implementing a FIFO I have used the following structure: struct Node { T
Im implementing a reminder app.There are two reminders (one for everyday and other for
When implementing a hash table using a good hash function (one where the probability
When implementing the Strategy Pattern, where does one put the code that determines which
When implementing a FIFO like Queues, my instructor always advise us to represent it
Implementing a low pass FIR filter, when should one use FFT and IFFT instead
When implementing composite primary keys in Hibernate or other ORMs there are up to
When implementing move constructors and move assignment operators, one often writes code like this:
Implementing Ajax requests in my rails 3 app has been one of the most
Implementing a Thread by providing a new class that extends Thread and overriding its

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.