Hello all,

This is my first post here, this board seems to contain interesting stuff regarding C++, that's why I subscribed into it.
Ok, let's go the problem...

I have a class with two methods into it:
floodLog
writeToLog

floodLog is intended to create threads that will execute the method writeToLog.

The problem is that I can't compile it, I'm receiving the following error:

"testing.cpp", line 54: Error: Cannot assign void*(Testing::*)(void*) to void*(*)(void*).

Below you can see the source code:

#include <iostream>
using namespace std;


#include <sys/types.h>
#include <sys/signal.h>
#include <sys/stat.h>
#ifndef AIX
#include <sys/siginfo.h>
#endif


#if defined sun
#include <sys/statvfs.h>
struct statvfs info;
#elif defined HPUX11
#include <sys/vfs.h>
struct statfs  info;
#elif defined AIX
#include <sys/vfs.h>
struct statfs  info;
#else
#include <sys/mount.h>
struct statfs  info;
#endif


#include <cLog.h>
#include <pthread.h>


const char *mpszFileVersion[] = {"a", "b"};
const char *mpszEnvironment[] = {"c", "d"};
char *SCCS_LONGVERSION ;


#define LOGLEVEL 4


class Testing
{
private:
cLog* m_Log;
pthread_t tid;
void* (*fPtr)( void* );


public:
Testing( );
~Testing( );
void floodLog( int nThreads );


private:
void* writeToLog( void* );
};


Testing::Testing( )
{
cout << "Called constructor of Testing...\n";
m_Log = new cLog("TESTING", LOGLEVEL, 5242880);
fPtr = writeToLog;
}


void* Testing::writeToLog( void* )
{
m_Log->Write( LOGLEVEL, "Another thread writting...\n" );


return NULL;
}


void Testing::floodLog( int nThreads )
{
int i = 0;


cout << "Flooding cLog class with " << i << " threads.\n";


for( i = 0; i < nThreads; i++ )
{
cout << "Created Thread: " << ( i + 1 ) << endl;


pthread_create( &tid, NULL, fPtr, NULL );
}


cout << "Finished flooding.\n";
}


int main( )
{
Testing test;


cout << "Now in main.\n";


test.floodLog( 10 );


return 0;
}

Thanks in advance,

Regards,

Caio.

non-static member functions have an implicit argument: the this pointer (a pointer to the object the function is called on). a pointer to a free function has nothing of this kind. the dereference operators on pointers to members ( .* and ->* ) are binary operators; you dereference a pointer to a member with respect to an object on which the member is applied. it is best to think of pointers to members as opaque abstractions that aren't really pointers (in the C sense).

the usual work around is something like this:

class Testing
{
  private:
    cLog* m_Log;
    pthread_t tid; // would one suffice?
    // std::vector<pthread_t> tids ; ?

    public:
      Testing( );
      ~Testing( );
      void floodLog( int nThreads );

    private:
      void* writeToLog( void* );
      
      static void* thread_fun( void* args ) ;

      struct thread_fun_args
      {
        Testing* This ;
        // other args as required; in this case
        void* actual_arg ;
        thread_fun_args( Testing* t, void* p ) 
            : This(t), actual_arg(p) {}
      };
};

void Testing::floodLog( int nThreads )
{
  cout << "Flooding cLog class with " << nThreads << " threads.\n";
  for( int i = 0; i < nThreads; i++ )
  {
    cout << "Created Thread: " << ( i + 1 ) << endl;
    pthread_create( &tid, NULL, &Testing::thread_fun, 
                            new thread_fun_args(this,0) );
  }
  cout << "Finished flooding.\n";
}

void* Testing::thread_fun( void* pv )
{
    thread_fun_args* tf_args = static_cast<thread_fun_args*>(pv) ;
    Testing* This = tf_args->This ;
    void* args = tf_args->actual_arg ;
    void* result = This->writeToLog( args ) ;
    delete tf_args ;
    return result ;
}

note: you may want to consider using asynchronous write to the file (instead of pthreads).
http://docs.sun.com/app/docs/doc/816-5171/aio-write-3rt?l=ru&a=view
http://publib.boulder.ibm.com/infocenter/pseries/v5r3/index.jsp?topic=/com.ibm.aix.basetechref/doc/basetrf1/aio_write.htm
you would get better performance (and the design would perhaps be easier).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.