如何启动一个数组,每个元素都在一个单独的线程中

发布于 2024-12-14 17:09:34 字数 1259 浏览 0 评论 0原文

我正在尝试创建一个大小为 n 的数组(其中 n 是用户的输入),当用户运行程序时,数组元素应设置为 1(每个元素都在单独的线程中)。这是我到目前为止所做的:

#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <process.h>

int *x;

DWORD WINAPI init_X(LPVOID param)
{
    int index = *(int *) param;
    x[index] = 1;

    return 0;
}

int main(int argc, char *argv[])
{
    int n = atoi(argv[1]);
    int i; // counter.
    HANDLE THandles[n];

    x = malloc(n * sizeof (int));

    for(i = 0; i < n; i++)
    {
        THandles[i] = CreateThread(NULL, 0, init_X, &i, 0, NULL);
    }

    // Now wait for threads to finish
    WaitForMultipleObjects(n, THandles, TRUE, INFINITE);

    // Close the thread handle
    for(i = 0; i < n; i++)
    {
        CloseHandle(THandles[i]);
    }

    printf("After initialization x = ");
    for(i = 0; i < n; i++)
    {
        printf("%d ", x[i]);
        if(i < n - 1) printf(" ");
    }

    // ...

    return 0;
}

我运行这个程序,但得到了错误的输出:

> Test.exe 3
After initialization x = 11611536 11600064 50397186

它应该是 After初始化 x = 1 1 1 。我不确定如何解决这个问题,但我确信它与指针有关。

PS:我是Java程序员,所以对指针不太熟悉。

I am trying to create an array of size n (where n is user's input) and when the user runs the program, the array elements should be set to 1 (each in a separate thread). Here is what I have done so far:

#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <process.h>

int *x;

DWORD WINAPI init_X(LPVOID param)
{
    int index = *(int *) param;
    x[index] = 1;

    return 0;
}

int main(int argc, char *argv[])
{
    int n = atoi(argv[1]);
    int i; // counter.
    HANDLE THandles[n];

    x = malloc(n * sizeof (int));

    for(i = 0; i < n; i++)
    {
        THandles[i] = CreateThread(NULL, 0, init_X, &i, 0, NULL);
    }

    // Now wait for threads to finish
    WaitForMultipleObjects(n, THandles, TRUE, INFINITE);

    // Close the thread handle
    for(i = 0; i < n; i++)
    {
        CloseHandle(THandles[i]);
    }

    printf("After initialization x = ");
    for(i = 0; i < n; i++)
    {
        printf("%d ", x[i]);
        if(i < n - 1) printf(" ");
    }

    // ...

    return 0;
}

I run this program and I got wrong outputs:

> Test.exe 3
After initialization x = 11611536 11600064 50397186

It should be After initialization x = 1 1 1 though. I am not sure how I can I fix this, but I am sure its something related to the pointers.

P.S: I'm Java programmer so I'm not familiar with pointers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡淡绿茶香 2024-12-21 17:09:37

作为数组索引传递的值很可能在线程运行时无效,因为无法保证线程在调用 CreateThread 后立即运行。

您有两种解决方案,要么按值传递(简单且容易,但并不总是安全),要么为线程使用时将释放的值分配一个临时缓冲区。

小更新:

事实上,更好的方法是传递 &x[i],然后你可以这样做 *(int*)param = 1;

The value you are passing as your array index will more than likely be invalid by the time the thread runs, as there is no guaranteeing that the thread is run immediately after the call to CreateThread.

You have two solutions, either pass by value (simple & easy, but not always safe) or allocate a temporary buffer for the value that will be freed by the thread when its used.

Minor Update:

In fact, a better way would be to pass &x[i], then you can just do *(int*)param = 1;

谜泪 2024-12-21 17:09:37

您通过指向线程的指针传递 i ,因此每个线程获取的值将取决于 int index = *(int *) param; 实际执行的时间,并且应该是介于 0n 之间的值。您可以仅按值传递 i (转换为指针)来避免这种情况。

You are passing i by pointer to the thread, so the value each thread gets will depend on when int index = *(int *) param; actually executes and it should be something between 0 and n. You can just pass i by value (casted to a pointer) to avoid this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文