CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2010
    Posts
    1

    Unhappy 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();
    }
    }
    ....

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Update the shared variable in two parallel threads

    1. What does this code snippet belong to?
    2. What is "mt.Lock();/mt.Unlock();"?
    3. 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

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    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

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Update the shared variable in two parallel threads

    Quote Originally Posted by VictorN View Post
    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.

  5. #5
    Join Date
    May 2006
    Location
    Indonesia & Japan
    Posts
    399

    Re: Update the shared variable in two parallel threads

    Quote Originally Posted by suwaxnj View Post
    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

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Update the shared variable in two parallel threads

    Quote Originally Posted by Arjay View Post
    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


  7. #7
    Join Date
    May 2006
    Location
    Indonesia & Japan
    Posts
    399

    Re: Update the shared variable in two parallel threads

    Quote Originally Posted by Arjay View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured