初始化用于pthread的结构体数组
Initializing an array of structs for usage with pthreads
我正在尝试使用GMP结构mpz_class
获得p_threads程序:
int main(){
pthread_t* threads = (pthread_t*)malloc(thread_count * sizeof(pthread_t));
data* results_n_params = (data*)malloc(thread_count * sizeof(*results_n_params));
int thread_count = 10;
unsigned long start = 1;
unsigned long end = 100;
unsigned long batch_size = 10;
for(int i = 0; i < thread_count; i++){
// Set parameters of results struct.
results_n_params[i].start = start + i * batch_size;
results_n_params[i].end = start + (i + 1) * batch_size;
pthread_create(&threads[i], NULL, add_part, (void*) &results_n_params[i])
}
结构体:
typedef struct {
unsigned long start;
unsigned long end;
mpz_class result;
} data;
下面是线程体:
void *do_things(void* data_struct){
data* current_data = (data*) data_struct;
mpz_class result = 0;
current_data->result = result; // <= This results in Bus error: 10
return NULL;
}
问题可能在于results_n_params
数组的初始化。thread_count
变量作为输入(只是将其更改为静态,以便更短的代码)当更改thread_count
周围或增加分配的大小时,我可以避免访问mpz_class result
时获得的bus error: 10
。像这样,我得到thread_size
的Bus Error: 10
在1和10之间。高一点也行。我做错了什么?
我不知道threadcount是在哪里声明的。
相关文章:
- 按结构体的特定成员对结构体数组进行排序
- 如何调用一个函数,它在一个单独的类中打印出一个结构体数组
- 如何创建一个结构体数组
- 初始化用于pthread的结构体数组
- 如何将文件的行读入结构体数组
- 结构体数组,非常量大小
- 无法将结构体数组传递给函数
- 确定它用作参数的结构体数组的大小
- 如何将结构体数组的单个成员数组传递给函数以执行线性搜索
- 如何使用std::binary_search或std::sort对结构体数组进行排序
- c++中对结构体数组使用排序选择
- 在c++中将结构体数组平整化为多个数组
- 在c++中创建结构体数组
- 不能操作由字符串组成的结构体数组
- 传递结构体数组的预期非限定id错误
- 将c++中的结构体数组传递给swift
- 尝试创建一个函数,将数据从文件读取到结构体数组中
- 在c++中创建结构体数组
- 添加并列出一个结构体数组
- Arduino编程中的结构体数组