为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

[专题]一个有错误的多线程求孪生素数的C程序(from github)

2017-11-13 6页 doc 18KB 10阅读

用户头像

is_005190

暂无简介

举报
[专题]一个有错误的多线程求孪生素数的C程序(from github)[专题]一个有错误的多线程求孪生素数的C程序(from github) 一个有错误的多线程求孪生素数的C程序(from github) #include #include #include #include #include #include #include #include #include #include static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; struct arg_struct { unsigned int *array...
[专题]一个有错误的多线程求孪生素数的C程序(from github)
[专题]一个有错误的多线程求孪生素数的C程序(from github) 一个有错误的多线程求孪生素数的C程序(from github) #include #include #include #include #include #include #include #include #include #include static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; struct arg_struct { unsigned int *array; int numthred; unsigned int thredin; unsigned int total; }; void setBit(unsigned int a[], int k){ int i= k/32; int pos = k%32; int flag = 1; flag = flag << pos; a[i] = a[i] | flag; } void clearBit(unsigned int a[], int k){ int i = k/32; int pos = k%32; int flag = 1; flag = flag << pos; flag = ~flag; a[i] = a[i] & flag; } int testBit(unsigned int a[], int k){ int i= k/32; int pos = k%32; int flag = 1; flag = flag << pos; if (a[i] & flag) return 1; return 0; } void multiples(unsigned int *array, unsigned int base, unsigned int total){ int i; if ((base == 0) || (base == 1)){ return; } else { for (i = base + 1; i < total; i++){ if (testBit(array, i) == 0){ if (i % base == 0){ setBit(array, i); } } } } } void * threadFunc(void *arguments){ struct arg_struct *args = arguments; int i, s; for (i = 2; i < args->total; i++){ if (i % args->numthred == args->thredin){ s = pthread_mutex_lock(&mtx); if (s != 0){ perror("mutex not locked"); exit(EXIT_FAILURE); } multiples(args->array, i, args->total); s = pthread_mutex_unlock(&mtx); if (s != 0){ perror("mutex not unlocked"); exit(EXIT_FAILURE); } } } return NULL; } int main(int argc, char **argv){ int c; int print = 1; int status; extern char *optarg; extern int optind, optopt; unsigned int totbits = 1000; int numthred = 2; unsigned int *bitArray; unsigned int size = ((totbits / 32)) + 1; int i; bitArray = malloc((sizeof(unsigned int)) * size); //getopts for numthred/totbits/quiet while ((c = getopt(argc, argv, "m:c:q")) != -1) { switch (c){ case 'm': totbits = atol(optarg); break; case 'c': numthred =atoi(optarg); break; case 'q': print = 0; break; } } //intitialize bit array for (i = 0; i < size; i++){ bitArray[i] = 0; } //make and initialize threads pthread_t thread[numthred]; struct arg_struct args[numthred]; for(i = 0; i < numthred; i++){ int s; args[i].array = bitArray; args[i].numthred = numthred; args[i].thredin = i; args[i].total = totbits; s = pthread_create(&thread[i], NULL, threadFunc, (void *)&args[i]); if (s != 0){ perror("thread create error"); exit(EXIT_FAILURE); } } for (i = 0; i < numthred; i++) pthread_join(thread[i], (void*)&status); if (print > 0){ for (i = 4; i < totbits; i++){ if ((testBit(bitArray, i - 2) == 0) && (testBit(bitArray, i) == 0)){ printf("%d, %d\n", i-2, i); } } } exit(EXIT_SUCCESS); }
/
本文档为【[专题]一个有错误的多线程求孪生素数的C程序&#40;from github&#41;】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索