\

Školení Návrhové vzory, OOP a UML


 Tuesday, October 19, 2004
Programátorská hádanka na nudné úterý

Dokážete najít dva ekvivalentní zápisy zámku (lock) v metodě AtomicOperation?

    class  AtomicTest
    {
        private int x = 0;
        private int y = 0;
        public void AtomicOperation(int arg)
        {
            lock(this)
            {
                x += arg;
                y += arg;
            }
        }
    }


Tuesday, October 19, 2004 5:27:00 PM (Central Europe Standard Time, UTC+01:00)       
Comments [16]  Programátorské hádanky


Tuesday, July 19, 2005 11:01:26 AM (Central Europe Standard Time, UTC+01:00)
1.
class AtomicTest
{
private int x = 0;
private int y = 0;
public void AtomicOperation(int arg)
{
Monitor.Enter(this);

try
{
x += arg;
y ...
Tuesday, July 19, 2005 11:01:26 AM (Central Europe Standard Time, UTC+01:00)
No to sa mi ale podarilo:))
Tuesday, July 19, 2005 11:01:26 AM (Central Europe Standard Time, UTC+01:00)
1. Monitor.Enter(this);
...
Monitor.Exit(this);

lock(obj) keyword je v IL prepisane prave pomocou Monitor.Enter(obj) a Monitor.Exit(obj)

2. Mutex mutex = new Mutex();
mutex.WaitOne();
...
mutex.ReleaseMutex();
Tuesday, July 19, 2005 11:01:27 AM (Central Europe Standard Time, UTC+01:00)
Shut, musim sa naucit rychlejsie pisat, ale cestne prehlasujem, ze v case reply som Andrejov post nevidel ;-)

Btw., to finally je dobry napad, s tym som nepocital...
Tuesday, July 19, 2005 11:01:27 AM (Central Europe Standard Time, UTC+01:00)
S Monitor.Enter bez vyhrad souhlasim, mutex by slo take pouzit, ale je to prilis nakladny zpusob synchronizace. Existuje jeste jedna technika, ktera je mnohem elegantnejsi.;)
Tuesday, July 19, 2005 11:01:27 AM (Central Europe Standard Time, UTC+01:00)
Mno, kedze sa jedna len o jednoduche priradenie hodnot do premennych, malo by sa dat pouzit aj Interlocked.Exchange(...)

Interlocked.Exchange(ref x, x + arg);
Interlocked.Exchange(ref y, y + arg);

Ak nie takto, tak potom snad este cez ...
Tuesday, July 19, 2005 11:01:27 AM (Central Europe Standard Time, UTC+01:00)
To jsou divocarny;)
Ne, existuje jeste jeden jednodussi zpusob...
Tuesday, July 19, 2005 11:01:27 AM (Central Europe Standard Time, UTC+01:00)
A co pouzit dekoraci:

[MethodImpl(MethodImplOptions.Synchronized)] ?

TR.
Tuesday, July 19, 2005 11:01:27 AM (Central Europe Standard Time, UTC+01:00)
To Tomas Rampas: Vyborne. Protoze je cele telo metody v sekci lock, tak mohu pouzit atribut [MethodImpl(MethodImplOptions.Synchronized)]
Tuesday, July 19, 2005 11:01:27 AM (Central Europe Standard Time, UTC+01:00)
Me spis ZaraZily casy poslednich 3 prispevku. Chlapi, to vaZne makate od "pred sedmou" ? Nebo ste nekde mimo Z casovym posunem? Co vam pomaha k nabootovani?
Tuesday, July 19, 2005 11:01:27 AM (Central Europe Standard Time, UTC+01:00)
Ty casy jsou posunute o hodinu.
Spatny cas nas erveru? Nicmene ja vstavam v 6:40, coz mi prijde taky jako nekrestanska hodina:)
Tuesday, July 19, 2005 11:01:27 AM (Central Europe Standard Time, UTC+01:00)
To filemon: BTW: Diky za odkaz na vasich strankach, revansuji se:)
Tuesday, July 19, 2005 11:01:27 AM (Central Europe Standard Time, UTC+01:00)
k reseni Tomase Rampase:
- clovek se porad uci.....

pozitivum je, ze stale narazi na nove a zajimave veci. Negativum, ze nevidi konce :-)))
Tuesday, July 19, 2005 11:01:27 AM (Central Europe Standard Time, UTC+01:00)
2 filemon: me velmi ucinne pomahaji deti :-), cloveku nic jineho nezbyva. Uz me skoro prevychovali z nocniho ptaka na normalniho cloveka :-)
Tuesday, July 19, 2005 11:01:27 AM (Central Europe Standard Time, UTC+01:00)
Jenom snad to ze

Interlocked.Exchange(ref x, x + arg);
Interlocked.Exchange(ref y, y + arg);

samozrejme neni ekvivalentni reseni protoze nezarucuje ze se promena x i y zmeni atomicky v ramci jedne "transakce".
Tuesday, July 19, 2005 11:01:27 AM (Central Europe Standard Time, UTC+01:00)
To Petr Lazecky: Diky za doplneni, ja jsem to shrnul slovem "divocarny" rovnou pod prispevkem ;)
Comments are closed.