C++多线程求数组中3的数目,但是程序为什么会经常出现段错误。

C++多线程求数组中3的数目,但是程序为什么会经常出现段错误。

问题描述:

#include<iostream>
#include<pthread.h> 
#include<ctime>
#include<windows.h> 
using namespace std ;
// 33554432 正确3的个数 
const int M = 1048576 ; // 一M大小 
const int THREAD_NUM = 4 ; // 线程个数 

int count3 = 0 ; // 3的个数 
// 计算3的个数 
void *caculate3Count(void *threadId) ;
// 数组初始化 
int* init_array(int length) ;

int length = 0 ;
int *array ;
int sizePerThread = 0 ; // 每个线程需要遍历的大小 
// 主函数 
int main(){
    length  = M * 256 / sizeof(int) ; // 数组大小
    sizePerThread = length / THREAD_NUM ; 
    array = init_array(length) ; 
      pthread_t tids[THREAD_NUM]  ;
      int indexes[THREAD_NUM];// 用数组来保存i的值
      cout << "四个线程分工:" << endl ; 
    for (int i=1;i<=THREAD_NUM;i++){
        cout << (i-1) * sizePerThread << "~" << ((i-1) * sizePerThread + sizePerThread) << endl ;
    }
      for (int i=1;i<=THREAD_NUM;i++){
          indexes[i] = i ;
//        int temp = i ;
          int ret = pthread_create(&tids[i],NULL,caculate3Count,(void *)indexes[i]) ;    
//        Sleep(100);
          if (ret!=0){
              cout << "pthread_create error: error_code=" << ret << endl ;
        }
    }
    //等各个线程退出后,进程才结束,否则进程强制结束了,线程可能还没反应过来;
//    cout << "3的个数:" <<count3 << endl ;
    pthread_exit(NULL);
    return 0 ; 
}

// 计算3的个数 
void *caculate3Count(void *threadId){
    int tid = *((int*)threadId) ;
    int beginIndex = (tid - 1) * sizePerThread ;
    clock_t start ,end ; 
    start = clock() ;
    // 计数阶段 start 
    for (int i=beginIndex;i<beginIndex+sizePerThread;i++){
        if (array[i]==3){
            count3 ++ ;
        }
    }
    // 计数阶段 end 
    end = clock() ;
    // 本线程耗时 
    double endtime=(double)(end-start)/CLOCKS_PER_SEC;
    cout << "线程" << tid << endl ;
    cout<<"Total time:" << endtime << endl;        //s为单位
    cout<<"Total time:" << endtime*1000 <<"ms"<<endl;    //ms为单位
    cout << "当前3的个数" << count3 << endl << endl << endl  ;
    pthread_exit(NULL);
}

// 数组初始化 
int* init_array(int length){
    int *array = new int[length] ;
    array[0] = 0 ;
    for (int i=0;i<length;i++){
        array[i] = (array[i-1]==0)?3:0 ;
    }
    return array ;
}

pthread_create传值的时候,最后一个参数应该是地址,而非变量的值。试想,如果变量为-1,或者0,请问有这样的地址吗?可以直接在子线程中访问吗?

建议再去看一下pthread_create man手册:

修改方法:将要传参数值indexes[i],修改为传地址&indexes[i]

// 原始代码
          int ret = pthread_create(&tids[i],NULL,caculate3Count,(void *)indexes[i]) ;    
// 修改后
          int ret = pthread_create(&tids[i],NULL,caculate3Count,(void *)&indexes[i]) ;    

另外,你没有觉得你的程序所包含的头文件很奇葩嚒?
pthread是linux线程库,然而你的头文件却包含了MSVC的windows.h。你的程序似乎也并没有用到Windows.h里面的内容。

段错误,一般都是空指针,或者没有初始化