Hi William,
Yes, I thought Interlocked does implicit lock or it doesn't really useful,
especially the variable is only simply advance itself.
I have abstract code following. I have a brife explain here first.
It is a client/Server application. There are 3 roles runs on differant
machines: Worker, Manager, and ApplicationServer.
Each worker works on a Item at a time.
When worker done a Item, it notifies server, and server advances the
project's current count if it is a "success" Item.
Each worker owns a thread in ApplicationServer, AppServer
talks(listen/write) to worker though that thread.
ApplicatonServer also have another backend thread run on an interval base to
read projects current value and update Managers. It check an arraylist to
see what projects need update. The project id is added to this arraylist
when worker's thread change current count in appServer.
Actual application is much more complicated then this. The following is
original code, and I am changing code to the one in comments.
public class Project
{
....
//later add this: private readonly object currentCountLock = new object();
private int currentCount; //later add volatile
public int CurrentCount
{
get
{
return this.currentCount;
}
}
/// <summary>
/// advance current by 1, in a atomic mode
/// </summary>
private void AdvanceCurrentCount()
{
Interlocked.Increment(ref this.currentCount);
//lock(this.currentCountLock)
//{
//this.currentCount ++;
//}
this.CheckIfFullAndMarkPPIfSo();
}
//80 workers' thread call this when it receive notificaton from workers
public bool DoneAnItem( ...)
{
...
if(successEnd)
{
AdvanceCurrentCount();
}
server.InfoClientQuotaChange(this);
}
}
public class Server
{
private ArrayList statusChangedProjects= new ArrayList();
// register status changed project id in the ArrayList
// then there is another thread scan this ArrayList
// to find out which project status
internal void InfoClientQuotaChange(Project project)
{
lock(this.statusChangedProjects.SyncRoot)
{
this.statusChangedProjects.Add(project.projectId);
}
}
.........
//backend thread
while(true)
{
foreach(Project p in statusChangedProjects)
{
sb.Append(p.CurrentCount);
}
//send sb.ToString() to manager
Thead.Sleep(500);
}
}
Thanks,
Ryan
"William Stacey [C# MVP]" <wi************@gmail.comдÈëÓʼþ
news:OK**************@TK2MSFTNGP06.phx.gbl...
I am not following that. You seem to be mixing locks and interlocked.
Can
we see a better sample of what your talking about.
--
William Stacey [C# MVP]
"Ryan Liu" <ad********@online.sh.cnwrote in message
news:eh***************@TK2MSFTNGP02.phx.gbl...
| Hi,
|
| Is there any known bug related to
| Interlocked.Increment(ref var)?
|
| My client report var's value going up and down in the client/server
| multile-thread application. There are about 80 clients connect to one
server
| though TCP client.
|
| But actully I never decrease var.
|
| I change its type to volatile int and use lock to increase its value
|
| lock(this.currentCountLock)
| {
| this.currentCount ++;
| }
| and handle over to my client. (It never decreases in my machine)
|
|
| Thanks,
| Ryan
|
| BTW, why volatile can not be used with ref?
|
|