|
-
June 5th, 2010, 10:28 PM
#1
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();
}
}
....
-
June 6th, 2010, 06:20 AM
#2
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.
Victor Nijegorodov
-
June 6th, 2010, 06:22 AM
#3
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
Victor Nijegorodov
-
June 8th, 2010, 10:32 PM
#4
Re: Update the shared variable in two parallel threads
 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.
-
June 11th, 2010, 12:12 AM
#5
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
----------------------------------
[email protected] is not my email address anymore...
Jangan Pernah Menyerah
-
June 11th, 2010, 03:50 PM
#6
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.
__________________________________________________________
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
-
June 12th, 2010, 09:08 AM
#7
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 [email protected]; June 12th, 2010 at 09:12 AM.
henky
----------------------------------
[email protected] is not my email address anymore...
Jangan Pernah Menyerah
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|