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 578885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:18:28+00:00 2026-05-13T14:18:28+00:00

I wrote the following program for alternatively incrementing and doubling a counter(increment first) using

  • 0

I wrote the following program for alternatively incrementing and doubling a counter(increment first) using boost condition variables. Can any one tell me if this is the correct use of boost condition variables. It is working correctly. I don’t understand the use of lock in wait function call. What does condition.wait(lock) mean? For example what is the use of two scoped locks in increment and multiply in this program. How can I avoid them?

#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/bind.hpp>
#include <boost/thread/locks.hpp>
#include <boost/thread/condition_variable.hpp>
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;

int counter=0;
boost::mutex m1,m2;
bool incremented=false,multiplied=false;
boost::condition_variable c1,c2;
void Increment()
{
    {
        boost::mutex::scoped_lock lk(m1);
        counter++;
        incremented = true;
        c1.notify_one();

        while(!multiplied)
            c2.wait(lk);
        multiplied=false;

    }   
}
void Multiply()
{
    {
        boost::mutex::scoped_lock lk(m2);
        while(!incremented)
            c1.wait(lk);
        incremented = false;
        counter = counter*2 ;
        multiplied = true;
        c2.notify_one();
    }
}

void IncrementNtimes(int n){

    for(int i=0;i<n;i++){
        Increment();
    }
}

void MultiplyNtimes(int n){

    for(int i=0;i<n;i++){
        Multiply();
    }
}
int main(int argc, char* argv[])
{
    srand ( time(NULL) );

    boost::thread thrd1(boost::bind(&IncrementNtimes,20));
    boost::thread thrd2(boost::bind(&MultiplyNtimes,20));
    thrd1.join();
    thrd2.join();
    cout<<"Main counter is:"<<counter<<endl;
    return 0;
}
  • 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-05-13T14:18:28+00:00Added an answer on May 13, 2026 at 2:18 pm

    No, this is not correct. You’re almost there, but the big problem is that the Multiply and Increment functions should be using the same mutex.

    A mutex is an object that provides MUTual EXclusion. In other words, the point of a mutex is to prevent two threads from touching the same variable at the same time and causing unpredictable results. A mutex is kind of like a token that one thread at a time holds that gives it the “right” to access a certain variable (or set of variables). In this case, the variable you are trying to protect is counter. There must be one and only one mutex that controls the right to access counter. In your case, each thread will hold its own token that it thinks gives it the right to access counter, and so there will be unpredictable behavior.

    You “hold” a mutex by locking it. That’s the point of the locks, and that’s why you cannot “avoid” them. The entire point of the scoped locks is that, assuming you have only one mutex m, when one of the threads holds the lock on m, the other thread is guaranteed to not also be holding a lock on m. If you’ve coded correctly, holding a lock on m should be a prerequisite for accessing counter, and so the value of counter should be predictable.

    Now, regarding the wait(). A call to wait() means “I give up the lock on this mutex until someone else signals this condition, and then I want it back”. In the mean time, the thread stops. So assuming you have only one mutex m and a condition c, and lk is a lock on m, the line c.wait(lk) means that the thread will give up the lock lk on mand then suspend execution until some other thread calls c.notify_one() (or c.notify_all()). When the waiting thread returns from the call to wait(), it will have automatically re-gained the lock lk on m and so is permitted to access counter again.

    Finally, these boost locks are “scoped” locks. This means that they are released automatically on destruction (when they go out of scope). So in this case, each function holds its lock until it exits, except for times when it has given up its lock to wait and has suspended execution pending a signal.

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

Sidebar

Ask A Question

Stats

  • Questions 344k
  • Answers 344k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I'm not sure that rolling your own solution is going… May 14, 2026 at 5:42 am
  • Editorial Team
    Editorial Team added an answer No, IIS7 is not supported on Windows XP: IIS 7.0,… May 14, 2026 at 5:42 am
  • Editorial Team
    Editorial Team added an answer Generally, you should avoid relying on what assemblies are currently… May 14, 2026 at 5:42 am

Related Questions

I'm preparing to write a COLLADA importer in Java. There aren't any pre-written importers,
I have a WPF assembly in which I would like to embed five icons
Right now I am working on a stub of a project. In the course
I wrote the following program to prime factorize a number: import math def prime_factorize(x,li=[]):
I wrote a library and want to test it. I wrote the following program,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.