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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:41:34+00:00 2026-05-14T00:41:34+00:00

I have a huge global array of structures. Some regions of the array are

  • 0

I have a huge global array of structures. Some regions of the array are tied to individual threads and those threads can modify their regions of the array without having to use critical sections. But there is one special region of the array which all threads may have access to. The code that accesses these parts of the array needs to carefully use critical sections (each array element has its own critical section) to prevent any possibility of two threads writing to the structure simultaneously.

Now I have a mysterious bug I am trying to chase, it is occurring unpredictably and very infrequently. It seems that one of the structures is being filled with some incorrect number. One obvious explanation is that another thread has accidentally been allowed to set this number when it should be excluded from doing so.

Unfortunately it seems close to impossible to track this bug. The array element in which the bad data appears is different each time. What I would love to be able to do is set some kind of trap for the bug as follows: I would enter a critical section for array element N, then I know that no other thread should be able to touch the data, then (until I exit the critical section) set some kind of flag to a debugging tool saying “if any other thread attempts to change the data here please break and show me the offending patch of source code”… but I suspect no such tool exists… or does it? Or is there some completely different debugging methodology that I should be employing.

  • 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-14T00:41:35+00:00Added an answer on May 14, 2026 at 12:41 am

    How about wrapping your data with a transparent mutexed class? Then you could apply additional lock state checking.

    class critical_section;
    
    template < class T >
    class element_wrapper
    {
    public:
        element_wrapper(const T& v) : val(v) {}
        element_wrapper() {}
        const element_wrapper& operator = (const T& v) {
    #ifdef _DEBUG_CONCURRENCY 
            if(!cs->is_locked())
                _CrtDebugBreak();
    #endif
            val = v;
            return *this;
        }
        operator T() { return val; }
        critical_section* cs;
    private:
        T val;
    };
    

    As for critical section implementation:

    class critical_section
    {
    public:
        critical_section() : locked(FALSE) {
            ::InitializeCriticalSection(&cs);
        }
        ~critical_section() {
            _ASSERT(!locked);
            ::DeleteCriticalSection(&cs);
        }
        void lock() {
            ::EnterCriticalSection(&cs);
            locked = TRUE;
        }
        void unlock() {
            locked = FALSE;
            ::LeaveCriticalSection(&cs);
        }
        BOOL is_locked() {
            return locked;
        }
    private:
        CRITICAL_SECTION cs;
        BOOL locked;
    };
    

    Actually, instead of custom critical_section::locked flag, one could use ::TryEnterCriticalSection (followed by ::LeaveCriticalSection if it succeeds) to determine if a critical section is owned. Though, the implementation above is almost as good.

    So the appropriate usage would be:

    typedef std::vector< element_wrapper<int> > cont_t;
    
    void change(cont_t::reference x) { x.lock(); x = 1; x.unlock(); }
    
    int main()
    {
        cont_t container(10, 0); 
    
        std::for_each(container.begin(), container.end(), &change);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a python module that makes use of a huge dictionary global variable,
I have a huge database with some 100 tables and some 250 stored procedures.
Hey right now I'm using jQuery and I have some global variables to hold
Using POSIX threads & C++, I have an Insert operation which can only be
I want to have huge background images on my site but without giving the
We have huge codebase and some classes are often used via reflection all over
I have huge 3D arrays of numbers in my .NET application. I need to
I have huge number of Word files I need to merge (join) into one
I have a huge web app that is having issues with memory leak in
I have a huge file, where I have to insert certain characters at 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.