CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Mar 2004
    Posts
    43

    What would happen if call the same function at same time from two different place?

    I have a function that both the work thread and the UI thread will
    call it , i think there exist such a possibility that they will call it at the same time(of course,not real "the same time"),is there any kind of mutex needed ?

  2. #2
    Join Date
    Feb 2002
    Posts
    5,757
    Correct. Mutex is one solution. Given that both threads reside on the same process, a better solution could be a critical section, which takes up less resources.

    Kuphryn

  3. #3
    Join Date
    May 2003
    Location
    Pakistan
    Posts
    223

    Re: What would happen if call the same function at same time from two different place?

    Originally posted by lugangxyz
    I have a function that both the work thread and the UI thread will
    call it , i think there exist such a possibility that they will call it at the same time(of course,not real "the same time"),is there any kind of mutex needed ?
    Well if that functin does not perfrom any action on a shared data or resource then I think there is no need for locking that function.
    As use of locking undesirably may cause some perfromence issues.
    Unmanaged in a .NET world

  4. #4
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Originally posted by kuphryn
    Correct. Mutex is one solution. Given that both threads reside on the same process, a better solution could be a critical section, which takes up less resources.
    Incorrect...you do not need synchronization for functions...

  5. #5
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: What would happen if call the same function at same time from two different place?

    Originally posted by lugangxyz
    I have a function that both the work thread and the UI thread will
    call it , i think there exist such a possibility that they will call it at the same time(of course,not real "the same time"),is there any kind of mutex needed ?
    The answer to the above question is no since functions do not need synchronization. However, if this function accesses shared data, then the shared data access needs to be synchronized - which should be done at the data level rather than the function level...

  6. #6
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    [Moved thread]

  7. #7
    Join Date
    May 2003
    Location
    Pakistan
    Posts
    223

    Re: Re: What would happen if call the same function at same time from two different place?

    Originally posted by Andreas Masur
    If this function accesses shared data, then the shared data access needs to be synchronized - which should be done at the data level rather than the function level...
    Can u elaborate what is difference between function level and data level , as data cannot do anything at its own , to sysnchronize data one will have to syschronize the functrions which are operating on that .
    Unmanaged in a .NET world

  8. #8
    Join Date
    Jan 2004
    Posts
    56
    As far as I know, data synchronization is necessary only when there's "write-to-data" action. Read-only data (includes function) does not need to be synchronized.
    Trust urself!

  9. #9
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    First of all, do not take these words as standardized ones...I simply tried to refer to the situtation that one should always try to encapsulate resources. The base idea is to follow the RAII (Resource Acquisition Is Initialization) idiom...in other words..
    Code:
    struct SharedData
    {
    public:
      void SetInt(int value)
      {
        // Lock access
        SharedInt = value;
      }
    
      int GetInt() const
      {
        // Lock access
        return SharedInt
      }
    
    private:
      int SharedInt;
    };
    This would be an example to do the synchronization on data level. Note though, that in order to work correctly, the locking mechanism would also follow the RAII idiom, otherwise the resource would not be unlocked in the above functions (especially the 'GetInt()' function)...

  10. #10
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354
    Just wanted to add:

    The reason you don't have to bother about functions being called from different threads is because each thread executes in its own context. When say Thread A calls func f1() and in the middle of execution of f1(), there is a switch and Thread B gets its turn. Now, say Thread B also calls f1(). In that case, the stack maintained for Thread B is different, so, no matter where Thread A was at the time of switch, thread B is using it's own data. When thread A gets its chance again, all the context at the time of the switch earleir is restored and thread A resumes execution from where it left. So, you see, thread A is not even aware that such a switch happened and it is all transparent.

    Now, all this was from a functional point of view as Andreas noted. WHat happens when the f1 acts on some data that is kinda common, i.e. say, it is acting on some global resource or shared resource. In that case, you may, depending on the needs, want to synchronize access to maintain data integrity.

    Sometimes it may so happen that a bunch of data may have some relation to one another. Now, it there was no protection, then what would happen is , thread A might change some data and before it has changed the rest, thread B gets it;s time slice and it manipulates some other data. As a result , what could happen is that the data is all messed up and the resulting data set has lost integrity.

    So, if at all some protection is required, is dependent on what data is involved and what are the operations performed.

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