Pagini: [1]   În jos
  Imprimă  
Ajutor Subiect: Broken counter  (Citit de 2903 ori)
0 Utilizatori şi 1 Vizitator pe acest subiect.
Cosmin
Echipa infoarena
Nu mai tace
*****

Karma: 351
Deconectat Deconectat

Mesaje: 1.799



Vezi Profilul
« : August 22, 2012, 23:33:51 »

http://infoarena.ro/blog/counter
Memorat
marius135
Echipa infoarena
Client obisnuit
*****

Karma: 19
Deconectat Deconectat

Mesaje: 56



Vezi Profilul
« Răspunde #1 : August 23, 2012, 00:13:01 »

eu votez 10 in baza 2 sau 10 pastrez suspans Smile
Memorat
blue_phoenix
Client obisnuit
**

Karma: 0
Deconectat Deconectat

Mesaje: 57



Vezi Profilul
« Răspunde #2 : August 23, 2012, 00:25:56 »

deci... intrebarea e care e numarul maxim de coliziuni care pot aparea? (coliziune= moment in care doua sau mai multe fire incearca sa incrementeze variabila in aproape acelasi timp; daca asta se intampla variabila va fi incrementata numai de un fir, si anume de primul, sau nu va fi incrementata deloc? );
Memorat
Prostu
Nu mai tace
*****

Karma: 134
Deconectat Deconectat

Mesaje: 323



Vezi Profilul
« Răspunde #3 : August 23, 2012, 01:32:40 »

The fact that you chose python for this code is very confusing to me.
« Ultima modificare: August 23, 2012, 02:19:57 de către Stefan-Alexandru Filip » Memorat
Cosmin
Echipa infoarena
Nu mai tace
*****

Karma: 351
Deconectat Deconectat

Mesaje: 1.799



Vezi Profilul
« Răspunde #4 : August 23, 2012, 03:07:43 »

Think of it as a pseudocode example, instead of Python if that makes it less confusing Smile.

Python threading sucks but the code is somewhat shorter than a Java equivalent.
Memorat
Cosmin
Echipa infoarena
Nu mai tace
*****

Karma: 351
Deconectat Deconectat

Mesaje: 1.799



Vezi Profilul
« Răspunde #5 : August 23, 2012, 03:55:48 »

@t3st your example has a small bug.
Memorat
raduberinde
Strain
*

Karma: 13
Deconectat Deconectat

Mesaje: 26



Vezi Profilul
« Răspunde #6 : August 23, 2012, 05:51:23 »

2:
Thread 1 reads value 0
Thread 2 does 9 iterations, thread 3-5 all their 10 iterations (the ordering doesn't matter)
Thread 1 writes value 1
Thread 2 reads value 1
Thread 1 reads and writes etc until it completes all its iterations
Thread 2 writes value 2

Basically what t3st said, he was just missing "thread 1 completes the rest of the iterations" before thread 2 does the final write.
Memorat
Cosmin
Echipa infoarena
Nu mai tace
*****

Karma: 351
Deconectat Deconectat

Mesaje: 1.799



Vezi Profilul
« Răspunde #7 : Septembrie 04, 2012, 22:56:15 »

As Radu and others have said the answer is 2.
Memorat
Cosmin
Echipa infoarena
Nu mai tace
*****

Karma: 351
Deconectat Deconectat

Mesaje: 1.799



Vezi Profilul
« Răspunde #8 : Septembrie 07, 2012, 22:36:52 »

Radu, I've tried with more threads and more increments and you start seeing interesting numbers even if Python uses GIL.
Memorat
Pagini: [1]   În sus
  Imprimă  
 
Schimbă forumul:  

Powered by SMF 1.1.19 | SMF © 2006-2013, Simple Machines