Here is how to make variable thread safe in java. Make it private and accessible via synchronized public function call.
public class MyCounter
{
private int count = 0; // count starts at zero
public synchronized void setCount(int amount)
{
count = amount;
}
public synchronized int getCount()
{
return count;
}
}
In C/C++, mutex, would be necessary.
No comments:
Post a Comment