UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2007-01-26 03:30 PM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: function pointer

Maybe it'll be better just to show a piece of code:

template<class T>
void SrvlMan<T>::CreateServlets()
{
	int i;
	pthread_t thread;
	void * (SrvlMan<T>::*srvl_run)(void*)	= &SrvlMan<T>::ServletRun;
	for ( i = 0; i < m_nCountSrvls; ++i )
	{
		m_nStart[i] = i*m_nSrvlRange;
		if ( pthread_create(&thread, 0, srvl_run, (void*)(m_nStart+i)) == 0 )
		{
			pthread_detach(thread);
		} else
		{
			LOGTS( <<"Can't create thread " << i );
		}
	}
}


cannot convert `void*(SrvlMan<abuffer>::*)(void*)' to `
   void*(*)(void*)' for argument `3' to `int pthread_create(pthread_t*, const
   pthread_attr_t*, void*(*)(void*), void*)'

How is it done in correct way?

Offline

#2 2007-01-26 04:07 PM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: function pointer

To say the truth I can imagine it is impossible.Because If the object calls the function,that creates thread,which will execute another function foo, then function foo can't see internal members of the object,that was created in another thread.So void * (SomeClass::*pFoo)(void *) is not the same, that void *(*)(void *) is?

Offline

#3 2007-01-26 04:38 PM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: function pointer

I tried to do smth like this:

class SrvlMan;
class chuck
{

}

Offline

#4 2007-01-26 04:52 PM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: function pointer

I tried smth like this:

class SrvlMan;

class chack
{
 public:
    ParamType m_threadParam;
    SrvlMan *  m_pSrlvMan;
}

class SrvlMan
{
 friend class chack;
protected:
   void CreateSrlvs();
   void SrvlFunc();
}

void * Chack(void * param)
{
 chack * p=static_cast<chack*>(param);
 p->m_pSrvlMan->ServlFunc(p->m_threadParam);
}

void SrvlMan::CreateSrvls()
{
   hack H;
   H.m_pSrvlMan
  for (int i=0;i<nThreads;++i)
  {
    H.m_threadParam=i;                   //for example
    pthread_create(&thread,0,Chack,(void*)(&H));
  }
}


Almost all is ok, however get error SrvlFunc is protected

Offline

#5 2007-01-26 07:09 PM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: function pointer


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

#6 2007-01-27 11:35 AM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: function pointer

Yes, thank you that you pointed at that problem, when the chack is deleted.This was a detail that i missed beacuse I was happy to find a decision so easy and fast. )

Offline

Board footer

Powered by FluxBB