Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
素数筛不总是在范围内得到正确的素数 我用C++中的ErasoTeSes筛选器使用多个线程来做素数筛选器,但是当我使用不止一个线程时,结果不一致。它必须能够通过的范围是1-2^32。当使用较小的范围(如1-1024)运行时,通常会得到正确的素数,但随着范围变大,误差幅度也会变大。我假设这是一个竞争条件,因为我不使用互斥体(而且我更愿意不使用互斥体,因为程序的设置不需要互斥体),或者我向线程函数发送数据的方式有问题。我仍然习惯C++和指针/引用。它找到的素数总是大于或等于实际数,而不是小于。对于复合数字,位图中位的索引设置为1。这可能只是我由于缺乏C/C++经验而忽略的一些愚蠢的小错误。如果我有什么不清楚的地方,请告诉我。谢谢你的关注 #include <iostream> #include <pthread.h> #include <string.h> #include <stdio.h> #include <stdlib.h> #include <sys/time.h> #include <sys/resource.h> #include <sched.h> #include <vector> #include <math.h> using namespace std; #define NUM_OF_THREADS 4 #define UPPER_LIMIT pow(2, 30) #define SQRT_UPPER_LIMIT sqrt(UPPER_LIMIT) typedef struct { int thread_index; unsigned long long prime; unsigned long long marked_up_to; pthread_t thread; bool thread_is_done; } thread_info_t; typedef struct { unsigned long long x; unsigned long long y; }indexes_t; static const unsigned long bitmasks[] = { 0x8000000000000000, 0x4000000000000000, 0x2000000000000000, 0x1000000000000000, 0x800000000000000, 0x400000000000000, 0x200000000000000, 0x100000000000000, 0x80000000000000, 0x40000000000000, 0x20000000000000, 0x10000000000000, 0x8000000000000, 0x4000000000000, 0x2000000000000, 0x1000000000000, 0x800000000000, 0x400000000000, 0x200000000000, 0x100000000000, 0x80000000000, 0x40000000000, 0x20000000000, 0x10000000000, 0x8000000000, 0x4000000000, 0x2000000000, 0x1000000000, 0x800000000, 0x400000000, 0x200000000, 0x100000000, 0x80000000, 0x40000000, 0x20000000, 0x10000000, 0x8000000, 0x4000000, 0x2000000, 0x1000000, 0x800000, 0x400000, 0x200000, 0x100000, 0x80000, 0x40000, 0x20000, 0x10000, 0x8000, 0x4000, 0x2000, 0x1000, 0x800, 0x400, 0x200, 0x100, 0x80, 0x40, 0x20, 0x10, 0x8, 0x4, 0x2, 0x1 }; clock_t start; clock_t stop; static unsigned long *bitmap; //array of longs static int bits_in_element = sizeof(unsigned long long)*8; static thread_info_t info[NUM_OF_THREADS]; indexes_t bit_indexes_from_number(unsigned long long number); void print_bitmap(); bool check_if_bit_index_is_prime(unsigned long long i, unsigned long long j); static void * threadFunc(void *arg) { thread_info_t *thread_info = (thread_info_t *)arg; unsigned long long prime = thread_info->prime; unsigned long long comp_number = prime+prime; int thread_index = thread_info->thread_index; indexes_t comp_index; for(; comp_number <= UPPER_LIMIT; comp_number += prime) // get rid of prime multiples { comp_index = bit_indexes_from_number(comp_number); bitmap[comp_index.x] |= bitmasks[comp_index.y]; info[thread_index].marked_up_to = comp_number; // so main thread only checks for primes past what's been marked } thread_info->thread_is_done = true; return NULL; } int main (int argc, char * argv[]) { long ncpus; double total_time; unsigned long long num_to_check; thread_info_t *thread_to_use; int thread_ret_val; start = clock(); for(int i = 0; i < NUM_OF_THREADS; i++) { info[i].thread_index = i; info[i].marked_up_to = 2; info[i].thread_is_done = true; } for(int i = 0; i < NUM_OF_THREADS; i++) { bitmap = (unsigned long *)malloc(sizeof(unsigned long *) * UPPER_LIMIT/8); bitmap[0] |= (bitmasks[0] | bitmasks[1]); } for(unsigned long long i = 0; i <= SQRT_UPPER_LIMIT/bits_in_element; i++)// go thru elements in array { for(unsigned long long j = (i == 0 ? 2 : 0); j < bits_in_element; j++) //go thru bits in elements { num_to_check = (i * bits_in_element) + j; //make sure all threads are past num_to_check for(int k = 0; ; k++) { if(k == NUM_OF_THREADS) k = 0; if(info[k].marked_up_to >= num_to_check) break; } if(check_if_bit_index_is_prime(i, j)) //check if bit index is prime { for(int k = 0; ; k++) //wait for a finished thread to use { if(k == NUM_OF_THREADS) k = 0; if(info[k].thread_is_done) { thread_to_use = &info[k]; info[k].thread_is_done = false; info[k].prime = (i * bits_in_element) + j; break; } } thread_ret_val = pthread_create(&thread_to_use->thread, NULL, threadFunc, (void *)thread_to_use); //thread gets rid of multiples if(thread_ret_val != 0) { cerr << "thread error: " << strerror(thread_ret_val) << "\n"; return -1; } } } } for(int i = 0; i < NUM_OF_THREADS; i++) { printf("waiting on %d\n", i); thread_ret_val = pthread_join(info[i].thread, NULL); if(thread_ret_val != 0) { cout << strerror(thread_ret_val); } } stop = clock(); total_time = (double)(stop - start) / (double)CLOCKS_PER_SEC; ncpus = sysconf(_SC_NPROCESSORS_ONLN); /* Print performance results */ printf ("Total time using %d threads : %.6f seconds\n", NUM_OF_THREADS, total_time / (NUM_OF_THREADS < ncpus ? NUM_OF_THREADS : ncpus)); print_bitmap(); return 1; } indexes_t bit_indexes_from_number(unsigned long long number) { indexes_t indexes; indexes.x = ceill(number / bits_in_element); //ceiling or not?? if(indexes.x == 0) indexes.y = number; else indexes.y = number - indexes.x*bits_in_element; return indexes; } void print_bitmap() { int count = 0; for(int i = 0; i <= UPPER_LIMIT; i++) { if(check_if_bit_index_is_prime(bit_indexes_from_number(i).x, bit_indexes_from_number(i).y)) { count++; } } cout << "number of primes between 1 and " << UPPER_LIMIT << ": " << count << "\n"; } bool check_if_bit_index_is_prime(unsigned long long i, unsigned long long j) { if(bitmap[i] & bitmasks[j]) return false; else return true; } #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 使用名称空间std; #定义\u线程的数量\u 4 #定义上限功率(2,30) #定义SQRT\u上限SQRT(上限) 类型定义结构{ int-thread_索引; 无符号长素数; 无符号长-长标记到; pthread\u t线程; 布尔线程已完成; }线程信息; 类型定义结构{ 无符号长x; 无符号长y; }指数; 静态常量无符号长位掩码[]={ 0x8000000000000000x40000000000x200000000000x10000000000000, 0x8000000000000、0x40000000000000、0x200000000000000、0x10000000000000、, 0x800000000000、0x40000000000000、0x20000000000000、0x10000000000000、, 0x80000000000,0x4000000000000,0x2000000000000,0x1000000000000, 0x8000000000,0x400000000000,0x2000000000000,0x1000000000000, 0x8000000000x40000000000x200000000000x10000000000, 0x800000000、0x4000000000、0x2000000000、0x100000000, 0x80000000、0x400000000、0x2000000000、0x100000000, 0x80000000、0x40000000、0x20000000、0x10000000, 0x8000000、0x4000000、0x2000000、0x1000000, 0x800000、0x400000、0x200000、0x100000、, 0x80000,0x40000,0x20000,0x10000, 0x8000、0x4000、0x2000、0x1000、, 0x800、0x400、0x200、0x100、, 0x80、0x40、0x20、0x10, 0x8、0x4、0x2、0x1 }; 时钟没有启动; 时钟不停; 静态无符号长*位图//长数组 元素中的静态int位=sizeof(无符号长)*8; 静态线程信息[线程数]; 索引\u t位\u索引来自\u编号(无符号长-长编号); 无效打印位图(); bool检查位索引是否为素数(无符号长i,无符号长j); 静态void*threadFunc(void*arg) { 线程信息*线程信息=(线程信息*)参数; 无符号长素数=线程信息->素数; 无符号长复数=素数+素数; int thread\u index=线程信息->线程索引; 综合指数; 对于(;comp_number_C++_Multithreading_Debugging_Pointers_Primes - Fatal编程技术网 素数; 无符号长复数=素数+素数; int thread\u index=线程信息->线程索引; 综合指数; 对于(;comp_number,c++,multithreading,debugging,pointers,primes,C++,Multithreading,Debugging,Pointers,Primes" /> 素数; 无符号长复数=素数+素数; int thread\u index=线程信息->线程索引; 综合指数; 对于(;comp_number,c++,multithreading,debugging,pointers,primes,C++,Multithreading,Debugging,Pointers,Primes" />

