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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:29:59+00:00 2026-05-31T20:29:59+00:00

I am having problems with passing arguments to new threads, I have the following

  • 0

I am having problems with passing arguments to new threads, I have the following

 doRowOffset[0] = 0;
 for(threads = 1; threads < NUMTHREADS; threads++){
      doRowOffset[threads] = threads;
      printf("Do row: %d\n", doRowOffset[threads]);
      pthread_create(&threadHandler[threads], NULL, multiplyRows, (void *) &doRowOffset[threads]);
 }

and

void * multiplyRows(void * dealWithRows){
        int offset = *((int *) dealWithRows);
        printf("%d\n", *((int *) dealWithRows));
        printf("Offset: %d\n", offset);
        printf("Size: %d\n", partitionSize/NUMTHREADS);
        printf("Calculated Offset: %d\n", offset*partitionSize/NUMTHREADS);
        ...

Now I expect to see the following output

Do row: 1
1
Offset: 1
Size: 2
Calculated offset: 2

However I keep getting garbage values or 0 for dealWithRows when I get into the thread am I doing something wrong with argument passing?

  • 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-31T20:30:00+00:00Added an answer on May 31, 2026 at 8:30 pm

    If you pass the address of a variable that is changing, you have to expect that it may change on you 🙂

    What’s probably happening is that you’re setting doRowOffset to 1 (for example) then starting the thread. Before that thread kicks up and dereferences its pointer, you change doRowOffset for the next thread.

    Then when the first thread finally dereferences the address, you’ve changed the underlying value.

    There are two ways to handle this. The first is to synchronise the main thread and child thread so that the main one waits until the child had dereferenced the variable and stored the value locally. Only then does the main thread change the variable for the next thread it’s about to start. However, that sort of defeats the purpose of threads 🙂

    The other is to cast the actual integer value (not the address) to a void pointer, which is passed to the thread function. This will mean the value will be copied to the stack of the thread function rather than the address, and the main thread will then not be able to affect it.

    Something like:

    pthread_create (&threadHandler[threads], NULL, multiplyRows,
        (void *) doRowOffset); // <<-- Note, removed the '&'.
    

    in the main thread and:

    int offset = (int) dealWithRows;
    

    in the child thread.

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

Sidebar

Related Questions

Im having problems displaying records to my view when passing viewdata to a user
I'm having some problems with Jquery UI Dialog and TinyMCE. Passing data to a
I am having a problem with views. I have a view and am passing
I'm having problems passing information, updating progress and indicating done with a SwingWorker class
I'm having serious problems with accepting payments. I'm passing the total amount in a
I'm working with PHPRO's MVC framework and am having problems passing a Registry object
I'm having problems passing parameters (in this case, @plan_id) to the next page (new_user_url).
I am new to objective C and am having a problem with the following
I'm having trouble passing arguments to xquery in powershell. Works fine in cygwin. What's
I'm having problems passing utf-8 strings to javascript (ajax). Currently i'm using rawurlencode on

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.