fangps
驱动牛犊
驱动牛犊
  • 注册日期2004-05-31
  • 最后登录2004-12-11
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
阅读:1274回复:3

为什么我的多线程应用程序效率这么差,那位指点一下?

楼主#
更多 发布于:2004-12-10 10:01
我在USB的接收程序中,一个线程用于读数据操作,一个线程用于把读取的数据存入硬盘。
但是我发觉这样的应用程序速度竟然比只有一个主线程(既读取数据后就存入硬盘)慢了很多

多线程实现方式是:用BC中的Thread Object实现的,实现读写功能的程序都是在线程的Execute函数中,其他关于线程的设置都是默认的,除了线程优先级外。
多优先级进行一些设置,感觉效果都不好

我想理论这样多线程的应用程序肯定比单线程快,但是我觉不知道为什么我的程序就慢了这么多,感觉是效率问题,但是不知道问题出在那?

那位能指点一下,本人不胜感激!~~~~~~~~~
xx_focus
驱动中牛
驱动中牛
  • 注册日期2003-08-06
  • 最后登录2007-06-08
  • 粉丝0
  • 关注0
  • 积分96分
  • 威望10点
  • 贡献值0点
  • 好评度8点
  • 原创分0分
  • 专家分0分
沙发#
发布于:2004-12-10 11:44
把你的线程模板代码贴出来(去掉你自己的代码),或许会好些
fangps
驱动牛犊
驱动牛犊
  • 注册日期2004-05-31
  • 最后登录2004-12-11
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
板凳#
发布于:2004-12-10 15:41
我的线程代码:
这两个线程之间有冲突

//----------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit2.h"
#include "Unit1.h"
#include "Unit3.h"
#include <fstream.h>
#include <iostream.h>
#include "stdio.h"
#pragma package(smart_init)
//----------------------------------------------------------

//   Important: Methods and properties of objects in VCL can only be
//   used in a method called using Synchronize, for example:
//
//      Synchronize(UpdateCaption);
//
//   where UpdateCaption could look like:
//
//      void __fastcall SecThread::UpdateCaption()
//      {
//        Form1->Caption = "Updated in a thread";
//      }

        extern int rcount;
        extern int scount;
        extern unsigned char Inbuf[16][65536];
        extern long leng[16];
        extern long Inlength ;
        extern bool noover ;
        extern int rnum ;
        extern int snum ;


//---------------------------------------------------------

__fastcall SecThread::SecThread(bool CreateSuspended)
        : TThread(CreateSuspended)
{
        FreeOnTerminate=true;
        Priority = tpNormal;
}
//---------------------------------------------------------------------------
void __fastcall SecThread::Execute()
{
        //---- Place thread code here ----
        
        SData();//实现功能的函数
}
//----------------------------------------------------------



//----------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit3.h"
#include "Unit1.h"
#include "Unit2.h"
#include "CyAPI.h"
#include "dbt.h"
#pragma package(smart_init)
//----------------------------------------------------------

//   Important: Methods and properties of objects in VCL can only be
//   used in a method called using Synchronize, for example:
//
//      Synchronize(UpdateCaption);
//
//   where UpdateCaption could look like:
//
//      void __fastcall ThrThread::UpdateCaption()
//      {
//        Form1->Caption = "Updated in a thread";
//      }

extern HANDLE hDevice  ;
extern CCyBulkEndPoint *BulkInEpt ;
extern CCyBulkEndPoint *BulkOutEpt ;
extern CCyBulkEndPoint *BulkInEpt6 ;

        extern int rcount;
        extern int scount;
        extern unsigned char Inbuf[16][65536];
        extern long leng[16];
        extern long Inlength ;
        extern bool noover ;
        extern int rnum ;
        extern int snum ;
//----------------------------------------------------------

__fastcall ThrThread::ThrThread(bool CreateSuspended)
        : TThread(CreateSuspended)
{
        FreeOnTerminate=true;
        Priority = tpNormal;
}
//----------------------------------------------------------
void __fastcall ThrThread::Execute()
{
        //---- Place thread code here ----
        RData();//实现功能的函数
}

denglide
驱动牛犊
驱动牛犊
  • 注册日期2003-12-06
  • 最后登录2021-01-08
  • 粉丝0
  • 关注0
  • 积分8分
  • 威望22点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
  • 社区居民
地板#
发布于:2004-12-10 18:42
在读写USB循环中的适当位置加Sleep函数,提供一些时间给系统使用,
就可能解决了
游客

返回顶部