素数筛不总是在范围内得到正确的素数 我用C++中的ErasoTeSes筛选器使用多个线程来做素数筛选器,但是当我使用不止一个线程时,结果不一致。它必须能够通过的范围是1-2^32。当使用较小的范围(如1-1024)运行时,通常会得到正确的素数,但随着范围变大,误差幅度也会变大。我假设这是一个竞争条件,因为我不使用互斥体(而且我更愿意不使用互斥体,因为程序的设置不需要互斥体),或者我向线程函数发送数据的方式有问题。我仍然习惯C++和指针/引用。它找到的素数总是大于或等于实际数,而不是小于。对于复合数字,位图中位的索引设置为1。这可能只是我由于缺乏C/C++经验而忽略的一些愚蠢的小错误。如果我有什么不清楚的地方,请告诉我。谢谢你的关注 #include <iostream> #include <pthread.h> #include <string.h> #include <stdio.h> #include <stdlib.h> #include <sys/time.h> #include <sys/resource.h> #include <sched.h> #include <vector> #include <math.h> using namespace std; #define NUM_OF_THREADS 4 #define UPPER_LIMIT pow(2, 30) #define SQRT_UPPER_LIMIT sqrt(UPPER_LIMIT) typedef struct { int thread_index; unsigned long long prime; unsigned long long marked_up_to; pthread_t thread; bool thread_is_done; } thread_info_t; typedef struct { unsigned long long x; unsigned long long y; }indexes_t; static const unsigned long bitmasks[] = { 0x8000000000000000, 0x4000000000000000, 0x2000000000000000, 0x1000000000000000, 0x800000000000000, 0x400000000000000, 0x200000000000000, 0x100000000000000, 0x80000000000000, 0x40000000000000, 0x20000000000000, 0x10000000000000, 0x8000000000000, 0x4000000000000, 0x2000000000000, 0x1000000000000, 0x800000000000, 0x400000000000, 0x200000000000, 0x100000000000, 0x80000000000, 0x40000000000, 0x20000000000, 0x10000000000, 0x8000000000, 0x4000000000, 0x2000000000, 0x1000000000, 0x800000000, 0x400000000, 0x200000000, 0x100000000, 0x80000000, 0x40000000, 0x20000000, 0x10000000, 0x8000000, 0x4000000, 0x2000000, 0x1000000, 0x800000, 0x400000, 0x200000, 0x100000, 0x80000, 0x40000, 0x20000, 0x10000, 0x8000, 0x4000, 0x2000, 0x1000, 0x800, 0x400, 0x200, 0x100, 0x80, 0x40, 0x20, 0x10, 0x8, 0x4, 0x2, 0x1 }; clock_t start; clock_t stop; static unsigned long *bitmap; //array of longs static int bits_in_element = sizeof(unsigned long long)*8; static thread_info_t info[NUM_OF_THREADS]; indexes_t bit_indexes_from_number(unsigned long long number); void print_bitmap(); bool check_if_bit_index_is_prime(unsigned long long i, unsigned long long j); static void * threadFunc(void *arg) { thread_info_t *thread_info = (thread_info_t *)arg; unsigned long long prime = thread_info->prime; unsigned long long comp_number = prime+prime; int thread_index = thread_info->thread_index; indexes_t comp_index; for(; comp_number <= UPPER_LIMIT; comp_number += prime) // get rid of prime multiples { comp_index = bit_indexes_from_number(comp_number); bitmap[comp_index.x] |= bitmasks[comp_index.y]; info[thread_index].marked_up_to = comp_number; // so main thread only checks for primes past what's been marked } thread_info->thread_is_done = true; return NULL; } int main (int argc, char * argv[]) { long ncpus; double total_time; unsigned long long num_to_check; thread_info_t *thread_to_use; int thread_ret_val; start = clock(); for(int i = 0; i < NUM_OF_THREADS; i++) { info[i].thread_index = i; info[i].marked_up_to = 2; info[i].thread_is_done = true; } for(int i = 0; i < NUM_OF_THREADS; i++) { bitmap = (unsigned long *)malloc(sizeof(unsigned long *) * UPPER_LIMIT/8); bitmap[0] |= (bitmasks[0] | bitmasks[1]); } for(unsigned long long i = 0; i <= SQRT_UPPER_LIMIT/bits_in_element; i++)// go thru elements in array { for(unsigned long long j = (i == 0 ? 2 : 0); j < bits_in_element; j++) //go thru bits in elements { num_to_check = (i * bits_in_element) + j; //make sure all threads are past num_to_check for(int k = 0; ; k++) { if(k == NUM_OF_THREADS) k = 0; if(info[k].marked_up_to >= num_to_check) break; } if(check_if_bit_index_is_prime(i, j)) //check if bit index is prime { for(int k = 0; ; k++) //wait for a finished thread to use { if(k == NUM_OF_THREADS) k = 0; if(info[k].thread_is_done) { thread_to_use = &info[k]; info[k].thread_is_done = false; info[k].prime = (i * bits_in_element) + j; break; } } thread_ret_val = pthread_create(&thread_to_use->thread, NULL, threadFunc, (void *)thread_to_use); //thread gets rid of multiples if(thread_ret_val != 0) { cerr << "thread error: " << strerror(thread_ret_val) << "\n"; return -1; } } } } for(int i = 0; i < NUM_OF_THREADS; i++) { printf("waiting on %d\n", i); thread_ret_val = pthread_join(info[i].thread, NULL); if(thread_ret_val != 0) { cout << strerror(thread_ret_val); } } stop = clock(); total_time = (double)(stop - start) / (double)CLOCKS_PER_SEC; ncpus = sysconf(_SC_NPROCESSORS_ONLN); /* Print performance results */ printf ("Total time using %d threads : %.6f seconds\n", NUM_OF_THREADS, total_time / (NUM_OF_THREADS < ncpus ? NUM_OF_THREADS : ncpus)); print_bitmap(); return 1; } indexes_t bit_indexes_from_number(unsigned long long number) { indexes_t indexes; indexes.x = ceill(number / bits_in_element); //ceiling or not?? if(indexes.x == 0) indexes.y = number; else indexes.y = number - indexes.x*bits_in_element; return indexes; } void print_bitmap() { int count = 0; for(int i = 0; i <= UPPER_LIMIT; i++) { if(check_if_bit_index_is_prime(bit_indexes_from_number(i).x, bit_indexes_from_number(i).y)) { count++; } } cout << "number of primes between 1 and " << UPPER_LIMIT << ": " << count << "\n"; } bool check_if_bit_index_is_prime(unsigned long long i, unsigned long long j) { if(bitmap[i] & bitmasks[j]) return false; else return true; } #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 使用名称空间std; #定义\u线程的数量\u 4 #定义上限功率(2,30) #定义SQRT\u上限SQRT(上限) 类型定义结构{ int-thread_索引; 无符号长素数; 无符号长-长标记到; pthread\u t线程; 布尔线程已完成; }线程信息; 类型定义结构{ 无符号长x; 无符号长y; }指数; 静态常量无符号长位掩码[]={ 0x8000000000000000x40000000000x200000000000x10000000000000, 0x8000000000000、0x40000000000000、0x200000000000000、0x10000000000000、, 0x800000000000、0x40000000000000、0x20000000000000、0x10000000000000、, 0x80000000000,0x4000000000000,0x2000000000000,0x1000000000000, 0x8000000000,0x400000000000,0x2000000000000,0x1000000000000, 0x8000000000x40000000000x200000000000x10000000000, 0x800000000、0x4000000000、0x2000000000、0x100000000, 0x80000000、0x400000000、0x2000000000、0x100000000, 0x80000000、0x40000000、0x20000000、0x10000000, 0x8000000、0x4000000、0x2000000、0x1000000, 0x800000、0x400000、0x200000、0x100000、, 0x80000,0x40000,0x20000,0x10000, 0x8000、0x4000、0x2000、0x1000、, 0x800、0x400、0x200、0x100、, 0x80、0x40、0x20、0x10, 0x8、0x4、0x2、0x1 }; 时钟没有启动; 时钟不停; 静态无符号长*位图//长数组 元素中的静态int位=sizeof(无符号长)*8; 静态线程信息[线程数]; 索引\u t位\u索引来自\u编号(无符号长-长编号); 无效打印位图(); bool检查位索引是否为素数(无符号长i,无符号长j); 静态void*threadFunc(void*arg) { 线程信息*线程信息=(线程信息*)参数; 无符号长素数=线程信息->素数; 无符号长复数=素数+素数; int thread\u index=线程信息->线程索引; 综合指数; 对于(;comp_number

