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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:26:36+00:00 2026-06-01T20:26:36+00:00

So, I’m trying to make a simple multi-threaded program that validates the Collatz Conjecture

  • 0

So, I’m trying to make a simple multi-threaded program that validates the Collatz Conjecture for a large set of numbers and returns the total amount of validated numbers. Each thread (total 4) does an interval of numbers and updates the “validated” variable when a number reaches 1. I’m also timing the entire process (to compare vs a single threaded computation)

The problem I’m having is that there is never any consistency when I print out the “validated” int at the end of the program, so I am guessing that either the threads are writing over each other, or the main thread is completing before the others, thus printing an incomplete number. I’m also assuming that the clock() calculations will be off too, if the main thread is completing before the others. So, how do I STOP the main thread from continuing until the other threads are completed (thus making it wait for a updated validated count and complete an accurate time measurement)? This is what I thought WaitForSingleObject did, but I’m guessing it just stops the main thread from EXITING, still allowing it to compute its other functions.

This is my first go at any multi-threading, and I don’t think I quite understand the workings of synchronization and the WaitForSingleObject command. Here is what I have so far in my main function:

EDIT: Here is my updated Main function and Collatz function. I modified it so that each thread is accessing a separate variable to avoid the synchronization issue, but the problem still persists. There is no consistent value when I print out “validated” *

EDIT AGAIN: Alright, so I removed the “thread” int per Mladen Janković, and just used a simple counter to distribute the different intervals to the created threads. There is now a consistent, correct value for “validated”. HOWEVER, I still cannot get the program to actually finish when there are 1,000,000 numbers. Testing it for 100,000 or even 10,000 works flawlessly, but when I up it to 1,000,000 numbers, the program runs indefinitely (hours) without actually returning a values. I’m guessing that it is getting stuck at a particular value (eg 750831 as Martin James pointed out). I tried substituting int for long int, but it seems that it still suffers from overflow. Any suggestions? And thanks for the immense help.

LAST EDIT: Alright, so I just used long long instead of int and now the program works flawlessly. Thanks for the help everyone!

void main() 
{
    clock_t start;
    clock_t finish;
    unsigned int thread = 0;

    start = clock();

    HANDLE h1 = (HANDLE)_beginthreadex(NULL, 0, collatz_thread, NULL, 0, NULL);

    HANDLE h2 = (HANDLE)_beginthreadex(NULL, 0, collatz_thread, NULL, 0, NULL);

    HANDLE h3 = (HANDLE)_beginthreadex(NULL, 0, collatz_thread, NULL, 0, NULL);

    for (int i = 750001 ; i <= 1000000 ; i++) { collatz(i, 4); }

    WaitForSingleObject( h1, INFINITE );
    WaitForSingleObject( h2, INFINITE );
    WaitForSingleObject( h3, INFINITE );

    finish = clock() - start;
    double time = finish / (double) CLOCKS_PER_SEC;

    validated = v1 + v2 + v3 + v4;
    cout << validated << " numbers validated." << endl;
    cout << endl << time << " seconds." << endl;
}

unsigned _stdcall collatz_thread (void* n)
{   
    selection++; // selects a different interval each time collatz_thread is called

    switch (selection) {
    case 1:
        for (int i = 1 ; i <= 250000; i++)      { collatz(i, 1); }
        break;
    case 2:
        for (int i = 250001 ; i <= 500000; i++)  { collatz(i, 2); }
        break;
    case 3:
        for (int i = 500001 ; i <= 750000; i++)  { collatz(i, 3); }
        break;
    }
    return 0;
}

int collatz (int n, int thread)
{
    int original = n;

    while (n != 1) {
    if (n%2 == 0)
        n = (n/2);
    else
        n = (3*n + 1);
    }

    if (n == 1) {
    switch (thread) {
        case 1:
            v1++;
            break;
        case 2:
            v2++;
            break;
        case 3:
            v3++;
            break;
        case 4:
            v4++;
            break;
    }
    return n;
}

}

  • 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-01T20:26:37+00:00Added an answer on June 1, 2026 at 8:26 pm

    You need to synchronize access to validated if it is shared variable. The easies way is to use InterlockedIncrement function instead of standard ++ operator when you want to increment it. Another approach is to use some kind of synchronization object like spinlock or mutex when you access the shared variable, but that is overkill if you just need to synchronize increment operation.

    If you want more details, please provide code of collatz function.

    As ‘usr’ suggested, for the better performance you can use separate variable fore each thread and then sum them in the main thread. In this scenario, you should pad those variables in such a way so they don’t share same cache-line to avoid false sharing.

    You haven’t provided collatz_thread function, which could be another cause of inconsistent results. The reason is you pass pointer to variable (&thread) that stores thread # which changes between the calls that create new threads, so depending on the state of the OS’s scheduler the new threads might not get a chance to start while thread variable is already changed to have another value, so then you’ll have more then one thread doing the same set of data, while other sets might be skipped. Since the behavior depends on current state of thread scheduler it’s pretty much unpredictable.

    The solution is cast thread variable to void* instead of passing its address and then in collatz_thread function cast it back to int:

    HANDLE h1 = (HANDLE)_beginthreadex(NULL, 0, collatz_thread, (void*)thread, 0, NULL);

    And as Martin suggested, you possibly have integer overflow, but it shouldn’t cause inconsistent results, just wrong results, but consistent nevertheless.

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

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

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.