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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:55:04+00:00 2026-06-10T04:55:04+00:00

Ive been googling mutex tutorials / examples but cant seem to find one for

  • 0

Ive been googling mutex tutorials / examples but cant seem to find one for the situation I need (or more likely I did, but just never realized how to apply it to my situation, as I don’t fully understand it at the moment).

I have a C++ dll that has 2 callback functions. The callback functions are called from a 3rd party program. Each call back function adds information to a global variable (separate global variables). There are 2 other functions that each use these global variables.

The 3rd party program has many threads, so the call back functions are being called all the time. The other 2 functions are manually called by the user. The global variables end up being accessed by 2 things at once and causes a crash.

right now the global variables are in a class like this

 class Global
{
public:
static CString &  get_allscores() {  static CString get_allscores; return get_allscores; }
static CString &  get_allplayers() {  static CString allplayers; return allplayers; }
};

Could someone show me a quick example on how to use mutex with 2 functions that are both accessing one of those global variables ?

Thanks

EDIT:

What about a situation like this

CString allchat; //global variable

void function1()
{
    allchat += "test";
}

void function2()
{
    cout << allchat;
}

if those functions were being called by many threads repeatedly and out of order, can I add a mutex object to make it multi thread friendly?

so far my plan was to create a mutex with no owner when the program launches.. and then when each function is called itll take ownership and release it at the end of the function. is this the correct way of doing it?

  • 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-10T04:55:06+00:00Added an answer on June 10, 2026 at 4:55 am

    With respect to the EDITed part of the answer: yes, if these are the only functions that access the global data, adding a mutex lock to both functions will make the code thread safe. The danger, of course, is forgetting, in the course of developing and maintaining the code, that this global data has to be protected with a mutex. Of course, the solution to that problem is to take the code as written and wrap it in a class, so that allchat is a private member, adding a mutex to the class, and using the mutex from any member functions that access allchat.

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

Sidebar

Related Questions

I've been googling for hours, and I cant seem to find a SIMPLE answer
Ive been googling for a few hours now and cant find this little snippet
I've been googling around for a bit but I can't seem to find an
Ive been googling for quite some while and couldn't find it! I need to
I've been googling for more than 2 weeks to find any Rails code working
I've been Googling for a while and can't seem to find an answer to
I've been googling this question a lot and I dont seem to find a
ive been googling and failed to find a jquery twitter plugin that is seo
I've been googling this for some time now, but I could never find an
I've been googling for this resource, but couldn't find anything in the fly, does

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.