Multiple Threads, Race Conditions, and Concurrent Processing

G

gnassar

Hello,

I've written to this group a couple times and would like to initially
start by thanking those who reply to the posts. I seem to be having
some issues that are out of my understanding at the moment and
hopefully someone knowledgeable will be able to guide me the right way.

I have created a program that utilizes three threads minimum. The
initial gui thread, and two worker threads. I haven't set the worker
threads as background threads yet. The problem I am facing is that each
thread when opened initialized itself and begins sending a file to a
server on my network.

Essentially I'd like to transfer two files at the same time to a single
place. The only problem I'm facing is that after one starts, the other
file essentially stops sending. The thread doesn't continue processing.
Actually it does nothing.

I was wondering what information you'd need to help me out. I'm a bit
confused as to why this is happening.

TIA
G
 
A

adebaene

Hello,

I have created a program that utilizes three threads minimum. The
initial gui thread, and two worker threads. I haven't set the worker
threads as background threads yet. The problem I am facing is that each
thread when opened initialized itself and begins sending a file to a
server on my network.

Essentially I'd like to transfer two files at the same time to a single
place. The only problem I'm facing is that after one starts, the other
file essentially stops sending. The thread doesn't continue processing.
Actually it does nothing.

What do you mean by "a single place". Do you mean you want to copy 2
fines in the same network directory?

How are you "sending" the files? Give us more details, or show us a
minimum code sample.

Arnaud
MVP - VC
 
G

gnassar

Hi Arnaud,

Thanks for the reply. Essentially the code for transferring the files
works. There's no issue in that.

I have two classes. The main one: Form1 and the secondary thread:
dthread. Essentially the transferring processes all occurs within
dthread. So, once you instantiate dthread in another thread it starts
sending files.


dthread
public:
public: void ThreadCallBack(Object^ threadContext);

The thread starts when it calls: ThreadCallBack in dthread. Via:

MRE = gcnew ManualResetEvent(true);
dthread^ t = gcnew dthread(this->clb_List->Items[a]->ToString(),
MRE);
WTHREAD = t;
System::UInt32^ m_i = gcnew UInt32(i);

//ThreadPool::QueueUserWorkItem(gcnew
System::Threading::WaitCallback(t, &dthread::ThreadCallBack),this);

Thread^ newThread =
gcnew Thread(gcnew ParameterizedThreadStart(t,
&dthread::ThreadCallBack));
ThreadList = newThread;

newThread->Start(this);
break;


You'll notice that this seems to be done to use a ThreadPool. I changed
it to normal threads thinking that would solve the issue. It didn't
seem to be that way. Inside ThreadCallBack we have:

if (ParentThread->InvokeRequired){
IAsyncResult^ A = ParentThread->BeginInvoke(gcnew
GetTextDelegate(this, &dthread::GetTextD));
M = (String^) ParentThread->EndInvoke(A);
}

ParentThread->BeginInvoke(gcnew SetStatusTextDelegate(this,
&dthread::WriteStatusInformation),
(Object^)(gcnew String("Thread: " + _n.ToString() + " Is sending: " +
M->ToString())));

if (!Execute(M)){
ParentThread->BeginInvoke(gcnew SetStatusTextDelegate(this,
&dthread::WriteStatusInformation),
(Object^)(gcnew String("Thread Forced Exit On: " + M + ". Error
Detected.")));
_doneEvent->Set();
return;
}


Execute is where I get the next file to be sent, then continues to loop
while getting files from the list box.

I have two threads. They're created above and placed into an array just
for a way to access them. Both threads hit the point of executing
"GetTextD". The problem is only one continues and attempts to send
files. The first stays there. At least I think it's there.

I was wondering shouldn't they be racing each other? Both attempting to
send files?
 
G

gnassar

Update:

I've discovered something interesting.
If the program is set to use 3 threads, two threads can send files, but
one of them is blocked. I can't seem to pinpoint where though.

TIA
 
G

gnassar

I've pinpointed the problem:
When I do:
Stream^ loPostData = Request->GetRequestStream();

Within two threads at near close times I assume one fails. Thus killing
the thread. Not sure why but I placed a delay
Thread::Sleep(randobj->next()) it seemed to have fixed the issue
temporarily.

I'm sure thats not the optimal solution.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top