【发布时间】:2012-09-25 14:41:34
【问题描述】:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <stdint.h>
#include <inttypes.h>
typedef struct tmp_num{
int tmp_1;
int tmp_2;
}t_num;
/* Dichiarazione prototipi secondo le direttive di pthread */
void *num_mezzo_1(void *num_orig);
void *num_mezzo_2(void *num_orig);
int main(int argc, char *argv[]){
/* Inizializzo i 2 thread */
pthread_t thread1, thread2;
int tmp=0,rc1,rc2,num;
t_num main_tnum;
num=atoi(argv[1]);
if(num <= 3){
printf("Questo è un numero primo: %d\n", num);
exit(0);
}
if( (rc1=pthread_create( &thread1, NULL, &num_mezzo_1, (void *)num)) ){
printf("Creazione del thread fallita: %d\n", rc1);
exit(1);
}
if( (rc2=pthread_create( &thread2, NULL, &num_mezzo_2, (void *)num)) ){
printf("Creazione del thread fallita: %d\n", rc2);
exit(1);
}
main_tnum.tmp_1 = 0;
main_tnum.tmp_2 = 0;
pthread_join(thread1, (void **)&(main_tnum.tmp_1));
pthread_join(thread2, (void **)&(main_tnum.tmp_2));
tmp=main_tnum.tmp_1+main_tnum.tmp_2;
if(tmp>2){
printf("Questo NON è un numero primo: %d\n", num);
}
else{
printf("Questo è un numero primo: %d\n", num);
}
exit(0);
}
void *num_mezzo_1(void *num_orig){
t_num p1_tnum;
int cont_1;
int n_orig=(int)num_orig;
p1_tnum.tmp_1 = 0;
for(cont_1=1; cont_1<=(n_orig/2); cont_1++){
if((n_orig % cont_1) == 0){
(p1_tnum.tmp_1)++;
}
}
pthread_exit((void *)(p1_tnum.tmp_1));
return NULL;
}
void *num_mezzo_2(void *num_orig){
t_num p2_tnum;
int cont_2;
int n_orig=(int)num_orig;
p2_tnum.tmp_2 = 0;
for(cont_2=((n_orig/2)+1); cont_2<=n_orig; cont_2++){
if((n_orig % cont_2) == 0){
(p2_tnum.tmp_2)++;
}
}
pthread_exit((void *)(p2_tnum.tmp_2));
return NULL;
}
我正在使用 Debian Sid 64 位,但我开发的这个简单程序有问题。
如果我使用 clang -Wall -Wextra -o test prime.c -lpthread 编译,我只收到 1 个警告(unused argc)并且程序运行良好。
但是如果我用 GCC-4.7.2 (gcc -Wall -Wextra -o test prime.c -lpthread) 编译它,我会得到 more 警告(unused argc + 各种 从指针到不同大小的整数的转换) 并且程序因 SegFault 而失败。
我不明白为什么使用 clang 它可以工作,而使用 GCC 却不行。
编辑:
num_primo_pthread.c: In function ‘main’:
num_primo_pthread.c:33:57: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
num_primo_pthread.c:37:57: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
num_primo_pthread.c:16:14: warning: unused parameter ‘argc’ [-Wunused-parameter]
num_primo_pthread.c: In function ‘num_mezzo_1’:
num_primo_pthread.c:61:13: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
num_primo_pthread.c:68:15: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
num_primo_pthread.c: In function ‘num_mezzo_2’:
num_primo_pthread.c:75:13: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
num_primo_pthread.c:82:15: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
【问题讨论】:
-
发布您的警告。这可能是因为 clang 正在修复一些简单的错误,但 gcc 为您提供了所有需要的线程。
-
另外:最好始终消除警告。他们在那里让你知道可能的危险情况。它们通常仍会编译,因为最好和最有经验的程序员有时会以编译器看不到的方式弯曲语言,但 90+% 的时间,你应该消除它们。从长远来看,你会感谢自己。我有一位大学教授,如果你的代码编译时出现警告,他会打分。有时,我希望所有教授都这样做。
-
一般来说,如果你不明白他们在做什么,就不要使用演员表。此外,这里的编译器会告诉你到底哪里出了问题,所以要解决这个问题,而不是仅仅在脚上开枪。 clang 没有检测到这个基本错误的事实令人惊讶。我不确定您是否正在为完全相同的架构进行编译。
-
在 Debian Wheezy 64 位上使用 gcc 4.7.1,它可以工作
-
@OttavioCampana O.o 天哪!太不可思议了:O