How to reconcile the two definitions of work? (mechanical and thermodynamical) When studying classical mechanics, work is defined as: $W_M=\int F_{tot} \hspace{2 mm} dx$.
However, for thermodynamics, work is defined as: $W_T=\int -F_{ext} \hspace{2 mm} dx$.
I'm having trouble reconciling both definitions. I have found the mathematical relationship between both of them (see below), but I wouldn't know how to interpret either this or the other thermodynamical functions in the light of this. For example, $\Delta U$ is usually related to the conservation of energy, but since the definition of $W$ is different, it isn't that direct a relationship now (for me at least).
$$
W_M=\int F_{tot} \hspace{3pt} dx \hspace{3pt} =\int F_{int}-F_{ext} \hspace{3pt} dx
$$ $$
W_M=\int F_{int}\hspace{3pt} dx+W_T \hspace{3pt}
$$
The books I've seen usually just throw the definition and start talking about the state functions, but do not spend any time reflecting on this definition or analyzing why is it different from the classical one. I found this source which tries to deal with this same issue: it is very interesting, but it gets a little confusing towards the end when he tries to relate his mechanical w and q with his thermodynamical W and Q (I don't understand how he combines equations 1 and 3 or 1 and 5 to get his results). Does anyone understand that last step?
Has anyone seen or thought about this issue before? Does anyone know of any other source or book where this issue is dealt with more detail?

EXAMPLE PROBLEM (to motivate the question): Consider a very big cylinder with a mobile piston in the middle separating two sections filled with gas (A and B). Both the cylinder walls and the piston are adiabatic, so there is no exchange of heat with the exterior or between sections. The pressure of the gas in A is higher than the pressure of the gas in B. The piston is originally being hold, but we let go of it for just a second and then hold it again (we assume the change in volume was small enough so as to not produce a change of pressures).
In this situation, I could consider the gas of section A as my system and claim it gave $p_B \Delta V$ of work to its exterior (the gas in section B, since it is the only exterior with which it interacts). But I could also consider the gas in section B as my system, and claim it received $p_A \Delta V$ of work from its exterior (gas in section A). But since $\Delta V$ is equal in both cases (only changes the sign, which is what determines is work is given or received) but the $P_i$ aren't, there is an energy difference I cannot account for.
My intuition tells me that the energy difference must come from the potential energy stored in the original pressure difference, but since thermodynamics (or the way I was taught thermodynamics) starts with a different definition of work and does not explain its relationship with the mechanical one, it is not that trivial to me how to incorporate this into a thorough thermodynamical analysis of this process.
 A: Perhaps I can explain the difference.  Consider a piston in a cylinder with the inside of the cylinder being the system.  The surroundings are outside the cylinder.  
Assume there is a gas inside the cylinder exerting a pressure $p$ on the piston from the inside.  Let there be a vacuum on the outside, but several weights sit on the outside of the piston.  These weights exert a gravitational force on the area of the piston.  The result of this is a pressure $p_e$.
So $p$ is the internal pressure and $p_e$ is the external pressure.  If the internal pressure is less than the external, the piston will descend and the external weights will do work on the system.  On the other hand, if the internal pressure is greater, then the system will do work on the surroundings, that is, the weights.
Now we are set up.  The thermodynamic definition of work is the work done ON the system by external weights (forces) or the work done by the system on the external weights.  The first of these is traditionally counted as positive, the latter as negative.
Consider an expansion of the system.  You can easily calculate the work done by the system on the weights if you know the acceleration of gravity (which we will assume you do), the mass $m$ of the weights, and the height $h$ to which they have been raised.  That's ALL you need to know.  Note that the pressure inside the cylinder does not come into it except to ensure that there is an expansion and that the pressure is great enough to raise the weights by $h$,
If there is a compression of the system, again all you need to know are the same quantities as for an expansion, except now $h$ is negative.
In one case we compute the work done to raise the weights; in the other we compute the work done when the weights are lowered.
How does this differ from the normal definition of work in physics?  It doesn't.  You only need to take all the forces into account if you want to compute the acceleration of the piston.  Thermodynamics doesn't care about that, in fact the piston itself is usually assumed to be weightless.
A: The question to ask your self is 

