在写c/c++时如何降低cpu使用率

wgf3341869 2013-08-21 05:02:25
我写了一个程序C/C++程序,单线程运行时没有问题,当多线程并发运行时发现cpu很快就满了,也不知道原因是什么,不知道怎么改代码,各位高手给点意见,是不是需要贴代码上去才能看清楚。
...全文
977 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
东莞某某某 2013-08-27
  • 打赏
  • 举报
回复
是不是死循环 不行的话 是不是要计算占用CPU时间来锁定
wgf3341869 2013-08-27
  • 打赏
  • 举报
回复
至于src的数据是用opencv读取的,目前主要是jpeg,即是调用函数IplImage *src = cvLoadImage(filename,-1);
wgf3341869 2013-08-27
  • 打赏
  • 举报
回复
int inter_linear_jpeg(IplImage *src,IplImage *dst,char *outputfilename,int quality){ if(src==NULL){ printf("can not open source file!"); return -1; } FILE *outfile; if((outfile = fopen(outputfilename,"wb"))==NULL){ printf( "can not open file to output %s/n", outputfilename); return -1; } //variable for jpeg start--------------- struct jpeg_compress_struct cinfo; struct jpeg_error_mgr jerr; int row_stride; /* physical row width in image buffer */ JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */ cinfo.err = jpeg_std_error(&jerr); jpeg_create_compress(&cinfo); jpeg_stdio_dest(&cinfo, outfile); cinfo.image_width = dst->width; /* image width and height, in pixels */ cinfo.image_height = dst->height; cinfo.input_components = dst->nChannels; /* # of color components per pixel */ /* colorspace of input image */ if(dst->nChannels==3) cinfo.in_color_space = JCS_RGB; else cinfo.in_color_space =JCS_GRAYSCALE; jpeg_set_defaults(&cinfo); jpeg_set_quality(&cinfo, quality, TRUE); jpeg_start_compress(&cinfo, TRUE); //variable for jpeg end--------------- //variable for inter_linear start------------- int i,j,k,chanel=src->nChannels,t; int si,sj,si1,sj1; double wk=(double)(src->width)/dst->width,hk=(double)(src->height)/dst->height; double u,v,th,tw; uchar* pst=(uchar*)dst->imageData; uchar* prc=(uchar*)src->imageData; //variable for inter_linear end------------------ //scaling and compressing for(i=th=0;i<dst->height;i++){ si=(int)th; si1=min(si+1,src->height-1); u=th-si; for(j=tw=t=0;j<dst->width;j++){ sj=(int)tw; sj1=min(sj+1,src->width-1); v=tw-sj; int ij=src->widthStep*si+sj*chanel,i1j=src->widthStep*si1+sj*chanel,ij1=src->widthStep*si+sj1*chanel,i1j1=src->widthStep*si1+sj1*chanel; for(k=0;k<chanel;k++){ pst[t+chanel-k-1]=(uchar)(prc[ij+k]+u*(prc[ij1+k]-prc[ij+k])+v*(prc[i1j+k]-prc[ij+k])+u*v*(prc[ij+k]-prc[i1j+k]-prc[ij1+k]+prc[i1j1+k])); } tw+=wk; t+=chanel; } row_pointer[0]=(unsigned char*)pst; (void) jpeg_write_scanlines(&cinfo, row_pointer, 1); pst=(uchar*)(pst+dst->widthStep); th+=hk; } //release jpeg_finish_compress(&cinfo); fclose(outfile); jpeg_destroy_compress(&cinfo); return 1; }
wgf3341869 2013-08-27
  • 打赏
  • 举报
回复
没关系,我贴代码
Carl_CCC 2013-08-26
  • 打赏
  • 举报
回复
先查查死循环的地方是不是有问题,比如while循环空转啊。
cpp_crab 2013-08-26
  • 打赏
  • 举报
回复
或者程序确实需要不停计算,多线程把CPU耗光。
cpp_crab 2013-08-26
  • 打赏
  • 举报
回复
显然是bug。
wgf3341869 2013-08-26
  • 打赏
  • 举报
回复
没有死循环,不过我听说浮点运算很耗cpu是吧
max_min_ 2013-08-22
  • 打赏
  • 举报
回复
开线程的后部分代码应该写的有问题!贴上来悄悄
wgf3341869 2013-08-22
  • 打赏
  • 举报
回复
不是的,是测试人员多线程并发请求的时候才会出现那个现象,在写的时候从来没有考虑过线程问题。而且我想说代码的依赖包很多就算贴上来也是运行不了的,依赖太多的运行环境。
qq120848369 2013-08-21
  • 打赏
  • 举报
回复
代码有BUG才会这样, 找问题吧.

23,216

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