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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:54:53+00:00 2026-05-27T05:54:53+00:00

I was revisting some code I wrote a long time ago, and decided to

  • 0

I was revisting some code I wrote a long time ago, and decided to rewrite it to better make use of threads (and better use of programming in general..).

It is located here: https://github.com/buddhabrot/buddhabrot/blob/master/basic.c:

It is an application that renders a buddhabrot fractal. For reasons out of the scope of this question it is hard to use memoization to optimize this, and basically if you’d profile this, over 99% of the time is spent in the innermost loop that eventually does:

buddhabrot[col][row]++;

Multiple threads will execute this code. Since incrementing is not thread-safe, I used a specific mutex lock around this part of the memory. So, each addressable location in the buddhabrot memory has a separate mutex.

Now, this is more efficient than using one lock of course (which would definitely make all the threads wait for each other), but it is less memory efficient; it appears the mutexes take some data as well. I am also wondering about other repercussions in the pthreads implementations with millions of mutexes?

I now have two other strategies to consider:

  • Use a less dense set of mutex locks, for each “region” in the map. So, a lock for [col/16][row/16], for instance, would only lock a thread if it visits the same region of 16 pixels as another one. The density of the locks could be dynamically adjusted.
    But as I was modeling this I was wondering if I’m not solving an existing problem that might even be implemented by kernels, and I also can’t really find a way to make this without slowing things down. I also thought about “trees of mutexes”, but all of this is just too slow inside this loop (to give an indication, after optimizating the order of some maths operations behind the compiler’s back I could squeeze out about 30% more processor time). Is there a topic for this, how do I look for mor einformation on “mutex density planning”..?

  • Copy the memory for each thread so I don’t even have to mutex around it. But this is even more memory-inefficient. It would solve the problem of having millions of mutexes without knowing the repercussions thereof.

So, is there anything else, anything better I could do?

  • 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-27T05:54:54+00:00Added an answer on May 27, 2026 at 5:54 am

    You can use atomic increment functions like InterlockedIncrement from the intrin.h on Windows platforms.

    #include <intrin.h>
    
    #pragma intrinsic(_InterlockedExchangeAdd, _InterlockedIncrement, _InterlockedDecrement, _InterlockedCompareExchange, _InterlockedExchange)
    #define InterlockedExchangeAdd _InterlockedExchangeAdd
    #define InterlockedIncrement _InterlockedIncrement
    #define InterlockedDecrement _InterlockedDecrement
    #define InterlockedCompareExchange _InterlockedCompareExchange
    #define InterlockedExchange _InterlockedExchange
    
    #pragma intrinsic(abs, fabs, labs, memcmp, memcpy, memset, strcat, strcmp, strcpy, strlen)
    #pragma intrinsic(acos, cosh, pow, tanh, asin, fmod, sinh)
    #pragma intrinsic(atan, exp, log10, sqrt, atan2, log, sin, tan, cos) 
    

    This incrementation is atomic and there is no need to have millions of mutex or a global lock on your matrix.

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

Sidebar

Related Questions

I'm reviewing a piece of code I wrote not too long ago, and I
I'm just reviewing some of my old code (have some spare time), and I
I am just reviewing some code I wrote to communicate with the serial port
I'm reviewing some code for a friend and say that he was using a
I'm reviewing some code from one of our contractors: if (userLists != null) {
I was just reviewing some old code and found the following (inside foo.asp): Const
I am having some issues with logging. After reviewing JBoss Seam source code, I
I'm reviewing some old notes from my algorithms course and the dynamic programming problems
Here's the situation. I'm reviewing some SDK code that I didn't write because I've
Some I'm reviewing some code within my team's code base, where we traverse over

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.