素数筛不总是在范围内得到正确的素数 我用C++中的ErasoTeSes筛选器使用多个线程来做素数筛选器,但是当我使用不止一个线程时,结果不一致。它必须能够通过的范围是1-2^32。当使用较小的范围(如1-1024)运行时,通常会得到正确的素数,但随着范围变大,误差幅度也会变大。我假设这是一个竞争条件,因为我不使用互斥体(而且我更愿意不使用互斥体,因为程序的设置不需要互斥体),或者我向线程函数发送数据的方式有问题。我仍然习惯C++和指针/引用。它找到的素数总是大于或等于实际数,而不是小于。对于复合数字,位图中位的索引设置为1。这可能只是我由于缺乏C/C++经验而忽略的一些愚蠢的小错误。如果我有什么不清楚的地方,请告诉我。谢谢你的关注 #include <iostream> #include <pthread.h> #include <string.h> #include <stdio.h> #include <stdlib.h> #include <sys/time.h> #include <sys/resource.h> #include <sched.h> #include <vector> #include <math.h> using namespace std; #define NUM_OF_THREADS 4 #define UPPER_LIMIT pow(2, 30) #define SQRT_UPPER_LIMIT sqrt(UPPER_LIMIT) typedef struct { int thread_index; unsigned long long prime; unsigned long long marked_up_to; pthread_t thread; bool thread_is_done; } thread_info_t; typedef struct { unsigned long long x; unsigned long long y; }indexes_t; static const unsigned long bitmasks[] = { 0x8000000000000000, 0x4000000000000000, 0x2000000000000000, 0x1000000000000000, 0x800000000000000, 0x400000000000000, 0x200000000000000, 0x100000000000000, 0x80000000000000, 0x40000000000000, 0x20000000000000, 0x10000000000000, 0x8000000000000, 0x4000000000000, 0x2000000000000, 0x1000000000000, 0x800000000000, 0x400000000000, 0x200000000000, 0x100000000000, 0x80000000000, 0x40000000000, 0x20000000000, 0x10000000000, 0x8000000000, 0x4000000000, 0x2000000000, 0x1000000000, 0x800000000, 0x400000000, 0x200000000, 0x100000000, 0x80000000, 0x40000000, 0x20000000, 0x10000000, 0x8000000, 0x4000000, 0x2000000, 0x1000000, 0x800000, 0x400000, 0x200000, 0x100000, 0x80000, 0x40000, 0x20000, 0x10000, 0x8000, 0x4000, 0x2000, 0x1000, 0x800, 0x400, 0x200, 0x100, 0x80, 0x40, 0x20, 0x10, 0x8, 0x4, 0x2, 0x1 }; clock_t start; clock_t stop; static unsigned long *bitmap; //array of longs static int bits_in_element = sizeof(unsigned long long)*8; static thread_info_t info[NUM_OF_THREADS]; indexes_t bit_indexes_from_number(unsigned long long number); void print_bitmap(); bool check_if_bit_index_is_prime(unsigned long long i, unsigned long long j); static void * threadFunc(void *arg) { thread_info_t *thread_info = (thread_info_t *)arg; unsigned long long prime = thread_info->prime; unsigned long long comp_number = prime+prime; int thread_index = thread_info->thread_index; indexes_t comp_index; for(; comp_number <= UPPER_LIMIT; comp_number += prime) // get rid of prime multiples { comp_index = bit_indexes_from_number(comp_number); bitmap[comp_index.x] |= bitmasks[comp_index.y]; info[thread_index].marked_up_to = comp_number; // so main thread only checks for primes past what's been marked } thread_info->thread_is_done = true; return NULL; } int main (int argc, char * argv[]) { long ncpus; double total_time; unsigned long long num_to_check; thread_info_t *thread_to_use; int thread_ret_val; start = clock(); for(int i = 0; i < NUM_OF_THREADS; i++) { info[i].thread_index = i; info[i].marked_up_to = 2; info[i].thread_is_done = true; } for(int i = 0; i < NUM_OF_THREADS; i++) { bitmap = (unsigned long *)malloc(sizeof(unsigned long *) * UPPER_LIMIT/8); bitmap[0] |= (bitmasks[0] | bitmasks[1]); } for(unsigned long long i = 0; i <= SQRT_UPPER_LIMIT/bits_in_element; i++)// go thru elements in array { for(unsigned long long j = (i == 0 ? 2 : 0); j < bits_in_element; j++) //go thru bits in elements { num_to_check = (i * bits_in_element) + j; //make sure all threads are past num_to_check for(int k = 0; ; k++) { if(k == NUM_OF_THREADS) k = 0; if(info[k].marked_up_to >= num_to_check) break; } if(check_if_bit_index_is_prime(i, j)) //check if bit index is prime { for(int k = 0; ; k++) //wait for a finished thread to use { if(k == NUM_OF_THREADS) k = 0; if(info[k].thread_is_done) { thread_to_use = &info[k]; info[k].thread_is_done = false; info[k].prime = (i * bits_in_element) + j; break; } } thread_ret_val = pthread_create(&thread_to_use->thread, NULL, threadFunc, (void *)thread_to_use); //thread gets rid of multiples if(thread_ret_val != 0) { cerr << "thread error: " << strerror(thread_ret_val) << "\n"; return -1; } } } } for(int i = 0; i < NUM_OF_THREADS; i++) { printf("waiting on %d\n", i); thread_ret_val = pthread_join(info[i].thread, NULL); if(thread_ret_val != 0) { cout << strerror(thread_ret_val); } } stop = clock(); total_time = (double)(stop - start) / (double)CLOCKS_PER_SEC; ncpus = sysconf(_SC_NPROCESSORS_ONLN); /* Print performance results */ printf ("Total time using %d threads : %.6f seconds\n", NUM_OF_THREADS, total_time / (NUM_OF_THREADS < ncpus ? NUM_OF_THREADS : ncpus)); print_bitmap(); return 1; } indexes_t bit_indexes_from_number(unsigned long long number) { indexes_t indexes; indexes.x = ceill(number / bits_in_element); //ceiling or not?? if(indexes.x == 0) indexes.y = number; else indexes.y = number - indexes.x*bits_in_element; return indexes; } void print_bitmap() { int count = 0; for(int i = 0; i <= UPPER_LIMIT; i++) { if(check_if_bit_index_is_prime(bit_indexes_from_number(i).x, bit_indexes_from_number(i).y)) { count++; } } cout << "number of primes between 1 and " << UPPER_LIMIT << ": " << count << "\n"; } bool check_if_bit_index_is_prime(unsigned long long i, unsigned long long j) { if(bitmap[i] & bitmasks[j]) return false; else return true; } #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 使用名称空间std; #定义\u线程的数量\u 4 #定义上限功率(2,30) #定义SQRT\u上限SQRT(上限) 类型定义结构{ int-thread_索引; 无符号长素数; 无符号长-长标记到; pthread\u t线程; 布尔线程已完成; }线程信息; 类型定义结构{ 无符号长x; 无符号长y; }指数; 静态常量无符号长位掩码[]={ 0x8000000000000000x40000000000x200000000000x10000000000000, 0x8000000000000、0x40000000000000、0x200000000000000、0x10000000000000、, 0x800000000000、0x40000000000000、0x20000000000000、0x10000000000000、, 0x80000000000,0x4000000000000,0x2000000000000,0x1000000000000, 0x8000000000,0x400000000000,0x2000000000000,0x1000000000000, 0x8000000000x40000000000x200000000000x10000000000, 0x800000000、0x4000000000、0x2000000000、0x100000000, 0x80000000、0x400000000、0x2000000000、0x100000000, 0x80000000、0x40000000、0x20000000、0x10000000, 0x8000000、0x4000000、0x2000000、0x1000000, 0x800000、0x400000、0x200000、0x100000、, 0x80000,0x40000,0x20000,0x10000, 0x8000、0x4000、0x2000、0x1000、, 0x800、0x400、0x200、0x100、, 0x80、0x40、0x20、0x10, 0x8、0x4、0x2、0x1 }; 时钟没有启动; 时钟不停; 静态无符号长*位图//长数组 元素中的静态int位=sizeof(无符号长)*8; 静态线程信息[线程数]; 索引\u t位\u索引来自\u编号(无符号长-长编号); 无效打印位图(); bool检查位索引是否为素数(无符号长i,无符号长j); 静态void*threadFunc(void*arg) { 线程信息*线程信息=(线程信息*)参数; 无符号长素数=线程信息->素数; 无符号长复数=素数+素数; int thread\u index=线程信息->线程索引; 综合指数; 对于(;comp_number,c++,multithreading,debugging,pointers,primes,C++,Multithreading,Debugging,Pointers,Primes,主要问题是您的线程信息。您需要确保此结构中成员更改的原子性。这不是原子性的事实最有可能导致您出现过多素数的问题。确保这些操作是原子性的将依赖于c++11的std::atomic或特定于平台的detai当然,你可以使用锁来确保一次只有一个线程接触一个线程信息 以下代码也存在一些问题: bitmap = (unsigned long *)malloc(sizeof(unsigned long *) * UPPER_LIMIT/8); bitmap[0] |= (bitmasks[0] | bi

