Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
638 views
in Technique[技术] by (71.8m points)

c++ - Problems passing array by reference to threads

I'm learning threading and I've found some simple examples.

What I'm hoping to do is create 5 threads, that each assign a random number to an array of 20 int's. Then finally have another 5 threads that reconstruct this array to a larger 100 sized int.

Here's some prior code I was trying. I was hoping to be able to pass an array by reference, with no luck.

Any ideas would be appreciated, please keep in mind, I'm completely new to threads

#include <process.h>
#include <windows.h>
#include <iostream>
#include <fstream>
#include <time.h>
//#include <thread>

using namespace std;

void myThread (void *dummy );
void myThread2 (void *dummy );

int main()
{

    ofstream myfile;
    myfile.open ("coinToss.csv");

    int rNum;

    long numRuns;
    long count = 0;
    int divisor = 1;
    float holder = 0;
    int counter = 0;
    float percent = 0.0;

    int array1[1000000];
    int array2[1000000];


    srand ( time(NULL) );

    printf ("Runs (use multiple of 10)? ");
    cin >> numRuns;

    for (int i = 0; i < numRuns; i++)
    {
        _beginthread( myThread, 0, (void *) (array1) );
        _beginthread( myThread2, 0, (void *) (array2) );

    }

}

void myThread (void *param )
{
    int i = *(int *)param;

    for (int x = 0; x < 1000000; x++)
    {
        //param[x] = rand() % 2 + 1;
        i[x] = rand() % 2 + 1;
    }

}

void myThread2 (void *param )
{
    int i[1000000] = *(int *)param;

    for (int = 0; x < 1000000; x++)
    {
        i[x] = rand() % 2 + 1;
    }

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First thing you need to realize:

 for (int i = 0; i < numRuns; i++)
    {
        _beginthread( myThread, 0, (void *) (array1) );
        _beginthread( myThread2, 0, (void *) (array2) );

    }

The calls to _beginthread return immediately. They don't wait for the threads to finish, or even to start. It just queues up the thread in the OS's scheduler and returns.

However, the code above is the end of the main() function. I very much doubt that under a Release build your threads will even have initialized before your whole program exits. You need to build a mechanism by which your main thread will wait for the worker threads to finish their work before your program shuts down. Doing this is way beyond the scope of an SO post, but look in to CreateEvent() and WaitForMultipleObjects().

Next thing you need to understand is the lifetime and ownership semantics of the stuff you send to the threads. You are passing pointers to arrays which are automatic variables scoped in main():

 int array1[1000000];
 int array2[1000000];

As soon as the scope in which these arrays are declared (here, main()) exits, the variables cease to exist. It is rarely, if ever, correct to pass a pointer to a locally-scoped variable to a worker thread -- never correct if the local scope exits before the thread finishes.

Dynamically allocating those arrays and then transferring ownership of them to the worker threads will fix that problem here. When doing this, please be careful when managing the ownership semantics of these objects/arrays.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...