multithreading – What is a “lock ordering inversion”?

When multiple threads acquire multiple locks, the only way to guarantee progress in certain cases is to ensure that the locks are always taken in the same order.

In the basic case of two locks and two threads, one lock may be designated Lock A, and one lock designated Lock B.

If Thread 1 seizes lock A, then Thread 2 will wait at Lock A until thread 1 releases it. Whilst Thread 2 waits at Lock A, Thread 1 is free to proceed and also seize Lock B. At some point Thread 1 will release Lock A (either before or after releasing B, it doesn’t matter), and Thread 2 will then proceed. If Thread 1 wanted to retake Lock A for whatever reason, then it would only do so after first releasing Lock B. This is the correct ordering, in the sense that it involves no risk of deadlock. A is always taken before B.

In the case of inverted ordering, Thread 1 proposes to seize A before B, whilst Thread 2 proposes to seize B before A. With suitable timing, a situation can come about in which each have seized the first of the locks they proposed to take, yet each also needs access to the other lock in order to proceed. This is a deadlock situation.

A railway analogy may be easiest for anyone who knows anything about signalling principles. If trains only ever go one way along a track, they can never enter deadlock with one another. But if a train is allowed to enter a single-track section from either end, even when another has already entered from the other end, then eventually one of them is going to have to reverse out to let the other go by. In practice, such single-track sections are treated as an indivisible whole – if a train enters from either end, then a lock is taken on the whole section, and the whole section is covered by just one lock.

When concurrent programming is approached as a science, and where locks can be held in a conflicting fashion, then an ordering (or hierarchy) of locks is defined, and all code must seize locks in a way that is consistent with that order or hierarchy. Going against the order is something that is only possible if you are prepared to deal with an ensuing deadlock.

“Priority inversion” in scheduling, meanwhile, is another thing entirely. Again to fall back on a railway analogy, it is holding the fast train in the sidings, to let the slow train go by, which is against the order of the fast/slow priority.

algorithms – Ordering puzzle problem- It is NP?

Suppose there are ‘n’ number of food items in a party.
f1 , f2 ….,fn.
In which order we eat them matters. Each ordering should contain all items included.
Some order in which I choose the food items may give me cancer.( there can be more than one such ordering).

Every food item is made and represented by three categories of ingredients: vegetables, spices and meat.

Set of types of vegetables: {a,b,c}.

Set of types of spices: {e,f,g}

Set of types of meat: {x,y,z}

Detecting Cancer: If all vegetables or spices or meat of any one type are eaten at the start or at the end before any another type, then that ordering is cancerous.

For example,
(1) f1 is made of {a,f,x} ;
f2 is made of {a,e,y} ;
f3 is made of {c,g,z} ;
f4 is made of {b,f,y} ;

a) ordering f1,f2,f3,f4:
Based on vegetables:{a,a,c,b} all a’s are eaten at the start, therefore this ordering is cancerous.
Based on spices:{f,e,g,f} , meat:{x,y,z,y} not cancerous as no type is continuously eaten at the start or end. Therefore, this order is cancerous based on vegetables.

b) ordering f1,f3,f2,f4:
Based on vegetables:{a,c,a,b} not cancerous ;
Based on spices:{f,g,e,f} not cancerous ;
Based on meat:{x,z,y,y} cancerous because all meat of type ‘y’ are eaten continuously at the end.

c) ordering f1,f2,f4,f3 :
Based on meat:{x,y,y,z} not cancerous(based on meat) because all meat of type ‘y’ are eaten in the middle, not at the start or end.
Based on vegetables:{a,a,b,c} all a’s are eaten at the start, therefore this ordering is cancerous.

There is a doctor at the party who can take a look at any given order (one at a time) and checks if it is cancerous or not (based on above conditions) in O(n).

Question 1: Can I detect if the given food items are given such that all orders are be cancerous?

Question 2: If somehow I find that there exist non-cancerous orders, how to make a cancerous order into non-cancerous order by swapping food items?

Question 3: Can I optimize the swappings in Question 2?

Design pattern for sorting / ordering items in card view

When sorting anything either a Vertical or Horizontal list of items is preferred. (but not both)

A vertical list is my personal preference as many devices are built to easily scroll up and down (i.e. mouse wheel, smartphones, etc.) among other reasons.

First of all, this is a great question so go ahead and vote it up now.

Laying out content in bite-sized chunks (cards) that respond to look good and fit in any size space is a reality. People seem to understand the card layout pattern used by many popular sites such as Pintrest. Card layouts work well on any size display and consequently continue to grow in popularity.

Simply put, We are living in a responsive world and I am a responsive girl (or boy).

The problem is that anytime you try and sort items in a grid (vertical and horizontal at the same time) then someone is bound to interpret it wrong…

Dr. Fie Nogsh or Never Forget?

grid of beads

Below are some questions to ask when in a grid sorting situation…

1. Can I do the sorting automatically without any user interaction at all?

One reason that most people understand card layouts is that card layouts understand them!

For example, if you’re listing movies from a certain category then just make the movie that other people watched the most and/or rated the highest bigger than the others.


2. Can I replace sorting with filters and/or groups?

