作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用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);
}
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/
我是一名优秀的程序员,十分优秀!