Click to See Complete Forum and Search --> : using osame function in two worker threads


asafaa
December 8th, 2004, 07:48 AM
Can i use the same function inside two worker threads that work independently?
i already know that i need to use CriticalSection on Buffers,variables and other objects , do functions need that kind of treatment too ?

More specific :
i have an MDI Doc/View project which the application class has a function that two other classes with worker thread need.
the both thread in both classes use it :

App->DoFunc() // in both threads

Tischnoetentoet
December 8th, 2004, 08:03 AM
if they have to work independent, you can create class that is derived from CWinThread.

You can create an object of this class, and then let it run.

If the functions have to work together, think of a singleton class.

asafaa
December 8th, 2004, 08:16 AM
sorry, Tischnoetentoet but i didn't understand your answer.
Does functions need a critical section treatment like objects do ?
is there a chance that if two threads use the same function , i will get an access violation ?

Tischnoetentoet
December 8th, 2004, 08:20 AM
well, if you create a class, say CMyThread, derived from CWinThread.

If you make 2 objects of this thread. One object for each view.

pMyThread = new CMyThread();

Now, the threads can run for each view. Because you have different objects, they will not give any errors, unless you let them edit the same file or something like that.

Is it more clear now?

Andreas Masur
December 8th, 2004, 08:22 AM
[ Moved thread ]

Andreas Masur
December 8th, 2004, 08:31 AM
There aren't any problems if the following is true:

The thread function is reentrant.
Access to shared data is synchronized (preferrable at data level)
Functions from external libraries used by the function need to be thread-safe as well (or need to be covered with synchronization)
No static or global variables (->race conditions)