473,804 Members | 2,255 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

multithreading, possible problems

65 New Member
i have implemented famous producer consumer example for multi-threading tutorials, to have some insight about multi-threading.
there is producer thread which inserts into a buffer, if the buffer is not full.
if the buffer is full it waits.
there is a consumer thread which removes from the buffer, if the buffer is not empty. if the buffer is empty it waits.

although my implementation works succesfully, i am suspicious about my design,
it may cause dead locks:

here is the pseudo code:

Expand|Select|Wrap|Line Numbers
  1. thread_inserter:  
  2. buffer.push();
  3. inside push method:  if(storedItems>0)  notify();
  4. inside push method:  if(storedItems==MAX_SIZE)   wait();  else insertData();
  5.  
  6. thread_remover:
  7. buffer.pop():
  8. inside pop method:  if(storedItems<MAX_SIZE)  notify();
  9. inside pop method:  if(storedItems==0)  wait();  else removeData();
  10.  
do you think that there is a problem with my locating both wait and notify methods in the same methods ?
Aug 27 '07 #1
1 1351
JosAH
11,448 Recognized Expert MVP
i have implemented famous producer consumer example for multi-threading tutorials, to have some insight about multi-threading.
there is producer thread which inserts into a buffer, if the buffer is not full.
if the buffer is full it waits.
there is a consumer thread which removes from the buffer, if the buffer is not empty. if the buffer is empty it waits.

although my implementation works succesfully, i am suspicious about my design,
it may cause dead locks:

here is the pseudo code:

Expand|Select|Wrap|Line Numbers
  1. thread_inserter:  
  2. buffer.push();
  3. inside push method:  if(storedItems>0)  notify();
  4. inside push method:  if(storedItems==MAX_SIZE)   wait();  else insertData();
  5.  
  6. thread_remover:
  7. buffer.pop():
  8. inside pop method:  if(storedItems<MAX_SIZE)  notify();
  9. inside pop method:  if(storedItems==0)  wait();  else removeData();
  10.  
do you think that there is a problem with my locating both wait and notify methods in the same methods ?
Yup; the logic is not correct; read about the wait() call: it should be the body
of a while loop checking the condition. I hope you did synchronize on the buffer
which is the shared resource.

The producer should first wait on a full buffer, i.e. it shouldn't wait when another
item can be pushed into it. The consumeer should wait on an empty buffer, i.e.
it shouldn't wait when the buffer contains items. After the pushing (the producer)
and after the popping (the consumer) the waiting 'others' should be notified,
all of them.

kind regards,

Jos
Aug 27 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1555
by: Murad Tchary | last post by:
Hi Here is the code that I use in my app and that code works just fine: loProcessThread = New Thread(AddressOf ProcessSomething) loProcessThread.IsBackground = True loProcessThread.Start() 'Wait until some data will be ready to be use ... Do
11
4274
by: Mark Yudkin | last post by:
The documentation is unclear (at least to me) on the permissibility of accessing DB2 (8.1.5) concurrently on and from Windows 2000 / XP / 2003, with separate transactions scope, from separate threads of a multithreaded program using embedded SQL. Since the threads do not need to share transaction scopes, the sqleAttachToCtx family of APIs do not seem to be necessary. <quote> In the default implementation of threaded applications against...
16
8514
by: Robert Zurer | last post by:
Can anyone suggest the best book or part of a book on this subject. I'm looking for an in-depth treatment with examples in C# TIA Robert Zurer robert@zurer.com
5
2143
by: sarge | last post by:
I would like to know how to perform simple multithreading. I had created a simple form to test out if I was multithreading properly, but got buggy results. Sometime the whole thig would lock up when I got two threads going at the same time. What I have is two text boxes (textBox1 and textBox2) and four buttons(cmdStartThread1, cmdStartThread2, cmdStopThread1, cmdStopThread2)
2
370
by: Alper AKCAYOZ | last post by:
Hello, I am developing a TCP/IP Client application in Windows Forms (.NET) template. I use one MDI form as initial form where I connect to server and starts a background thread (thread_A) for receiving data. Here, I use the Socket_A By clicking a toolbarbutton, I send a command to server. If it is successfull, I opened the MDI-Child form with "frmChildForm->Show();" command. I create two more sockets (Socket_B, Socket_C) at child form...
16
1749
by: who be dat? | last post by:
Consider the following code which enables multithreading in an ASP.Net application I'm writing: Code in global.asx Application_start subroutine, Threadnotify is declared Globally as a new thread where it uses the address of EmailNotify below: ThreadNotify.IsBackground = True ThreadNotify.Start()
2
3743
by: shonend | last post by:
**** sorry about the length of the message. If you can't read the whole thing and still willing to help, read the last 2 paragraphs where the main problem is described. The introduction story is mentioned to, as much clear as possible, give a picture in what environment the problems rise**** Hello experts! I would appreciate if you can address this problem I have and give a hint what could be wrong.
55
3340
by: Sam | last post by:
Hi, I have a serious issue using multithreading. A sample application showing my issue can be downloaded here: http://graphicsxp.free.fr/WindowsApplication11.zip The problem is that I need to call operations on Controls from a delegate, otherwise it does not work. However each time I've done an operation, I must update the progressbar and progresslabel, but this cannot be done in the delegate as it does not work.
2
2271
by: Pradnya Patil | last post by:
hi , I am trying to draw ' html div-tag ' on the screen which will resemble a rectangle through vb.net code. I want it to be drawn faster...so I introduced multithreading using Threadpool. I divided the complete drawing into 3 parts..1st will be done by main thread and other two are done in these procedures - <1LongTimeTask <2LongTimeTask2 I have invoked the threads using below method. **************
1
1457
by: RSH | last post by:
Hi, I have a situation in where I have a main loop that queries a "Parent" Global database table. Based on that resultset I loop through all of the databases and the appropriate table in the "children" databases. Because we have roughly 5000 "Children" databases this process is taking quite some time. I was wondering if there is some way to incorporate multithreading to speed
0
9714
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10346
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10347
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7635
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6863
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5531
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4308
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.