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
- What does this code snippet belong to?
- What is "mt.Lock();/mt.Unlock();"?
- How was this "CArray ar" passed in your threads?
When I need to share some object between multiple threads I usually use CRITICAL_SECTION to prevent simultaneous access to the object. Something like:
Code:
::EnterCriticalSection(&crSection);
int index = sharedArray.Add(newValue);
::LeaveCriticalSection(&crSection);
where crSection is CRITICAL_SECTION instance.
BTW, if you used Code tags your code snippet would look much better and be much more understandable.
Re: Update the shared variable in two parallel threads
You can also look at this thread to see how you can very easy create your own thread-safe CArray derived class
Re: Update the shared variable in two parallel threads
Quote:
Originally Posted by
VictorN
You can also look at
this thread to see how you can very easy create your own thread-safe CArray derived class
The CProtectedArray shown isn't thread safe.
Re: Update the shared variable in two parallel threads
Quote:
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.
Re: Update the shared variable in two parallel threads
Quote:
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.
__________________________________________________________
Arjay
See my latest series on using WCF to communicate between a Windows Service and WPF task bar application.
Tray Notify - Part I Tray Notify - Part II
Need a little help with Win32 thread synchronization? Check out the following CG articles and posts:
Sharing a thread safe std::queue between threads w/progress bar updating
Simple Thread: Part I Simple Thread: Part II
Win32 Thread Synchronization, Part I: Overview Win32 Thread Synchronization, Part 2: Helper Classes
www.iridyn.com
Re: Update the shared variable in two parallel threads
Quote:
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.