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

  • SEARCH
  • Home
  • 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 7998349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:11:40+00:00 2026-06-04T15:11:40+00:00

I tried to write a program that can have mutiple-threaded reads and writes. It

  • 0

I tried to write a program that can have mutiple-threaded reads and writes. It works fine with one read and one write, but it ended in deadlock when I use two reads and one write.

Can anyone help to check?

const int BUF_SIZE = 10;
const int ITERS = 100;

boost::mutex io_mutex;

class buffer
{
public:

    typedef boost::mutex::scoped_lock scoped_lock;

    buffer() : p(0), c(0), full(0)
    {}

    void put(int m)
    {
        scoped_lock lock(mutex);
        if (full == BUF_SIZE)
        {
            {
                boost::mutex::scoped_lock lock(io_mutex);
                std::cout << "Buffer is full. Waiting..." << std::endl;
            }
            while (full == BUF_SIZE)
                cond.wait(lock);
        }

        buf[p] = m;

        p = (p+1) % BUF_SIZE;

        ++full;

        cond.notify_all();
    }

    int get()
    {
        scoped_lock lk(mutex);

        if (full == 0)
        {
            {
                boost::mutex::scoped_lock lock(io_mutex);
                std::cout << "Buffer is empty. Waiting..." << std::endl;
            }

            while (full == 0)
                cond.wait(lk);
        }

        int i = buf[c];

        c = (c+1) % BUF_SIZE;

        --full;

        cond.notify_one();

        return i;
    }

private:
    boost::mutex mutex;

    boost::condition cond;

    unsigned int p, c, full;

    int buf[BUF_SIZE];
};

buffer buf;

void writer()
{
    for (int n = 0; n < ITERS; ++n)
    {
        {
            boost::mutex::scoped_lock lock(io_mutex);
            std::cout << "sending: " << n << std::endl;
        }
        buf.put(n);
    }
}

void reader()
{
    for (int x = 0; x < ITERS; ++x)
    {
        int n = buf.get();
        {
            boost::mutex::scoped_lock lock(io_mutex);
            std::cout << "reader1: received: " << n <<  std::endl;
        }
    }
}

int main(int argc, char* argv[])
{
    boost::thread thrd1(&reader);
    boost::thread thrd2(&writer);
    boost::thread thrd3(&reader);

    std::string str;

    thrd1.join();
    thrd2.join();
    thrd3.join();

    std::getline(std::cin,str);
}
  • 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-04T15:11:41+00:00Added an answer on June 4, 2026 at 3:11 pm

    You possibly lost the forest for the trees a bit here. Your code writes ITERS values but tries to read 2 * ITERS values since you have two readers. That can’t work.

    You’ll at least have to write 2 * ITERS values to get the program to complete.

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

Sidebar

Related Questions

I tried to write a program that uses threads, but couldn't understand the o/p.
I have a simple java program that runs fine in eclipse but cannot find
I have tried to write a simple program that converts numbers to words (i.e
i'm quite a beginner in C# , i tried to write a program that
I have tried to write my first Boost program from information on the Boost
I have to write a program that accepts a sequence of average daily temperatures
Can someone write up a source for a program that just has a game
I've problem with serial port. I've written a program that can read ports COM1
I have a program that monitors debug messages and I have tried using a
I have a bash program that will write to an output file. This file

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.