芙门泡芙 2021-12-13 23:26 采纳率: 50%
浏览 18
已结题

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 ;
}

  • 写回答

2条回答 默认 最新

  • fortunely2 2021-12-13 23:47
    关注

    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里面的内容。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 12月21日
  • 已采纳回答 12月13日
  • 创建了问题 12月13日

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集