我打算用5个线程同时传输文件数据,也就是将文件拆成5个部分,然后当这5个线程都结束时,在消息响应里将文件合并。但是过程中遇到问题了。
这个5个线程是利用同一个回调函数,但是都传入了不同的指针,这5个指针都是new出来的。
m_multiple[0]=CreateThread(NULL,0,ThreadMultiple,(LPVOID)bigmsg,0,&a);
m_multiple[1]=CreateThread(NULL,0,ThreadMultiple,(LPVOID)bigmsga,0,&b);
                  m_multiple[2]=CreateThread(NULL,0,ThreadMultiple,(LPVOID)bigmsgb,0,&c);
m_multiple[3]=CreateThread(NULL,0,ThreadMultiple,(LPVOID)bigmsgc,0,&d);
m_multiple[4]=CreateThread(NULL,0,ThreadMultiple,(LPVOID)bigmsgd,0,&e);
数据理应没有冲突,在线程函数中,我觉得没有同步的问题。但是我发现每次只有3个线程或4个线程传输了数据,另外两个或一个是没有工作的。真奇怪!!!!!!!!!!为什么,大家有遇到过吗

解决方案 »

  1.   

    你用socket网络传输?先说清楚一点,你只说你传输文件数据,到底线程是怎么干的?
    你贴出的代码看不出什么
      

  2.   

    对,就是socket,线程1读取文件A的5分之1处,线程2读取文件A的5分之2处,线程3读取文件A的5分之3处     长度都是文件长度的5分之1DWORD WINAPI CFiletranDlg::ThreadMultiple(LPVOID lp)
    {
    MsgCommand *msg=(MsgCommand*)lp;   //传入文件段长度,偏移,目标文件IP
    MsgCommandB command;          //发送给服务器的命令,里面包括文件段长度,偏移等等
    ZeroMemory(&command,sizeof(MsgCommandB));
    SOCKET sock=socket(AF_INET,SOCK_STREAM,0);     //
    SOCKADDR_IN tcpaddr;
    tcpaddr.sin_addr.S_un.S_addr=msg->DesIp;
    tcpaddr.sin_family=AF_INET;
    tcpaddr.sin_port=htons(5556);
    int len =sizeof(SOCKADDR);
    int sendrt,recvrt;
    char tempbuf[1024*8]={0};
    CFile file;
    CString strclientlocation=msg->clientlocation;
    int count=msg->fileoffset/msg->filelength;       //计算出段数
    int fileoffset=0;
    int filelength=msg->filelength;

    if(connect(sock,(SOCKADDR*)&tcpaddr,len)!=SOCKET_ERROR)
    {
    memset(command.msgcommand,0,4);     //标识位
    command.filelength=(msg->filelength)*(count+1);  //得出文件段的长度
    memcpy(command.location,msg->location,128);   //服务器文件的位置
    command.fileoffset=msg->fileoffset;    //文件段的偏移
    sendrt=send(sock,(const char*)&command,sizeof(MsgCommandB),0);
    BOOL bResult = file.Open(strclientlocation.GetBuffer(0), CFile::modeWrite|CFile::modeNoTruncate, NULL);//打开一个已存在的文件
    if(sendrt<=0)
    {
    AfxMessageBox("sending error");
    return 0;
    }
    else
    {
    while(bResult&&fileoffset<filelength)   
    {
    recvrt=recv(sock,tempbuf,1024*8,0);
    if(recvrt>0)
    {
    if(fileoffset+recvrt>filelength)
    {
    recvrt=filelength-fileoffset;
    }
    else
    { file.Seek(fileoffset,CFile::begin);
    file.Write(tempbuf,recvrt);


    fileoffset+=recvrt;
    }


    }
    else
    {

    break;
    } }
    }    file.Close(); 
    }   //delete msg;
    return 0;}
      

  3.   

    线程1  recv socket----》  send  服务器文件A  5分之0处,长度为总长度的5分之一
    线程2  recv socket----》  send  服务器文件A  5分之1处,长度为总长度的5分之一
    线程3  recv socket----》  send  服务器文件A  5分之2处,长度为总长度的5分之一
    线程4  recv socket----》  send  服务器文件A  5分之3处,长度为总长度的5分之一
    线程5  recv socket----》  send  服务器文件A  5分之4处,长度为总长度的5分之一5个线程都调用了此函数DWORD WINAPI CFiletranDlg::ThreadMultiple(LPVOID lp)
    传入的lp都是不同的
      

  4.   


    /*数据理应没有冲突,在线程函数中,我觉得没有同步的问题。但是我发现每次只有3个线程或4个线程传输了数据,另外两个或一个是没有工作的。
    */
    // I think the reason is : CPU can't switch thread contex so quickly in time ?
      

  5.   

    you may printf the offset of each thread's file transport, and the threadID.
    to test the problem
      

  6.   

    才5个线程,cpu真的会处理不来吗?还有,它处理不来,我们程序员都没办法的了。。
    不过非常谢谢你的调试建议
      

  7.   

    不是没有运行。多半是在调用socket的时候异常退出了。你可以调试下for(...)
    {
       hThread[i] = CreateThread(...);
    }
    Waitforobject(...);
      

  8.   


    用一个线程传,把socket缓存设置大些,效率会更高。5个线程之间来回切换会导致效率下降。