|
多线程的结果错的
354053835
389900230
394458474
393939720
394399987
394452486
394459235
394459157
=
3110123124
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#define NUM_THREADS 8
void *calc(void *threadid)
{
char s[10][10]={"0","11","222","3333","44444","555555","6666666","77777777","888888888"};
char s1[11];
char *st;
long x,i,c=0;
for(x=1111111111L*((int)threadid+1);x<1111111111L*((int)threadid+2);x++)
{
sprintf(s1,"%ld",x);
for(i=0;i<9;i++)
{
if ((st=strstr(s1,s[ i]))!=NULL){x+=((long)pow(10,10-(st-s1)-i-1)-1);goto end;}//continue;
}
c++;
end: ;
}
printf("%ld\n",c);
pthread_exit(NULL);
}
int main (int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
int rc, t;
for(t=0; t<NUM_THREADS; t++){
printf("In main: creating thread %d\n", t);
rc = pthread_create(&threads[t], NULL, calc, (void *)t);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
pthread_exit(NULL);
}
|
|