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?
Re: Multi threading issue joining threads
Re: Multi threading issue joining threads
Quote:
Originally Posted by
Igor Vartanov
Quote:
Originally Posted by
troutguy
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
Re: Multi threading issue joining threads