CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2012
    Location
    Eugene, OR
    Posts
    1

    Multi threading issue joining threads

    I have a windows form application that is using multiple threads to connect named pipes to multiple processes...

    I use and array of threads:

    Code:
    array<Thread^>^ serverThread;
    and start each thread:

    Code:
    {			
    	brokerCnt=this->checkedListBox1->CheckedItems->Count;
    	for(int i=0;i<brokerCnt;i++)
    	{
    		this->serverThread[this->serverNum] =
    		gcnew Thread(gcnew ThreadStart(this,&Form1::startTheServer));
    
    		this->serverThread[this->serverNum]->Start();
    		this->serverNum++;
    	}
    }
    Each thread starts a named pipe and then enters a while loop to do some comm:

    Code:
    private: System::Void startTheServer()
    		 {			 
    				 array<Byte>^ inBuffer=gcnew array<Byte>(512);
    				 array<Byte>^ outBuffer;
    				 array<String^>^ inputArr;
    				 array<double>^ tempArr=gcnew array<double>(24);
    				 int codeIn;
    				 int brokerNum;
    				 int pipeNo=this->pipeCnt;
    
    				 this->pipeCnt++;
    
    				 this->namedPipes[pipeNo]=gcnew NamedPipeServerStream(this->pipeName[pipeNo],
    					 PipeDirection::InOut,1,PipeTransmissionMode::Byte);				 
    
    				 this->setText("Pipe Connected and waiting...");
    
    				 namedPipes[pipeNo]->WaitForConnection();
    
    				 try
    				 {
    					 while(this->running)
    					 {
    						 namedPipes[pipeNo]->Read(inBuffer,0,inBuffer->Length);
    						 String^ receiveString=Encoding::UTF8->GetString(inBuffer);
    						 inputArr = receiveString->Split('\n');
    
    						 //todo: add code analyzer...and methods to handle.
    						 brokerNum=Int32::Parse(inputArr[0]);
    						 codeIn=Int32::Parse(inputArr[1]);
    
    						 switch(codeIn)
    						 {
    							case 1:
    								 for(int i=2;i<12;i++)
    								 {
    									 tempArr[i]=Double::Parse(inputArr[i]);
    								 }
    								 this->setData(tempArr,brokerNum);
    								 //put the analysis method in here...
    								 break;
    						 }	
    						 
    						 outBuffer = Encoding::UTF8->GetBytes( "Got your Message" );
    
    						 namedPipes[pipeNo]->Write(outBuffer,0,outBuffer->Length);	 
    					 }
    				 }
    				 catch(IOException^ e)
    				 {
    					 //this->namedPipes->Close();
    				 }
    
    				 namedPipes[pipeNo]->Read(inBuffer,0,inBuffer->Length);
    				 String^ receiveString=Encoding::UTF8->GetString(inBuffer);
    				 inputArr = receiveString->Split('\n');
    
    				 //todo: add code analyzer...and methods to handle.
    				 brokerNum=Int32::Parse(inputArr[0]);
    				 codeIn=Int32::Parse(inputArr[1]);
    
    				 outBuffer = Encoding::UTF8->GetBytes( "4" );
    				 namedPipes[pipeNo]->Write(outBuffer,0,outBuffer->Length);
    
    				 this->namedPipes[this->brokerCnt]->Close();
    			
    		 }
    Then when closing the app...I run this method which sets the flag for the threads to exit the while loop and join the threads:

    Code:
    private: System::Void setShutDown()
    		 {
    			 this->running=false;
    			 for(int i=0;i<this->serverNum-1;i++)
    			 {
    				 if(serverThread[i]->IsAlive::get())
    				 {
    					 serverThread[i]->Join();
    					 serverNum--;
    				 }
    			 }
    		 }
    when this runs and i close the app...all threads go into a [wait, sleep, or join] status based on the debugger thread window...

    Can anyone help in identifying why the join method loop is stopping all threads? I believe it should wait the main thread while the others join...and then finish cleaning up and close...

    Any comments?

  2. #2
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Multi threading issue joining threads

    Any comments?
    Only one: Wrong forum.
    Best regards,
    Igor

  3. #3
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Multi threading issue joining threads

    Quote Originally Posted by Igor Vartanov View Post
    Quote Originally Posted by troutguy View Post
    Any comments?
    Only one: Wrong forum.
    ... and another (rather) short one (sorry, I know it's off-topic here...):

    Thread::Join() is a blocking operation, i.e. it doesn't return until the worker function of the respective thread itself has returned. So your shutdown loop may easily cause a deadlock if there are any synchronization inter-dependencies between the individual worker threads.

    Also, it's not clear what class startTheServer() is a member of, IOW what this represents inside this function. However, the presence of the setText() member suggests it's some sort of GUI object or at least manipulates one. You need to be aware that in a Windows Forms app GUI manipulations are only allowed to be made by the GUI thread (IOW the application's main thread). Failure to observe this rule may lead to all sorts of weird (i.e. undefined) behaviour, including your program seemingly working without problems, leading to a false sense of safety.

    BTW, the correct forum section for C++/CLI questions like this one is: http://forums.codeguru.com/forumdisp...ed-C-and-C-CLI
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  4. #4
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Multi threading issue joining threads

    [ moved thread ]
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

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