gpt4 book ai didi

c++ - 如何在pthread线程中创建memcpy?

转载 作者:行者123 更新时间:2023-12-02 10:38:30 25 4
gpt4 key购买 nike

我正在尝试使用c++中的pthreads做2个矩阵的总和。我一直试图将线程内部计算的总和的结果传递给我的主函数。

要添加的2个值在结构内部:

struct sum{
int value1;
int value2;
int result;
}typedef struct_sum;

并将包含值的结构作为参数传递给 pthread_create(),以便在线程内部执行操作。

这是我的例程:
void * routine(void * sum) {
std::cout<<((struct_sum *)sum)->value1 + ((struct_sum *)sum)->value2<<std::endl;
std::cout<<((struct_sum *)sum)->value1<<std::endl;
std::cout<<((struct_sum *)sum)->value2<<std::endl;
int i = (((struct_sum *) sum)->value1 + ((struct_sum *) sum)->value2);
// memcpy(&(((struct_sum *)sum)->result), reinterpret_cast<const void *>(i), sizeof(i));
((struct_sum *)sum)->result = i;
std::cout<<&(((struct_sum *)sum)->result)<<std::endl;
pthread_exit(nullptr);
}

在前3个 cout中,我检查我的值是否正确到达线程。
在最后一个 cout中(退出线程之前),我检查了结构的result元素的内存地址(因此,我可以看到它在main函数中具有相同的地址)。

主要功能如下:
int main(int argc, char * argv[]) {
int mat_1[ROW_SIZE][COLUMN_SIZE] = {{1, 2},
{6, 7}};
int mat_2[ROW_SIZE][COLUMN_SIZE] = {{3, 15},
{9, 14}};
int mat_result[ROW_SIZE][COLUMN_SIZE];
int mat_size = sizeof(mat_1) / sizeof(int);
int row_size = sizeof(mat_1) / sizeof(mat_1[0]);
int column_size = sizeof(mat_1[0]) / sizeof(int);
pthread_t threads[mat_size];
int thread_number = 0;
int thread_handler;
for (int row = 0; row < row_size; row++) {
for (int column = 0; column < column_size; column++) {
struct_sum *result;
result = static_cast<struct_sum *>(malloc(sizeof(struct_sum)));
result->value1 = mat_1[row][column];
result->value2 = mat_2[row][column];
result->result = 0;
thread_handler = pthread_create(&threads[thread_number], nullptr, routine, result);
if(thread_handler) return(-1);
std::cout << &(result->result)<<std::endl;
thread_number++;
mat_result[row][column] = result->result;
// free(result);
}
}
pthread_exit(nullptr);
}

我有两个问题:
  • 即使结果在主线程和线程中具有相同的地址,当我将i的值复制到((struct_sum *)sum)->result时,在main函数中,result->result仍为0。
  • 当我取消注释memcpy()行时,该线程根本无法运行,所以我不知道我做错了什么。

  • 我期待在 main函数中使用 std::cout << (result->result) <<std::endl语句返回操作结果,但当前值为0。

    那么,如何在线程中正确执行 memcpy()

    最佳答案

    您必须加入线程。这意味着,等待他们完成。
    您的工作方式基本上是启动线程,而不给它保证的时间做任何事情。除此之外,对API进行了一些重要更改,请检查以下注释:

    void * routine(void * sum) {
    int i = (((struct_sum *) sum)->value1 + ((struct_sum *) sum)->value2);
    ((struct_sum *)sum)->result = i;

    // notice you don't need memcpy(), in fact...
    // but you could use it here if you want... it won't fail.

    // you have to use this function so it return your result to the main thread.
    pthread_exit(sum);
    }

    int main(int argc, char * argv[]) {
    int mat_1[ROW_SIZE][COLUMN_SIZE] = {{1, 2},
    {6, 7}};
    int mat_2[ROW_SIZE][COLUMN_SIZE] = {{3, 15},
    {9, 14}};
    int mat_result[ROW_SIZE][COLUMN_SIZE];
    int mat_size = sizeof(mat_1) / sizeof(int);
    int row_size = sizeof(mat_1) / sizeof(mat_1[0]);
    int column_size = sizeof(mat_1[0]) / sizeof(int);
    pthread_t threads[mat_size];
    int thread_number = 0;
    int thread_handler;
    for (int row = 0; row < row_size; row++) {
    for (int column = 0; column < column_size; column++) {
    struct_sum *result;
    result = static_cast<struct_sum *>(malloc(sizeof(struct_sum)));
    result->value1 = mat_1[row][column];
    result->value2 = mat_2[row][column];
    result->result = 0;
    thread_handler = pthread_create(&threads[thread_number], nullptr, routine, result);
    if(thread_handler) return(-1);
    // std::cout << &(result->result)<<std::endl;
    thread_number++;
    // forget this line below
    // mat_result[row][column] = result->result;
    }
    }

    // here you wait for the threads to JOIN
    // here it means they actually "finished" their job
    for (int i = 0; i < mat_size; i++)
    {
    struct_sum *result;

    // here you wait for the threads to finish their job
    // add something to your struct to "identify" the thread, so
    // you can figure out where in the final matrix you put the result
    pthread_join(threads[i], (void**)&result);
    std::cout << result->result << "\n";

    // this will print the correct sums: 4, 17, 15 and 21
    // notice: it will be printed in ANY order, once you
    // don't know which thread will finish first
    // but result->result has the... result you need!
    // you have to figure out how to fit this result in your matrix.
    // but this is out of scope of the question
    // and you can do yourself. have fun! :-)

    // here you can free the result, you already got the value!
    free(result);
    }

    // you don't need this line below... this goes to routine()
    // pthread_exit(nullptr);
    return 0;
    }

    关于c++ - 如何在pthread线程中创建memcpy?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57684222/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com