-
June 2nd, 2004, 07:54 PM
#1
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 ?
-
June 2nd, 2004, 11:53 PM
#2
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
-
June 3rd, 2004, 12:34 AM
#3
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
-
June 3rd, 2004, 04:10 AM
#4
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...
-
June 3rd, 2004, 04:12 AM
#5
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...
-
June 3rd, 2004, 04:14 AM
#6
-
June 3rd, 2004, 04:19 AM
#7
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
-
June 3rd, 2004, 04:48 AM
#8
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!
-
June 3rd, 2004, 04:48 AM
#9
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)...
-
June 3rd, 2004, 11:05 AM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|