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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:31:56+00:00 2026-05-28T14:31:56+00:00

Im trying to create 4 threads to run a function simultaneously at my 4

  • 0

Im trying to create 4 threads to run a function simultaneously at my 4 CPU cores. The function i call will change some loop offsets depending on val variable value.

I tried this, but it doesnt increase the val counter properly, some of the threads report same values, it seems to change randomly:

int val = 1;
threads[0] = CreateThread(0, 0, my_thread_1, &val, 0, 0);
val++;
threads[1] = CreateThread(0, 0, my_thread_1, &val, 0, 0);
val++;
threads[2] = CreateThread(0, 0, my_thread_1, &val, 0, 0);
val++;
threads[3] = CreateThread(0, 0, my_thread_1, &val, 0, 0);

But this seems to work just fine:

int val1 = 1;
int val2 = 2;
int val3 = 3;
int val4 = 4;
threads[0] = CreateThread(0, 0, my_thread_1, &val1, 0, 0);
threads[1] = CreateThread(0, 0, my_thread_1, &val2, 0, 0);
threads[2] = CreateThread(0, 0, my_thread_1, &val3, 0, 0);
threads[3] = CreateThread(0, 0, my_thread_1, &val4, 0, 0);

What could be the reason for this, and how is it properly done to give some parameter to a thread?

This is my function:

DWORD WINAPI my_thread_1(void *params){ 
    int val = *(int *)params;
...
}
  • 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-28T14:31:57+00:00Added an answer on May 28, 2026 at 2:31 pm

    This fails simply because in your first example you are passing the pointer to the same memory location for all 4 threads. The fact that you incremented the value before passing the pointer is inconsequential, as the memory location stays the same.

    In your second example you instead pass 4, mutually exclusive pointers to the 4 threads. Therefore, the threads all read independent values.

    You could restructure your code slightly to help with maintainability and flexibility:

    int threadData[NTHREADS]; /* in the future this could be an array of structs */
    HANDLE threads[NTHREADS];
    int tt;
    
    for (tt = 0; tt < NTHREADS; ++tt)
    {
        threadData[tt] = tt + 1; /* placeholder for actual logic */
        threads[tt] = CreateThread(
            NULL,            /* lpThreadAttributes */
            0,               /* dwStackSize */
            my_thread_1,     /* lpStartAddress */
            &threadData[tt], /* lpParameter: each thread will receive its own data */
            0,               /* dwCreationFlags */
            NULL             /* lpThreadId */);
    }
    
    /* Since threadData and threads are local to the enclosing scope,
     * we must wait for them to finish here to ensure we don't read
     * data we no longer own
     */
    WaitForMultipleObjects(NTHREADS, threads, TRUE, INFINITE);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to learn how create new threads and run them. I need to
I'm trying to create a thread that configures a run loop to run a
I am trying to create a UDP listener that will listen on a separate
I am trying to create a thread to handle a login function that is
I am trying to use a class based implementation of Win32 threads to create
I'm trying to create a Tile rendering program. Heres some basic code. Header class
I´m Trying to launch two threads witch calls DispFrontEnd function First thread ended OK,
I'm trying to write a ThreadManager for my C# application. I create several threads:
I am trying to create a sleep function for Lua, since my Lua-Scripts all
I'm trying to create a handy extension method to Action to basically run that

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.