mfc >> Member function and multithread

by garyolsen » Sat, 29 Nov 2003 05:38:42 GMT

A class is declared as:

class Test
{
public:
void StringMethod(char a[]) {...}
void IntMethod(int b) {...}
};

and two instances of the Test have been created, OTest1, OTest2.

In Thread A, it calls StringMethod() and IntMethod():

char aString[] = "dsfgsd";
int nInt = 123;
OTest1.StringMethod(aString);
OTest1.IntMethod(nInt);


In Thread B, it calls StringMethod() and IntMethod() also:

char aString[] = "hjkhgjk";
int nInt = 5678;
OTest2.StringMethod(aString);
OTest2.IntMethod(nInt);

My questions are:
Should the calls to StringMethod() and IntMethod() be synchronized?


Now, put the thread function inside the class declaration and keep the rest
the same, OTest1 and OTest2, etc. Thread A and B now run in the scope of
class Test.

If aString or nInt are local variables inside the thread function,

class Test
{
public:
void StringMethod(char a[]) {...}
void IntMethod(int b) {...}

static unsigned __stdcall thread_cal(void * ar)
{
char aString[] = "hjkhgjk";
int nInt = 5678;
StringMethod(aString);
IntMethod(nInt);
}

};

should the calls (StringMethod/IntMethod) be synchronized?


If aString or nInt become member variables inside of class Test, and again
two Test objects, OTest1 and OTest2, are created:

class Test
{
public:
static unsigned __stdcall thread_cal(void * ar)
{
StringMethod(aString);
IntMethod(nInt);
}

private:
char *aString;
int nInt;

void StringMethod(char a[]) {...}
void IntMethod(int b) {...}

};

should the calls (StringMethod/IntMethod) be synchronized?














mfc >> Member function and multithread

by GuitarBill » Sat, 29 Nov 2003 06:56:24 GMT


If I understand you correctly...
There's no reason why they would be synchronized. Each
thread could end up executing any piece of code at any
time; there's nothing to tell them when they should do
anything relative to each other.
The functions being members of a class is irrelevant; they
are just functions.

To synchronize them you would need to do something like
SetEvent(hevent) at the end of one call and have the other
thread waiting in WaitForSingleObject(hevent) before it
proceeds.

BTW your static thread_cal will bring a compiler error
because its making a call to non-static members (the
static call doesn't have an instance of the Test class to
operate on).


OTest2.
synchronized?
and keep the rest
run in the scope of
function,
class Test, and again



mfc >> Member function and multithread

by Scott McPhillips [MVP] » Sat, 29 Nov 2003 09:14:49 GMT




> <two more cases snipped>

No, for all the cases. Synchronization is needed only when the same
data is accessed by multiple threads. Synchronization has nothing to do
with functions or classes - just data values that are concurrently
accessed, such as a global variable or a piece of data that two threads
both have a pointer to.

--
Scott McPhillips [VC++ MVP]



Similar Threads

1. Multithread (MT) vs. Multithread DLL (MD) option

2. Multithread or Multithread DLL?

Hello,

I'm trying to create a worker thread for my VC++ program, and was
wondering whether I should be linking with the Multithread /MT or
Multithread DLL /MD option?  I'm not quite sure, in layman's terms,
what the difference is.

When I build with Multithread DLL, the linker complains about not
being able to find a bunch of "unresolved external symbols" associated
with nafxcwd.lib.

When I build with Multithread, it completes, but when I run, I'm
getting a "Debug Assertion Failed!" error message, associated with
thrdcore.cpp.

Any insight/help appreciated.

Robert

( modify address for return mail )

www.numbersusa.com
www.americanpatrol.com

3. change library from not-multithread to multithread RAD Studio 2007 - C++ Builder IDE

4. specialized member function takes precedence over generic template member function

Here is the compilable code, along w/ the error

#include<iostream>
#include<complex>
typedef std::complex<float> ComplexSingle;
using namespace std;
template<typename T> class X
{
private:
  T number;
public:
  X(T value)
  {
    number=value;
  }
  template<typename Other>  X(Other Y)
  {
    assign(Y);
  }
  template<typename Other> void assign(Other Y);


  T return_number()
  {
    return number;
  }
};

template<typename T>
template<typename Other> void X<T>::assign( Other Y)
{
  number=Y.return_number();
}

template<> template<typename ComplexSingle> void
X<float>::assign(ComplexSingle Y)
{
  number=norm(Y.return_number());
}
int main (void)
{
  //this works fine
  ComplexSingle a(2,3);
  X<ComplexSingle> A(a);
  X<float> B(A);


 //error
 //In member function `void X<T>::assign(Other) [with Other =
 //X<double>, T = float]':
 //ex1.cc:16:   instantiated from `X<T>::X(Other) [with Other =
 // X<double>, T = float]'
 //ex1.cc:45:   instantiated from here
 //ex1.cc:35: error: no matching function for call to `norm(double)'
  
  X<double> C(3.3);
  X<float> D(C);
  return 0;
}

5. Passing a member function as a parameter of a member function

6. Member function pointer to member function of another class

Hi,
I'm having huge syntax problem dealing with function pointers. Here's
what I'm doing:

I have 2 classes, ClassA and ClassB. ClassA owns an instance of
ClassB. What I want to do is pass a function pointer to a member
function of ClassB as a parameter to a member function of ClassA. I
don't want ClassA's member function to call ClassB's function directly
because ClassA can be used in 2 ways, one of which is a case in which
it does not create an instance of ClassB; hence by passing a function
pointer I can handle that case easily.

This is the code I have:

class ClassB {
  public:
    BOOL GetFlagX(void) { return m_bFlagX;};
  private:
    BOOL m_bFlagX;
};

class ClassA {
  public:
    typedef BOOL (ClassA::*FPAbortFlag)(void);
    FPAbortFlag m_fpAbort;

  protected:
    ClassB *m_pClassB;
    BOOL ClassAMemFcn(FPAbortFlag fpAbort);
};

In this case ClassA has new'd an instance of ClassB. What I want to do
is have ClassA's function pointer 'm_fpAbort' point to ClassB's
'GetFlagX' function. Can't seem to figure out the syntax for this.

I did get the following to compile but it wasn't pointing to the
member function associated with ClassA's instance of ClassB:

m_fpAbort = (FPAbortFlag)(&(ClassB::GetFlagX));

How do I do this?

Thanks in advance,
Ashish.

7. Member function pointers to member functions with default arguments

8. pointer to member function and pointer to constant member function

Are these pointers the same?

Fraser.