CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 2006
    Location
    beyond the pillars of hercules
    Posts
    295

    Threading Problems

    ok, i am developing a TCP/IP Client-Server application. Im currently working on transferring multiple files simultaneously from Server to Client.

    Here is the code that receives data

    Code:
    //Global variable
    	struct SepComm 
    	{
    	      char UnmanagedData[8192];
    	      int TotalSize;
    	      int Sock;
    	      void* param;
    	};
    Code:
    case FD_READ:
    {
    	int SizeOfData = 0;
    	int SockNO=0;
    
    	for(SockNO=0;SockNO<=TotalConn;SockNO++)
    	{
    	          if (pMsg->wParam == sTemp[SockNO])
    			break;
    	}
    
             ZeroMemory(SepCommParam.UnmanagedData,sizeof(SepCommParam.UnmanagedData));
             SizeOfData=recv(sTemp[SockNO],(char*)SepCommParam.UnmanagedData,sizeof(SepCommParam.UnmanagedData)/sizeof(SepCommParam.UnmanagedData[0]),0);
    
    	if(SizeOfData == -1)
    		goto Close_Sock;
    
    	if(SizeOfData != -1)
    		if(SizeOfData != 0)
    		{
    			SepCommParam.TotalSize = SizeOfData;
    			SepCommParam.Sock = SockNO;
    			SepCommParam.param = this;
    			DWORD threadID;
    			HANDLE hThread = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)SeparateCommands, &SepCommParam, 0, &threadID);
    		}
    					
    }
    break;
    the problem is (if you haven't noticed it already) that before the thread (SeparateCommands) is completed , a new data is arrived and it changes the SepComm variables and crashes the function.

    whats is the best what to prevent or fix this issue?

    thanks

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured