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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:29:32+00:00 2026-06-15T18:29:32+00:00

I am a newbie in c++ working on what should be a pretty basic

  • 0

I am a newbie in c++ working on what should be a pretty basic file read and then process data functionality and I keep getting stuck on being able to at least provide a “state” of on thread to another so that data could be consumed. This just maybe something really basic that I am overlooking – could use some insight into using pthreads in c++.

Bellow is some basic extracted code that functions OK, reads the file and provides data to be processed. Another thread that will process the data needs to know the state of this one.
What would be the best strategy? I am attempting to request the state of the thread through a function from another thread but receive incorrect response.

Reader::Reader(){

    _threadId = 1; 
    _msg = NONE; // enum NONE, READ, STOP
    active = false;
    pthread_mutex_init(&_mutex, 0);
}

Reader::~Reader(){

    pthread_mutex_destroy(&_mutex);
}

void Reader::read(){

    _msg = READ;
    active = true;

    pthread_create(&_threadId, 0, work, this);
}

void * Reader::work(void *myselfreader){

    Reader * reader = (Reader*)myselfreader;

    reader->loop();

    pthread_exit(0);

    return 0;
}

void Reader::loop(){

    while(active){

            pthread_mutex_lock(&_mutex);

            switch(_msg){

                case READ:

                    // do the reading of the IO file - which works fine                 
                    // once done reading the file - the _msg is set to STOP

                    break;

                case STOP:

                    stopThread();

                    break;


                default:
                        break;
            }

            pthread_mutex_unlock(&_mutex);
        }

    return;
}

void Reader::stopThread(){

    active = false;

    _msg = ENC_NONE;

    pthread_join(_threadId, 0);
}

/*****************/

int Reader::getReaderState(){

    // another thread needs to know the state of this thread
    // 
    return _msg // ??
    return active // ??
}
  • 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-15T18:29:33+00:00Added an answer on June 15, 2026 at 6:29 pm

    It seems that you have race condition somewhere in your code.

    You need to protect your _msg variable with mutex. Every time you need to update _msg variable request mutex, update variable and close the mutex. You need to do the same thing for the reading in getReaderState() function – acquire mutex, copy variable into temp, release mutex and return temp variable.

    For easy and error-prone usage, you should create getter and setter function for accessing _msg field which will be guarded by the same mutex (and not the one that you are already using):

    void setMsg(int msg)
    {
        pthread_mutex_lock(&msg_mutex);
        _msg = msg;
        pthread_mutex_unlock(&msg_mutex);
    }
    
    
    int getMsg()
    {
        int tmp;
        pthread_mutex_lock(&msg_mutex);
        tmp = _msg;
        pthread_mutex_unlock(&msg_mutex);
        return tmp;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi i'm a newbie working through Hartl. I'm stuck on microposts in Ch 10.
I'm a newbie working through Michael Hartl's Tuby on Rails Tutorial and I have
I am a newbie working towards developing an IE extension that would appear as
I'm a PHP newbie working a some scripts to display some news articles from
i am newbie to mongoDB ,as i start working with test application (ASP.Net) found
I am a newbie in Rails and am working on a page that enables
I'm a newbie C++ developer and I'm working on an application which needs to
I am kind of newbie on C++, and working on a simple program on
Hi i'm a newbie to iphone application development.. present i'm working on Picasa web
I'm a real newbie with php and MySQL. Now I'm working on the following

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.