传球达阵参考线问题 [英] Problems passing array by reference to threads

查看:88
本文介绍了传球达阵参考线问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习线程我发现一些简单的例子。

什么我希望做的就是创建5个线程,每个分配一个随机数的20 INT的数组。后来终于有另外5个线程重建这个数组到一个更大尺寸的100 INT。

下面是一些之前code我尝试。我希望能够通过引用传递一个数组,没有运气。

任何想法,将AP preciated,请记住,我是完全新的线程

 的#include< process.h>
#包括LT&;&WINDOWS.H GT;
#包括LT&;&iostream的GT;
#包括LT&;&的fstream GT;
#包括LT&;&time.h中GT;
//#包括LT&;螺纹>使用命名空间std;无效MyThread的(无效*虚拟);
无效myThread2(void *的虚拟);诠释的main()
{    MYFILE ofstream的;
    myfile.open(coinToss.csv);    INT RNUM;    长numRuns;
    长计数= 0;
    INT除数= 1;
    浮动支架= 0;
    INT计数器= 0;
    浮动百分比= 0.0;    INT数组1 [百万]
    INT数组2 [百万]
    函数srand(时间(NULL));    的printf(运行(10个使用多个)?);
    CIN>> numRuns;    的for(int i = 0; I< numRuns;我++)
    {
        _beginthread(MyThread的,0,(无效*)(数组1));
        _beginthread(myThread2,0,(无效*)(数组2));    }}无效MyThread的(无效*参数)
{
    INT I = *为(int *)参数;    对于(INT X = 0; X< 1000000; X ++)
    {
        //参数[X] =兰特()%2 + 1;
        我[X] =兰特()%2 + 1;
    }}无效myThread2(void *的参数)
{
    INT I [百万] = *为(int *)参数;    对于(INT = 0; X< 1000000; X ++)
    {
        我[X] =兰特()%2 + 1;
    }}


解决方案

你需要认识到的第一件事:

 的for(int i = 0; I< numRuns;我++)
    {
        _beginthread(MyThread的,0,(无效*)(数组1));
        _beginthread(myThread2,0,(无效*)(数组2));    }

_beginthread 在调用返回的立即的。他们不会等待线程完成,甚至开始。它只是排队在操作系统的调度,并返回该线程。

不过,code以上为的main()函数的结束。我很怀疑,一个版本下建立自己的线程甚至会产生的初始化的你的整个程序退出前。你需要建立一个机制,你的主线程将等待工作线程完成其工作程序关闭前。这样做是远远超出了SO职位的范围,但在看的 CreateEvent()并的 WaitForMultipleObjects的()

你需要了解接下来的事情是对的的东西寿命和所有权语义的送你到线程。您传递指针到这是在范围的自动变量数组的main()

  INT数组1 [百万]
 INT数组2 [百万]

只要在这些数组的声明范围内(在这里,的main())退出,这些变量的不复存在的。这是很少,如果有的话,正确的指针传递给本地范围的变量工作线程 - 的从不的正确的,如果在本地范围内的线程完成之前退出

动态分配的数组,然后把它们转移所有权有关的工作线程将在这里解决这个问题。在这样做时,请管理这些对象/数组的所有权语义时要小心。

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;
    }

}

解决方案

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.

这篇关于传球达阵参考线问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