Ahoj jak mam synchronizovat tento priklad pomoci lock aby mi soucet dal milion
Sem to zkousel ale nefunguje to, diky za rady a popripade nejake vysvetleni jak ten lock pracuje
namespace vlaknalock
{
class Program
{
static void Main(string[] args)
{
int size = 20;
Thread[] vlakno = new Thread[size];
Pricist aaa = new Pricist(0);
for (int i = 0; i < size; i++)
{
vlakno[i] = new Thread(aaa.pricitam);
}
for (int i = 0; i < size; i++)
{
vlakno[i].Start();
}
Console.WriteLine("Soucet {0} ",aaa.polozka);
}
}
class Pricist
{
public int polozka;
public Pricist(int polozka)
{
this.polozka = polozka;
}
/* public int Polozka
{
get
{
return polozka;
}
}*/
public void pricitam()
{
for (int i = 0; i < 50000; i++)
{
lock(this)
{
polozka += 1;
}
}
}
}
}