5

Simple examples of blocking

 2 years ago
source link: https://www.codesd.com/item/simple-examples-of-blocking.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Simple examples of blocking

advertisements

I would like to explain threading deadlocks to newbies. I have seen many examples for deadlocks in the past, some using code and some using illustrations (like the famous 4 cars). There are also classic easily-deadlocked problems like The Dining Philosophers, but these may be too complex for a real newbie to fully grasp.

I'm looking for the simplest code example to illustrate what deadlocks are. The example should:

  1. Relate to a "real" programming scenario that makes some sense
  2. Be very short, simple and straight forward

What do you recommend?


Maybe a simple bank situation.

class Account {
  double balance;

  void withdraw(double amount){
     balance -= amount;
  } 

  void deposit(double amount){
     balance += amount;
  } 

   void transfer(Account from, Account to, double amount){
        sync(from);
        sync(to);

        from.withdraw(amount);
        to.deposit(amount);

        release(to);
        release(from);
    }

}

Obviously, should there be two threads which attempt to run transfer(a, b) and transfer(b, a) at the same time, then a deadlock is going to occur because they try to acquire the resources in reverse order.

This code is also great for looking at solutions to the deadlock as well. Hope this helps!

Related Articles

Example of blocks in ios5

I am a bit familier with the concepts of Blocks but I don't know how to create them in iOS5. Can anyone provide me with a simple example ? Much appreciated :)Use this simple example in Blocks.. int multiplier = 7; int (^myBlock)(int) = ^(int num) { r

ANTLR: Is there a simple example?

I'd like to get started with ANTLR, but after spending a few hours reviewing the examples at the antlr.org site, I still can't get a clear understanding of the grammar to Java process. Is there some simple example, something like a four-operations ca

Simple example of decoupling the user interface and network calculation

I am looking for a simple example, UI is web based (accessed through browser over network) Business logic (APIs) is deployed on a server. It is a printer management application. Business logic will access external electronic printer & can access its

Simple Example Linq Subquery

T-SQL Query Select * from dbo.User_Users Where UserID IN (Select UserID from Course_Enrollments) LINQ to Entities alternative of Above Query var innerquery = from en in Course_Enrollments select en.UserID; var query = from u in User_Users where inner

Simple example of Masstransit with RabbitMQ

I want to use MassTransit bus with RabbitMQ. But I am not able to find a simple example. I am looking for example which will get me started. What I have tried. googled: But most the examples are using MSMQ or they using too many configuration options

Can someone give me a very simple example to change the color of the text?

I have searched the Internet for a very simple example, but all of them were too complex and I couldn't understand them. Here is my code, but my linter said there was an error and I have no idea why. (I would like to request a REALLY simple example)

Simple example using Backbone.js and Rivets.js

I am looking for a very simple example where e.g. there is a two way binding between span text and an input element using Backbone.js and Rivets.js. Perhaps there is one in the Rivets.js docs, but I can't find it. Any help please?Assuming that you me

& Ldquo; Can not find the symbol & rdquo; Error - even on a ridiculously simple example

So I've been trying to solve this problem for a matter of hours now. I've scoured the internet, I've scoured StackOverflow, I've asked some co-workers (I'm an intern) and honestly no one can tell me what is going on! I put together a really really si

American Fuzzy Lop fails with a simple example

I've been trying to use American Fuzzy Lop but I can't make it work with a simple example like this: #include <stdio.h> #include <string.h> int main(int argc, char * argv[]){ char name[10]; if ( argc > 1 ){ strcpy(name, argv[1]); printf(&qu

NodeJs callback simple example

can any one give me a a simple example of nodeJs callbacks, I have already searched for the same on many websites but not able to understand it properly, Please give me a simple example. getDbFiles(store, function(files){ getCdnFiles(store, function(

WPF MVVM EF Simple example

I was hoping someone could help me get this simple WPF MVVM example off the ground as I am struggling to plumb the data into the view model. I have an SQL Table Temperatures where each record has a time stamp and then a numerical value eg. LogTime---

A simple example of Django and CSS

I'm new to Django, and am trying to set up a really simple Django app. Now, I'm up to chapter 5 in the Django online book : http://www.djangobook.com/en/2.0/chapter05/ All I want to do now, before I start trying databases, is to add in some simple CS

How do I pass a statement by reference in this simple example?

I'm having a problem in my code, I hacked up this simple example to illustrate it. I have a Player class that has a public enum PlayerType. I have a Manager class with a createPlayer method that accepts a reference to a player and a player type. But

Simple examples implementing push notifications using the latest Phonegap for iOS?

Anyone have a simple example or tutorial of how to implement push notifications using the latest version of Phonegap (3.1 or thereabouts) and current APN site? I've looked at Holly Schinsky' (DevGirl's Weblog), and while detailed, some of it's really

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK