pthread_create in class method
2013-10-08 10:44
204 查看
pthread_create can not use class normal methods directly, but can use static methods. Here is a example:
//g++ -o test test.c -lpthread
#include <iostream>
#include <pthread.h>
using namespace std;
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) {}
};
};
Testing::Testing( )
{
cout << "Called constructor of Testing...\n";
}
Testing::~Testing( )
{
cout << "Called Desconstructor of Testing...\n";
}
void* Testing::writeToLog( void* )
{
return NULL;
}
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 ;
}
int main()
{
Testing t;
t.floodLog(2);
return 0;
}
Cited from http://www.daniweb.com/forums/thread115838.html
//g++ -o test test.c -lpthread
#include <iostream>
#include <pthread.h>
using namespace std;
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) {}
};
};
Testing::Testing( )
{
cout << "Called constructor of Testing...\n";
}
Testing::~Testing( )
{
cout << "Called Desconstructor of Testing...\n";
}
void* Testing::writeToLog( void* )
{
return NULL;
}
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 ;
}
int main()
{
Testing t;
t.floodLog(2);
return 0;
}
Cited from http://www.daniweb.com/forums/thread115838.html
相关文章推荐
- java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class
- Cannot refer to a non-final variable inside an inner class defined in a different method
- IllegalArgumentException in class: my.models.base.BaseVo, getter method of property: id
- Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; neste
- The method setClass(Context, Class<?>) in the type Intent is not applicable for the问题
- Part 61 to 63 Talking about partial class and partial method in C#
- 在liunx编译C语言程序出现Undefined reference to pthread_create in Linux 错误*
- 解决FATAL ERROR in native method: JDWP on getting class status, jvmtiError=JVMTI_ERROR_WRONG_PHASE
- What happens when more restrictive access is given to a derived class method in C++?
- The ServiceClass object does not implement the required method in the following form: OMElement sayHello(OMElement e)
- java.lang.IllegalStateException: Could not find a method false(View) in the activity class com.xuan
- BaiduMap提示 java.lang.UnsatisfiedLinkError: Native method not found: com.baidu.mapapi.Mj.initClass:(L
- Cannot refer to a non-final variable hql inside an inner class defined in a different method
- JAVA错误:Cannot refer to a non-final variable * inside an inner class defined in a different method
- python调用py方法,报错【ValueError: no such test method in <class 'mytestcase.MyTestCase'>: runTest】
- Insert Features using Ifeatureclass.createFeaturebuffer method
- 接入百度地图api报错 Native method not found: com.baidu.platform.comjni.tools.JNITools.initClass
- 异常:Absent Code attribute in method that is not native or abstract in class
- In Javascript Class, how to call the prototype method.(three method)
- Eclipse:Could not create the view: Plug-in org.eclipse.jdt.ui was unable to load class org.eclipse.j