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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T21:31:32+00:00 2026-05-28T21:31:32+00:00

So I am beginning to write a program to parse and then process a

  • 0

So I am beginning to write a program to parse and then process a large amount of text. I have set up a class that contains methods which are run as boost threads. As of now each of these threads simply prints some text statement and then return. The code compiles and runs without any errors. However, the text is printed out inconsistently. This is to be expected as the threads are running in parallel, so I tried using a mutex to coordinate use of the output. However, I am clearly doing something wrong as the output is still inconsistent. To add on to this some output is printed twice which I cannot explain as a failure to code the mutex correctly. Below is my code:

/* 
 * File:   ThreadParser.h
 * Author: Aaron Springut
 *
 * Created on Feburary 2, 2012, 5:13 PM
 */

#ifndef THREADPARSER_H
#define THREADPARSER_H

#include <string.h>
#include <iostream>
#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/bind.hpp>
#include "FileWordCounter.h"

class ThreadParser {

    public:

    ThreadParser();
    ThreadParser(std::string fileName);



    private:

    //mutex for cout
    boost::mutex coutMut;  

    std::string dataFile;
    FileWordCounter fCounter;

    //threads
    void parseFile();
    void processSearches();



};

#endif     


/* 
 * File:   ThreadParser.cpp
 * Author: Aaron Springut
 *
 * Created on Feburary 2, 2012, 5:13 PM
 */


 #include "ThreadParser.h"

 using namespace std;


 ThreadParser::ThreadParser(){

    double buyNum = 0;
    buyNum = buyNum * 100;
    cout << "Percentage of people buying: "<< buyNum <<"%"<<endl;

 }

 ThreadParser::ThreadParser(string fileName){

    dataFile = fileName;
    double buyNum = 0;

    //create the mutex and aquire a lock on it for this thread

    boost::mutex::scoped_lock(coutMut);

    boost::thread parseThread(boost::bind(&ThreadParser::parseFile, this));
    boost::thread processSearches(boost::bind(&ThreadParser::processSearches,this));

    buyNum = buyNum * 100;
    cout << "Percentage of people buying: "<< buyNum <<"%"<<endl;


 }


 void ThreadParser::parseFile(){

    boost::mutex::scoped_lock(coutMut);
    cout << "parseFileThreadLaunch"<<endl;
    return;

 }


 void ThreadParser::processSearches(){

    boost::mutex::scoped_lock(coutMut);
    cout << "processSearchesLaunch"<<endl;
    return;

 }

As an example of what is going wrong here are two outputs from the program that runs this:

Percentage of people buying: parseFileThreadLaunch
processSearchesLaunch
0%

Okay, cout is not thread safe and I’ve done something wrong with the mutex.

Percentage of people buying: parseFileThreadLaunch
0%
processSearchesLaunch
processSearchesLaunch

This is confusing, how is the last line being printed twice? Is it a consequence of cout not being thread safe? Or, am I missing part of the bigger picture.

EDIT:
The class is being called like so in the main function:

string fileName("AOLData.txt");
cout << fileName<< endl;

ThreadParser tp(fileName);
  • 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-28T21:31:32+00:00Added an answer on May 28, 2026 at 9:31 pm
    boost::mutex::scoped_lock(coutMut);
    

    This does not do what you think it does. This creates a temporary, which is immediately destroyed, releasing the lock. Just like int(3);.

    You want:

    boost::mutex::scoped_lock sl(moutMut);
    

    This creates an object, sl, that holds the lock until it goes out of scope.

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

Sidebar

Related Questions

We have a database app that we're beginning to write some new systems for
I have a program which reads data from 2 text files and then save
I'm taking a beginning Java class and an assignment requires that I write a
I'm in the process of debugging a C program (that I didn't write). I
My class assignment is to write a program that has the user input a
I'm writing a C++ program for class that requires a set of N child
I am beginning to write a translator program which will translate a string of
I'm trying to write a program that takes input of - hexadecimals, octals, and
I have a program that periodically polls a certain folder for log files. When
I would like to write a small tool that takes a C++ program (a

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.