Launching multiple threads using member functions 
Author Message
 Launching multiple threads using member functions

I'm currently using the member-function thread-creation structure (due
to Mr Newcomer) in a dialog app:

AfxBeginThread(CheckPg, this, THREAD_PRIORITY_LOWEST);
.
.
UINT CMyDlg::CheckPg(LPVOID p) {
        CMyDlg *cmydlg = (CMyDlg *)p;
        cmydlg->CheckPg();
        return 0;

Quote:
}

.
.
void CMyDlg::CheckPg() {
.
.

This works fine, but I want to generalize to multiple threads.  Can I
simply enclose the AfxBeginThread statement in a for loop to launch
multiple threads?  i.e.:

for(i=0; i<n; i++) {
        AfxBeginThread(CheckPg, this, THREAD_PRIORITY_LOWEST);

Quote:
}

The fact that each thread is not handed unique data is not an issue;
each thread would retrieve its own data from a controlled-access list.
I know that a global function is an option, but prefer to use a member
function if possible.

Thanks in advance.
Max
p.s. I'm reposting this, as I can't see (with my newsreader) my original
post from yesterday.



Fri, 21 Oct 2005 14:25:21 GMT  
 Launching multiple threads using member functions
Hi Max,

You can enclose the beginthread() inside a for loop but only of you want
these different threads to use the same instance of your CMyDialog (as you
are passing this instance as 'this' to the checkPg())

If you want to have different instances of CMyDialog(s) to be passed to
checkPg() then do something like:
for (i=0;i<mycount;i++)
{
  CMyDialog *d = new CMyDialog;
  d->Create(IDD_DIALOG1);
  AfxBeginThread(CheckPg, (LPVOID)d, THREAD_PRIORITY_LOWEST);

Quote:
}

Then later inside CheckPg() do remember to call delete mydialog_instance to
free the passed instance.

Regards,
Elias

Quote:
> I'm currently using the member-function thread-creation structure (due
> to Mr Newcomer) in a dialog app:

> AfxBeginThread(CheckPg, this, THREAD_PRIORITY_LOWEST);
> .
> .
> UINT CMyDlg::CheckPg(LPVOID p) {
> CMyDlg *cmydlg = (CMyDlg *)p;
> cmydlg->CheckPg();
> return 0;
> }
> .
> .
> void CMyDlg::CheckPg() {
> .
> .

> This works fine, but I want to generalize to multiple threads.  Can I
> simply enclose the AfxBeginThread statement in a for loop to launch
> multiple threads?  i.e.:

> for(i=0; i<n; i++) {
> AfxBeginThread(CheckPg, this, THREAD_PRIORITY_LOWEST);
> }

> The fact that each thread is not handed unique data is not an issue;
> each thread would retrieve its own data from a controlled-access list.
> I know that a global function is an option, but prefer to use a member
> function if possible.

> Thanks in advance.
> Max
> p.s. I'm reposting this, as I can't see (with my newsreader) my original
> post from yesterday.



Fri, 21 Oct 2005 15:04:20 GMT  
 Launching multiple threads using member functions

Quote:
> I'm currently using the member-function thread-creation structure (due
> to Mr Newcomer) in a dialog app:

> AfxBeginThread(CheckPg, this, THREAD_PRIORITY_LOWEST);
> .
> .
> UINT CMyDlg::CheckPg(LPVOID p) {
> CMyDlg *cmydlg = (CMyDlg *)p;

Better use C++ style of casting:

CMyDlg* pDlg = reinterpret_cast<CMyDlg*>( p );

Quote:
> cmydlg->CheckPg();

Be sure that this method does not touch *any* of the GUI parts; exception to
this is posting messages to HWND's.

Quote:
> return 0;
> }
> .
> .
> void CMyDlg::CheckPg() {
> .
> .

> This works fine, but I want to generalize to multiple threads.  Can I
> simply enclose the AfxBeginThread statement in a for loop to launch
> multiple threads?  i.e.:

> for(i=0; i<n; i++) {
> AfxBeginThread(CheckPg, this, THREAD_PRIORITY_LOWEST);
> }

This is a way of doing it.

Quote:
> The fact that each thread is not handed unique data is not an issue;
> each thread would retrieve its own data from a controlled-access list.
> I know that a global function is an option, but prefer to use a member
> function if possible.

Use a static member function. This function can be passed as a parameter to
AfxBeginThread (if it has the correct signature!):

// header
class CMyDlg {
    // ...
    static UINT CheckPg(LPVOID p);

Quote:
};

// impl.
UINT CMyDlg::CheckPg( LPVOID p )
{
    /* ... */

Quote:
}
> Thanks in advance.
> Max

HTH,
Tom T.


Fri, 21 Oct 2005 15:28:11 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Using class member function as a thread function

2. Using class member function as a thread function

3. Another thread as a member function thread

4. using non-static member functions using delegates

5. Using Non-Static Callback Functions as member Functions VC5.0

6. Get access to a view's member var from another view using multiple templates

7. Multiple inheritance of abstract classes with same member functions

8. Multiple inheritance of abstract classes with same member functions

9. static function access member variable and member function

10. Visual C++ - Threads and Member Functions

11. member function as thread??

12. Starting a thread which is a member function of a class

 

 
Powered by phpBB® Forum Software