主要问题是您的线程信息。您需要确保此结构中成员更改的原子性。这不是原子性的事实最有可能导致您出现过多素数的问题。确保这些操作是原子性的将依赖于c++11的std::atomic或特定于平台的detai当然,你可以使用锁来确保一次只有一个线程接触一个线程信息


以下代码也存在一些问题:

 bitmap = (unsigned long *)malloc(sizeof(unsigned long *) * UPPER_LIMIT/8);
 bitmap[0] |= (bitmasks[0] | bitmasks[1]);
这段代码中有两个错误。首先,内存泄漏,除了最后一个之外的所有分配都丢失,没有指向它的指针,也不可能释放


其次,为位图分配内存后,数据未定义。因此位图[0]|=(位掩码[0]|位掩码[1])作为位图[0]无效有一个未定义的值。

啊,我不相信……我之前尝试过使用位图数组,但忘了将该循环取出……但我不明白为什么线程信息是一个问题,因为每个线程在数组中都有它自己的特定副本,一次只有一个线程更改它的内容:“拥有”的线程它会在threadFunc中更改它,而主线程会在主线程中更改它,但只有在threadFunc完成后才更改?看起来像是在线程函数周围放置了互斥锁修复了它。仍然不确定为什么…?@FrederickCraine我相当肯定编译器在重新排序指令时有很大的回旋余地。更不用说延迟了对于要跨线程传播的内容,等等。最好确保它的安全,并使用互斥锁或保证原子的东西。这不仅仅是编译器。硬件可以根据其核心内容重新排序读写到达内存的方式。