"Work done on the system or work done by the system?"

The sign convention (and not every book uses the same one), is all about which direction you measure as the positive displacement in the thermodynamic case.
To keep it sorted out you just have to remember that the first law of thermodynamics is simply a re-statement of the conservation of energy. Fixing the direction convention will tell you the sign convention or fixing the sign convention will force the direction convention.
A: (delta)U = (delta)Q + (W)  --> suggested by IUPAC in one of their convention
where: +W is used, if it is done on the system
-W is used, if it is done by the system
(delta)U = (delta)Q - (W)  --> this is what we are thought in college (BSME)
where: -W is used, if it is done on the system
+W is used, if it is done by the system
both will arrive on the same answer, the catch here is the sign of the Work (W), depends if it is done ON the system, if (+) sign is used in the formula, Work done ON the system must be (+), but is (-) sign is used in the formula, Work done ON the system must be (-).
This is my guide which keep me in synch with whatever type of formula is written and used in the textbook.
I hope this will be of help to students and engineers.
A: Mechanics defines work as done on the system by a force acting through a distance; the first law of thermodynamics (as developed in most engineering texts) typically defines work done by the system, hence the difference in sign.  (Note: Some physics texts that address thermodynamics define work as done on the system.)
Work in thermodynamics is a much broader concept than work in mechanics.  In thermodynamics work is defined as "energy transferred without mass tranfer across the boundary of a system because of an intensive property difference other than temperature between the system and its surroundings".  Using this definition, electrical current flowing in/out of a system is work.  (Heat is defined as energy transferred without mass transfer across the boundary of a system solely because of a difference in temperature between the system and it surroundings.  Mass transfer is addressed using enthalpy.)  See a good thermodynamics text such as one by Obert, or by Sonntag and Van Wylen.
Trying to use the more restrictive definition of work from mechanics in applications of the first law of thermodynamics is where you are having the confusion.
To distinguish between these two definitions of work, some use the name pseudowork for the work defined in mechanics and reserve work to mean work as defined in thermodynamics.  You can find discussions of this approach on the web under "pseudowork" and/or articles written by Sherwood.
A: In your example of a closed pipe with a piston held in middle between two gases A & B, let's further say that A & B are ideal gases and for simplicity are always in thermal equilibrium between each other. Note that since $P_A > P_B$, it must also follow that $n_A > n_B$ according to the ideal gas law ($PV = nRT$).
If you release the piston, it will eventually settle somewhere in the pipe giving more volume to gas A and less to gas B when pressure equilibrium is attained, i.e. $P_A = P_B$. In fact, if $V_i$ is the original volume of gas A, then the final volume of gas A will be $$V_f = \frac{2n_AV_i}{n_A + n_B}$$ And so by letting gas A irreversibly (as in your example) expand from $V_i \to V_f$, you will generate
$$\Delta S = n_A R \ln\frac{V_f}{V_i} = n_A R \ln\frac{2n_A}{n_A + n_B}$$
of entropy inside of the pipe. Note that by the first law of thermodynamics $\Delta U = Q - W$  the overall temperature of the pipe would remain unchanged since $\Delta U_A = -W = -\Delta U_B$, and therefore $U_{total} = U_A + U_B$ still stays the same.
In other words, the "work done" by gas A will increase the internal energy of gas B by the same amount, regardless of whether the process was reversible or irreversible. In your example you provided an irreversible process, but as you can see (short of violating the First Law of Thermodynamics), the "magnitude" (but not the sign) of work done by moving the piston has to be identical regardless of whether we limit our system to a component of the pipe ("gas A" or "gas B").
