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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:02:27+00:00 2026-06-11T22:02:27+00:00

I am in the process of creating a two thread array summation program and

  • 0

I am in the process of creating a two thread array summation program and i am using windows.h threads. And this is the code that i have so far.

#include "StdAfx.h"
#include <stdio.h>
#include <iostream>
#include <windows.h>
#include <process.h>     // needed for _beginthread()

void  silly( void * );   // function prototype

using namespace std;

int arraySum[100];

int main()
{
    // Our program's first thread starts in the main() function.

    printf( "Now in the main() function.\n" );


    for(int i = 0 ; i  < 100 ; i++){
        arraySum[i] = i;
    }

    // Let's now create our second thread and ask it to start
    // in the silly() function.


    _beginthread( silly, 0, (void*)1 );
    _beginthread( silly, 0, (void*)2 );

    Sleep( 100 );

    int a;
    cin >> a;

}

void  silly( void *arg )
{
    printf( "The silly() function was passed %d\n", (INT_PTR)arg ) ;
    int partialSum = 0;
    for(int i =50*((INT_PTR)arg - 1); i < 50 * ((INT_PTR)arg) ; i++){
    partialSum == arraySum[i];
    }
}

What i seem to find it hard to do is make the function return the partion sum to the main method. Could someone please help me out.

  • 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-11T22:02:29+00:00Added an answer on June 11, 2026 at 10:02 pm

    Pass the address of an int to silly(), which can act as both input and output parameter, and have silly() populate it with the value required by the caller:

    int silly_result_1 = 1;
    int silly_result_2 = 2;
    
    _beginthread( silly, 0, (void*)&silly_result_1 );
    _beginthread( silly, 0, (void*)&silly_result_2 );
    
    void silly( void *a_arg )
    {
        int* arg = (int*) a_arg;
    }
    

    You need to wait for the two threads to complete.


    Note it is necessary that the variables whose addresses are passed to _beginthread() exist for the lifetime of the thread. For example, the following would result in undefined behaviour:

    void _start_my_thread()
    {
        int silly_result = 2;
        _beginthread( silly, 0, (void*)&silly_result );
    } /* 'silly_result' is out of scope but still used by the thread. */
    

    This can be solved by dynamically allocating memory for the variable (and decide whether the main thread or the new thread is reponsible for destroying the allocated memory).

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

Sidebar

Related Questions

I have a Main C program that creates two threads and each of those
I am using pthread library under Linux for creating threads and I have two
I am creating a new thread in c using _beginthreadex. I have written the
I have the main process forking two times and thus creating two children. The
I have written this program that has a main function, inside which, I am
Suppose that a process is creating a mutex in shared memory and locking it
I am in the process of creating an iPhone app that requires to interact
I am in the process of creating a site that i would like to
I'm in the process of creating a metasearch engine and I'm stuck! Using php
I'm in the process of creating a web service that uses XML to handle

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.