Perhaps it’s hard to find a lot of information on this topic is because there are better ways to get at the information besides sorting everything and then looking though all the items. If there is an option to type anything I want and instantly see items disappear that have nothing to do with what I’m typing then this is my preferred approach. Once I find the card I’m after you could provide additional options to see how many items come before and/or after in the list. In any case, make absolutely sure that sorting items in a grid is what you require.

3. Do I really need to sort on more than one key at a time?

Avoid sorting a grid on multiple keys at all costs! This would end up looking like a pivot table that no sane person can understand

If you must sort your content using both both rows and columns at the same time, however, then start in the top left corner and go to the right first before continuing on the next row like reading a book (top to bottom). There may be rare times when you would start at the right and go left first such as countries where they read right to left but it should be self evident that putting the first sorted item at the bottom is always wrong.

group card sort

Most of this information should really be self evident but various eye tracking studies show that the Top Left corner is where people look first so the key that you’re sorting on probably belongs there as well.

eye tracking study results

Try and eliminate as much information as possible so the order of the cards can be understood quickly. Only show the value you are sorting on (in this case carbohydrates) next to the most vital piece of information or even just a picture of the item. Additional info can be shown/hidden on hover/click…

mcdonalds menu

When sorting cards it is usually preferred to use either a Vertical or Horizontal layout but not both so for completeness I’ll discuss those below…

When sorting a list of cards vertically the key used to do the ordering should stand out from the card if possible so that it’s instantly clear why the top card is at the top and the direction to go next.

vertical list

There are few examples of sorting cards horizontally though it should be clear that the item to the far Left is the Most Popular and goes without saying. The popularity decreases (or stays the same) the further to the Right I go in the example below.

netflix most popular movies

Stop Canonical Ordering of Output List

I’m working with the code below, which works well for my purposes; however, instead of the canonical ordering seen in the output, I want the output produced in the order I give in the Cases line of code. Any help is most appreciated.

m = {{N11, "x"}, {N12, "y"}, {N19, "z"}, {N20, "w"}, {N41, 
    "exz"}, {N42, "jy"}, {N49, "br"}, {N50, "mew"}, {N61, "qr"}, {N62,
     "xqe"}, {N539, "ycf"}, {N551, "pyu"}, {N559, "wq"}, {N560, 
Cases[m, {N19 | N560 | N41 | N61 | N551 | N20 | N539, __}];

Output from the above code

N19 z
N20 w
N41 exz
N61 qr
N539 ycf
N551 pyu
N560 hra

magento 1 update or change advance search filter ordering

I would like to move country of manufacture element at the bottom

see screenshot

I am unable to do that by changing the positions of the attribute group.

Please suggest or guide me further thanks

depth first search – pictorial representation of pre and post ordering for edge types

I am reading chapter 3 Decomposition of Graphs of Algorithms by Dasgupta, Papadimitriou & Vazirani and they show a pictorial representation of pre and post numbers with brackets for tree, forward, back and cross edges. I have pasted the image of the area I am unclear on.

I am assuming that u is the node/vertex we see first and then we see v later. Comparing to the 2nd image showing the definitions on a graph, I am taking u as node A.

What I don’t get is the order for the other 2 back and cross edges. What I have is

  • u v u v for back edge.
  • u u v v for cross edge.

Essentially the order is flipped with u and v in what the textbook has. Could someone clarify why this is the case with the image below.

Edge types

Edge definitions

I read this answer but I came out more confused with the explanation.

Surprisingly I was able to correctly identify the pre and post numbers in this figure in the textbook. It is the letters u and v that have me confused.

enter image description here

Ordering Multicasting in Distributed Systems

We know that Causal ordering in ordering multicasting implies FIFO ordering. But does FIFO ordering implies Casual Ordering?

information architecture – Best practices for address item ordering for user forms

We are currently re-designing a profile portion for user signup to our Saas, and was looking for some insight into best practices for ordering location information.

Right now our currently ordering metric is:
– Address (Free Text Field)
– City (Free Text Field)
– Country (Drop down)
– State/Province (Drop down – population of this is dependant upon Country selection).

This flow does not feel natural to me, however the country must come before state in order to populate the appropriate states.

Guidance and thoughts much appreciated!

magento2 – Prevent new customers (never purchaser anything before) from ordering specific products in Magento 2

I am in a situation that requires to prevent new customers (who never purchased before) from placing an order that contains some specific products.

these products should be available for all customers (logged in and not logged in), however when checking out, new customers shouldn’t be able to complete and should receive a custom error message.

is there any extension that allow that?

*Additional information: the story behind this is that these products are actually refills products, and should be only available to previous customers.

Thank you

postgresql – Mealplan: Allow ordering until 9am

Imagine you have a meal service and everyday there is a different meal which customers can order.

Mon, Dec 12: Pasta
Tue, Dec 13: Pizza
Wed, Dec 14: Gnocci

Orders are possible until 9am.

This mean Monday on 8am customers may order Pasta for today, at 9:15am they are no longer allowed to order for today.

How to handle this is with PostgreSQL.

I see two ways:

  1. Create a cron job which switches the column “orderable” to False.
  2. Create a view which calculates the field “orderable” on the fly.

Is there a feasible third or forth way?

Which way is simple and robust?