473,403 Members | 2,323 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Thread Synchronization

Hello,

I asked a question regarding thread synchronization on this usergroup
some days back. I was using the AutoResetEvent class for signalling
between two threads. The problem with AutoResetEvent was that it
sometimes it used to stop working a thread never come back from Wait
state.
I implemented Monitor for signalling following the suggestions came in
reply to that post. It started to getting worse and the application
started to hang (become irresponsive). I googled around and found that
there occurs a deadlock when Invoke is called on a control within the
Monitor lock. The alternatives were

i. Call the method (which is invoking the control) outside the lock
ii. Use BeginInvoke instead of Invoke.

both solutions were not feasible with current implementation of the
software, so we decided to device a custom signalling mechanism using
the Socket connections with the loopback address and writing and
reading a byte to Set and Wait respectively. This became even worse
and sometimes application as well as OS is hanged.

PC is Intel P4 2.67 with 2GB RAM. The user usually uses 9 to 10
instances of MS Excel, MS Outlook, Internet Explorer (several
instances) and some other applications.

Can anyone of you suggest what might be the cause of the problem? It
is a simple Producer/Consumer pattern, is it related to the PC
resource (GDI objects etc).

Regards

Maqsood Ahmed
Kolachi Advanced Technologies
http://www.kolachi.net

Aug 14 '07 #1
2 1419
On Aug 14, 8:05 am, Maqsood Ahmed <maqsood.ah...@gmail.comwrote:
I implemented Monitor for signalling following the suggestions came in
reply to that post. It started to getting worse and the application
started to hang (become irresponsive). I googled around and found that
there occurs a deadlock when Invoke is called on a control within the
Monitor lock.
Only if you try to acquire the same lock from the UI thread.
The alternatives were

i. Call the method (which is invoking the control) outside the lock
ii. Use BeginInvoke instead of Invoke.

both solutions were not feasible with current implementation of the
software, so we decided to device a custom signalling mechanism using
the Socket connections with the loopback address and writing and
reading a byte to Set and Wait respectively. This became even worse
and sometimes application as well as OS is hanged.
You've got a fundamental problem if the alternatives listed above
aren't feasible for you. You need to redesign so that they *are*
feasible, rather than rolling your own locking strategy which will
have the same problems.
PC is Intel P4 2.67 with 2GB RAM. The user usually uses 9 to 10
instances of MS Excel, MS Outlook, Internet Explorer (several
instances) and some other applications.

Can anyone of you suggest what might be the cause of the problem? It
is a simple Producer/Consumer pattern, is it related to the PC
resource (GDI objects etc).
Well, you haven't really said what you're doing, but it *sounds* like
you're acquiring a lock in one thread, then trying to reaquire it in
the UI thread. That will deadlock because the UI thread won't be able
to acquire the lock until the worker thread has released it, and by
using Invoke instead of BeginInvoke the worker thread won't release
the lock until the UI has returned. Deadlock.

Now, if you could explain more about what you're trying to do, we may
be able to help you design your way out of that situation.

Jon

Aug 14 '07 #2
Maqsood Ahmed wrote:
[...]
both solutions were not feasible with current implementation of the
software, so we decided to device a custom signalling mechanism using
the Socket connections with the loopback address and writing and
reading a byte to Set and Wait respectively. This became even worse
and sometimes application as well as OS is hanged.
In addition to what Jon wrote, I would suggest that you consider the
fact that deadlock is generally a consequence of design, not implementation.

In other words, it really doesn't matter how you communicate between
threads. If your code has deadlock potential, all you change by using a
different inter-thread signaling mechanism is how you wind up deadlocking.

Deadlock happens when one thread is waiting on another at the same time
that other thread is waiting on the first. You can change the way
threads wait on each other, but they will still deadlock.

Pete
Aug 14 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bill Davidson | last post by:
Hello All: I've got a question about synchronization requiremements in a C# worker thread procedure that, among other things, sinks events from outside sources. I realize the worker thread will...
1
by: Bill Davidson | last post by:
(RESEND: I added a little more code to the sample for clarity) Hello All: I've got a question about synchronization requiremements in a C# worker thread procedure that, among other things,...
4
by: scott | last post by:
hi all, Thx to any one that can offer me help, it will be much appreciated. iv got a multithreaded program and need to use thread synchronization. The synchronization does not have to...
20
by: Bob Day | last post by:
Using VS 2003, VB, MSDE... There are two threads, A & B, that continously run and are started by Sub Main. They instantiationsl of identical code. Thread A handles call activity on telephone...
6
by: Robert Speck | last post by:
Hi there, Can anyone shed anymore light on why "Thread.Suspend()" has been deprecated by MSFT beyond what MSDN says about it. I'm not sure if I quite appreciate the various pitfalls they discuss...
13
by: arun.darra | last post by:
Are the following thread safe: 1. Assuming Object is any simple object Object* fn() { Object *p = new Object(); return p; } 2. is return by value thread safe?
9
by: RvGrah | last post by:
I'm completely new to using background threading, though I have downloaded and run through several samples and understood how they worked. My question is: I have an app whose primary form...
0
by: sundman.anders | last post by:
Hi all! I have a question about thread synchronization and c++ streams (iostreams, stringstreams, etc). When optimizing a program for a multicore processor I found that stringstream was causing...
19
by: Hapa | last post by:
Does only reading (never writing) of a variable need thread synchronisation? Thanks for help? PS. Anybody knows a Visual C++ news group?
8
by: Brad Walton | last post by:
Hello. First post, but been doing a bit of reading here. I am working on a project in Java, but decided to switch over to C# after seeing some of the additional features I can get from C#. One of...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.