Quantcast
Channel: CodeGuru Forums - Visual C++ Programming
Viewing all articles
Browse latest Browse all 3046

Multi threading issue joining threads

$
0
0
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?

Viewing all articles
Browse latest Browse all 3046

Trending Articles