Update the shared variable in two parallel threads
Anyone help me??
i have a CArray ar and when i edited it thread 1, the shared variable doesn't change in thread 2, both thread are running at the same time.. i used Mutex, but nothing happened... please help me!
this is the body of my thread
....
while(true)
{
int can_download;
int i;
mt.Lock();
int n= ar.GetCount();
for(i=0; i<n;i++)
if(strcmp(ar[i].filename, srfile)==0)
break;
if(ar[i].port_request == prt)
{
can_download = 1;
m_down.Send(&can_download, sizeof(int), 0);
m_down.Send(srfile, 32768, 0);
int don;
m_down.Receive(&don, sizeof(int), 0);
if(don == 1)
ar.RemoveAt(i);
mt.Unlock();
break;
}
else
{
n = ar.GetCount();
mt.Unlock();
}
}
....
Re: Update the shared variable in two parallel threads
Originally Posted by suwaxnj
Anyone help me??
i have a CArray ar and when i edited it thread 1, the shared variable doesn't change in thread 2, both thread are running at the same time.. i used Mutex, but nothing happened... please help me!
How do both threads access the array? Do they access the same object?
I guess each thread accesses different object.
--> It doesn't relate to locking mechanism you pointed. Locking is to
protect the data from accessed by multiple threads at the same time.
henky
---------------------------------- henky@nok.co.id is not my email address anymore... Jangan Pernah Menyerah
Re: Update the shared variable in two parallel threads
Originally Posted by Arjay
The CProtectedArray shown isn't thread safe.
To expand on this a bit. The example isn't thread safe because it only shows locking while adding items to the array. In order to be thread safe, locking must be performed while removing and reading items in the array.
Re: Update the shared variable in two parallel threads
Originally Posted by Arjay
To expand on this a bit. The example isn't thread safe because it only shows locking while adding items to the array. In order to be thread safe, locking must be performed while removing and reading items in the array.
That's true. Locking mechanism should be applied while adding, deleting and reading the items to make an array thread-safe.
Last edited by henky@nok.co.id; June 12th, 2010 at 09:12 AM.
henky
---------------------------------- henky@nok.co.id is not my email address anymore... Jangan Pernah Menyerah
Bookmarks