围绕pthread_t的一个非常简单的C ++抽象...... [英] a really simple C++ abstraction around pthread_t...

查看:56
本文介绍了围绕pthread_t的一个非常简单的C ++抽象......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在所有的C ++项目中使用以下技术;这里是示例

代码,为简洁起见省略了错误检查:

__________________________________________________ _______________

/ *简单线程对象

__________________________________________________ ____________ * /

#include< pthread.h>

externC" void * thread_entry(void *);


class thread_base {

pthread_t m_tid;

friend void * thread_entry(void *) ;

虚拟空洞on_active()= 0;


public:

virtual~thread_base()= 0;


void active_run(){

pthread_create(& m_tid,NULL,thread_entry,this);

}


void active_join(){

pthread_join(m_tid,NULL);

}

};


thread_base :: ~thread_base(){}

void * thread_entry(void * state){

reinterpret_cast< thread_base *> ;(州) - > on_active();

返回0;

}


模板< typename T>

struct active:public T {

active():T(){

this-> active_run();

}


~active(){

this-> active_join();

}


模板< typename T_p1>

有效(T_p1 p1):T(p1){

this-> active_run();

}


模板< typename T_p1,typename T_p2>

有效(T_p1 p1,T_p2 p2):T(p1,p2){

this-> active_run();

}


// [以及更多参数......]

};


/ *简单用法示例

__________________________________________________ ____________ * /

#include< string>

#include< cstdio>

class worker:public thread_base {

std :: string const m_name;


void on_active(){

std :: printf("(%p) - > worker(%s):: on_thread_entry()\ n",

(void *)this,m_name.c_str());

}


public:

worker(std :: string const&姓名)

:m_name(姓名){

std :: printf("(%p) - > worker(%s):: my_thread()\ n",

(void *)this,m_name.c_str());

}


~worker(){

std :: printf("(%p) - > worker(%s)::〜my_thread()\ n",

(void *)这个,m_name.c_str());

}

};

class another_worker:public thread_base {

unsigned const m_id;

std :: string const m_name;


void on_active(){

std :: printf(" ;(%p) - > my_thread(%u /%s):: on_thread_entry()\ n",

(void *)this,m_id,m_name.c_str()); < br $>
}


public:

another_worker(unsigned const id,std :: string const& name)

:m_id(id),m_name(name){

}

};

int main(void){

{

有效< workerworkers [] = {

" Chris",

" John",

" Jane",

" Steve",

" Richard",

" Lisa"

};


有效< another_workerother_workers [] = {

有效< another_worker>(21," Larry"),

有效< another_worker>(87," Paul"),

有效< another_worker>(43," Peter"),

有效< another_worker>(12," Shelly"),

} ;

}


std :: puts(" \ nn \ n \ n __________________ \ nhit< ENTER to exit ..." );

std :: fflush(stdout);

std :: getchar();

返回0;

}

__________________________________________________ _______________


我个人比Boost更喜欢这种技术。我发现它更直接

前进,也许更多面向对象,'活跃''

助手类的RAII性质也不会受到伤害。另外,我确实认为它更有效b&b高效。比起Boost创建线程的方式因为它没有b $ b复制任何东西......


恕我直言,关于它的真正好处就是它'主动''帮手

等级。它允许我运行并加入来自ctor / dtor的任何对象,即

公开(T :: active_run / join)的公共接口。此外,它允许我

将可变数量的参数直接传递给它直接包裹的对象

其ctor;这确实相当方便...

关于如何改进这种结构的任何建议?

I use the following technique in all of my C++ projects; here is the example
code with error checking omitted for brevity:
__________________________________________________ _______________
/* Simple Thread Object
__________________________________________________ ____________*/
#include <pthread.h>
extern "C" void* thread_entry(void*);

class thread_base {
pthread_t m_tid;
friend void* thread_entry(void*);
virtual void on_active() = 0;

public:
virtual ~thread_base() = 0;

void active_run() {
pthread_create(&m_tid, NULL, thread_entry, this);
}

void active_join() {
pthread_join(m_tid, NULL);
}
};

thread_base::~thread_base() {}

void* thread_entry(void* state) {
reinterpret_cast<thread_base*>(state)->on_active();
return 0;
}

template<typename T>
struct active : public T {
active() : T() {
this->active_run();
}

~active() {
this->active_join();
}

template<typename T_p1>
active(T_p1 p1) : T(p1) {
this->active_run();
}

template<typename T_p1, typename T_p2>
active(T_p1 p1, T_p2 p2) : T(p1, p2) {
this->active_run();
}

// [and on and on for more params...]
};


/* Simple Usage Example
__________________________________________________ ____________*/
#include <string>
#include <cstdio>
class worker : public thread_base {
std::string const m_name;

void on_active() {
std::printf("(%p)->worker(%s)::on_thread_entry()\n",
(void*)this, m_name.c_str());
}

public:
worker(std::string const& name)
: m_name(name) {
std::printf("(%p)->worker(%s)::my_thread()\n",
(void*)this, m_name.c_str());
}

~worker() {
std::printf("(%p)->worker(%s)::~my_thread()\n",
(void*)this, m_name.c_str());
}
};
class another_worker : public thread_base {
unsigned const m_id;
std::string const m_name;

void on_active() {
std::printf("(%p)->my_thread(%u/%s)::on_thread_entry()\n",
(void*)this, m_id, m_name.c_str());
}

public:
another_worker(unsigned const id, std::string const& name)
: m_id(id), m_name(name) {
}
};
int main(void) {
{
active<workerworkers[] = {
"Chris",
"John",
"Jane",
"Steve",
"Richard",
"Lisa"
};

active<another_workerother_workers[] = {
active<another_worker>(21, "Larry"),
active<another_worker>(87, "Paul"),
active<another_worker>(43, "Peter"),
active<another_worker>(12, "Shelly"),
};
}

std::puts("\n\n\n__________________\nhit <ENTERto exit...");
std::fflush(stdout);
std::getchar();
return 0;
}
__________________________________________________ _______________


I personally like this technique better than Boost. I find it more straight
forward and perhaps more object oriented, the RAII nature of the `active''
helper class does not hurt either. Also, I really do think its more
"efficient" than Boost in the way it creates threads because it does not
copy anything...

IMHO, the really nice thing about it would have to be the `active'' helper
class. It allows me to run and join any object from the ctor/dtor that
exposes a common interface of (T::active_run/join). Also, it allows me to
pass a variable number of arguments to the object it wraps directly through
its ctor; this is fairly convenient indeed...
Any suggestions on how I can improve this construct?

推荐答案

10月30日,10:39 * pm,Chris M. Thomasson < n ... @ spam.invalidwrote:
On Oct 30, 10:39*pm, "Chris M. Thomasson" <n...@spam.invalidwrote:

我在所有的C ++项目中使用以下技术;这里是示例

代码,为简洁起见省略了错误检查:

__________________________________________________ _______________

/ *简单线程对象

__________________________________________________ ____________ * /

#include< pthread.h>


extern" C" void * thread_entry(void *);


class thread_base {

* pthread_t m_tid;

* friend void * thread_entry(void *);

* virtual void on_active()= 0;


public:

* virtual~thread_base()= 0 ;


* void active_run(){

* * pthread_create(& m_tid,NULL,thread_entry,this);

*}


* void active_join(){

* * pthread_join(m_tid,NULL);

*}


};


thread_base :: ~thread_base(){}


void * thread_entry(void *州){

* reinterpret_cast< thread_base *>(州) - > on_active();

*返回0;


}


模板< typename T>

struct active:public T {

* active():T() {

* * this-> active_run();

*}


* ~active(){

* * this-> active_join();

*}


*模板< typename T_p1& gt;

*有效(T_p1 p1):T(p1){

* * this-> active_run();

*}


*模板< typename T_p1,typename T_p2>

*有效(T_p1 p1,T_p2 p2):T(p1,p2){

* * this-> active_run();

*}


* // [以及更多参数... ]


};


/ *简单用法示例

__________________________________________________ ____________ * /

#include< string>

#include< cstdio>


班级员工:public thread_base {

* std :: string const m_name;


* void on_active(){

* * std :: printf("(%p) - > worker( %s):: on_thread_entry()\ n",

* * *(void *)this,m_name.c_str());

*}


public:

* worker(std :: string const&名称)

* *:m_name(名称){

* * std :: printf("(%p) - > worker(%s):: my_thread ()\ n",

* * *(void *)this,m_name.c_str());

*}


*〜worker(){

* * std :: printf("(%p) - > worker(%s)::〜my_thread()\ n",

* * *(void *)this,m_name.c_str());

*}


};


class another_worker:public thread_base {

* unsigned const m_id;

* std :: string const m_name;


* void on_active(){

* * std :: printf("(%p) - > my_thread(%u /%s):: on_thread_entry()\\ \\ n",

* * *(void *)this,m_id,m_name.c_str());

*}


public:

* another_worker(unsigned const id,std :: string const& name)

* *:m_id(id),m_name(name){

*}


};


int main(void){

* {

* *有效< workerworkers [] = {

* * *" Chris",

* * *" John",

* * *" Jane",

* * *" Steve" ,

* * *" Richard",

* * *" Lisa"

* *};


* *有效< another_workerother_workers [] = {

* * *有效< another_worker>(21," Larry"),

* * *有效< another_worker>(87," Paul"),

* * *有效< another_worker>(43," Peter"),

* * *有效< another_worker>(12,Shelly),

* *};

*}


* std :: puts (\ n \ n \\ nn __________________ \ nhit< ENTER to exit ...);

* std :: fflush(stdout);

* std :: getchar();

*返回0;}


__________________________________________________ _______________


我个人喜欢这种技术比Boost更好。我发现它更直接

前进,也许更多面向对象,'活跃''

助手类的RAII性质也不会受到伤害。另外,我确实认为它更有效b&b高效。比起Boost创建线程的方式因为它没有b $ b复制任何东西......


恕我直言,关于它的真正好处就是它'主动''帮手

等级。它允许我运行并加入来自ctor / dtor的任何对象,即

公开(T :: active_run / join)的公共接口。此外,它允许我

将可变数量的参数直接传递给它直接包裹的对象

其ctor;这确实相当方便......


关于如何改进这种结构的任何建议?
I use the following technique in all of my C++ projects; here is the example
code with error checking omitted for brevity:
__________________________________________________ _______________
/* Simple Thread Object
__________________________________________________ ____________*/
#include <pthread.h>

extern "C" void* thread_entry(void*);

class thread_base {
* pthread_t m_tid;
* friend void* thread_entry(void*);
* virtual void on_active() = 0;

public:
* virtual ~thread_base() = 0;

* void active_run() {
* * pthread_create(&m_tid, NULL, thread_entry, this);
* }

* void active_join() {
* * pthread_join(m_tid, NULL);
* }

};

thread_base::~thread_base() {}

void* thread_entry(void* state) {
* reinterpret_cast<thread_base*>(state)->on_active();
* return 0;

}

template<typename T>
struct active : public T {
* active() : T() {
* * this->active_run();
* }

* ~active() {
* * this->active_join();
* }

* template<typename T_p1>
* active(T_p1 p1) : T(p1) {
* * this->active_run();
* }

* template<typename T_p1, typename T_p2>
* active(T_p1 p1, T_p2 p2) : T(p1, p2) {
* * this->active_run();
* }

* // [and on and on for more params...]

};

/* Simple Usage Example
__________________________________________________ ____________*/
#include <string>
#include <cstdio>

class worker : public thread_base {
* std::string const m_name;

* void on_active() {
* * std::printf("(%p)->worker(%s)::on_thread_entry()\n",
* * * (void*)this, m_name.c_str());
* }

public:
* worker(std::string const& name)
* * : m_name(name) {
* * std::printf("(%p)->worker(%s)::my_thread()\n",
* * * (void*)this, m_name.c_str());
* }

* ~worker() {
* * std::printf("(%p)->worker(%s)::~my_thread()\n",
* * *(void*)this, m_name.c_str());
* }

};

class another_worker : public thread_base {
* unsigned const m_id;
* std::string const m_name;

* void on_active() {
* * std::printf("(%p)->my_thread(%u/%s)::on_thread_entry()\n",
* * * (void*)this, m_id, m_name.c_str());
* }

public:
* another_worker(unsigned const id, std::string const& name)
* * : m_id(id), m_name(name) {
* }

};

int main(void) {
* {
* * active<workerworkers[] = {
* * * "Chris",
* * * "John",
* * * "Jane",
* * * "Steve",
* * * "Richard",
* * * "Lisa"
* * };

* * active<another_workerother_workers[] = {
* * * active<another_worker>(21, "Larry"),
* * * active<another_worker>(87, "Paul"),
* * * active<another_worker>(43, "Peter"),
* * * active<another_worker>(12, "Shelly"),
* * };
* }

* std::puts("\n\n\n__________________\nhit <ENTERto exit...");
* std::fflush(stdout);
* std::getchar();
* return 0;}

__________________________________________________ _______________

I personally like this technique better than Boost. I find it more straight
forward and perhaps more object oriented, the RAII nature of the `active''
helper class does not hurt either. Also, I really do think its more
"efficient" than Boost in the way it creates threads because it does not
copy anything...

IMHO, the really nice thing about it would have to be the `active'' helper
class. It allows me to run and join any object from the ctor/dtor that
exposes a common interface of (T::active_run/join). Also, it allows me to
pass a variable number of arguments to the object it wraps directly through
its ctor; this is fairly convenient indeed...

Any suggestions on how I can improve this construct?



现在你最好接受有关术语的建议

(有效):

< a rel =nofollowhref =http://groups.google.com/group/comp.lang.c++/msg/6e915b5211cce641\"target =_ blank> http://groups.google.com/group/comp .... 915b5211cce641


最诚挚的问候,

Szabolcs

Now it is better that you have taken the advice about the terminology
(active):

http://groups.google.com/group/comp....915b5211cce641

Best Regards,
Szabolcs




" Szabolcs Ferenczi" < sz *************** @ gmail.comwrote in message

news:de *************** ******************* @ u29g2000 pro.googlegroups.com ...

10月30日晚上10:39,Chris M 。托马森 < n ... @ spam.invalidwrote:

"Szabolcs Ferenczi" <sz***************@gmail.comwrote in message
news:de**********************************@u29g2000 pro.googlegroups.com...
On Oct 30, 10:39 pm, "Chris M. Thomasson" <n...@spam.invalidwrote:

我在所有C ++项目中使用以下技术;这里是

示例

代码,为简洁起见省略了错误检查:

__________________________________________________ _______________
I use the following technique in all of my C++ projects; here is the
example
code with error checking omitted for brevity:
__________________________________________________ _______________



[...]

[...]


__________________________________________________ _______________


我个人比Boost更喜欢这种技术。我发现它更多

直接

前进,也许更多面向对象,RAII性质

'活跃''

助手类也没有伤害。另外,我确实认为它更有效b&b高效。比起Boost创建线程的方式因为它没有b $ b复制任何东西......


恕我直言,关于它的真正好处就是它'主动''

帮手

等级。它允许我运行并加入来自ctor / dtor的任何对象,即

公开(T :: active_run / join)的公共接口。另外,它允许我



将可变数量的参数传递给它直接包装的对象

通过

它的ctor;这确实相当方便......


关于如何改进这种结构的任何建议?
__________________________________________________ _______________

I personally like this technique better than Boost. I find it more
straight
forward and perhaps more object oriented, the RAII nature of the
`active''
helper class does not hurt either. Also, I really do think its more
"efficient" than Boost in the way it creates threads because it does not
copy anything...

IMHO, the really nice thing about it would have to be the `active''
helper
class. It allows me to run and join any object from the ctor/dtor that
exposes a common interface of (T::active_run/join). Also, it allows me
to
pass a variable number of arguments to the object it wraps directly
through
its ctor; this is fairly convenient indeed...

Any suggestions on how I can improve this construct?


现在最好接受有关术语的建议

(有效):
Now it is better that you have taken the advice about the terminology
(active):

http://groups.google.com/group/comp....915b5211cce641



是的;我认为你的权利。

Yeah; I think your right.




" Chris M. Thomasson" < no@spam.invalidwrote in message

news:dI ************ @ newsfe01.iad ...

"Chris M. Thomasson" <no@spam.invalidwrote in message
news:dI************@newsfe01.iad...

>我在所有的C ++项目中使用以下技术;这里是
示例代码,为简洁起见省略了错误检查:

__________________________________________________ _______________
>I use the following technique in all of my C++ projects; here is the
example code with error checking omitted for brevity:
__________________________________________________ _______________



[...]

[...]


__________________________________________________ _______________
__________________________________________________ _______________



[...]

[...]


关于如何改进这个结构的任何建议?
Any suggestions on how I can improve this construct?



我忘记添加的一个补充是在'active''辅助对象中创建一个明确的守护帮助器

对象所以那个人可以创建对象并且它的ctor和它实际运行时间干预了......这里很完整

显示这一刻的示例代码:

__________________________________________________ _______________

/ *简单线程对象

__________________________________________________ ____________ * /

#include< pthread.h>

externC void * thread_entry(void *);


class thread_base {

pthread_t m_tid;

friend void * thread_entry(void *) ;

虚拟空洞on_active()= 0;


public:

virtual~thread_base()= 0;


void active_run(){

pthread_create(& m_tid,NULL,thread_entry,this);

}


void active_join(){

pthread_join(m_tid,NULL);

}

};


thread_base :: ~thread_base(){}

void * thread_entry(void * state){

reinterpret_cast< thread_base *> ;(州) - > on_active();

返回0;

}

模板< typename T>

struct active:public T {

struct guard {

T& m_object;


guard(T& object):m_object(object){

m_object.active_run();

}


~guard(){

m_object.active_join();

}

};


有效():T(){

this-> active_run();

}


~active(){

this-> active_join();

}


template< typename T_p1>

有效(T_p1 p1):T(p1){

this-> active_run();

}


模板< typename T_p1,typename T_p2>

有效(T_p1 p1,T_p2 p2):T(p1,p2){

this - > active_run();

}


// [以及更多参数的开启......]

};


/ *简单用法示例

__________________________________________________ ____________ * /

#include< string>

#include< cstdio>

类worker:public thread_base {

std :: string con st m_name;


void on_active(){

std :: printf("(%p) - > worker(%s):: on_active ()\ n",

(void *)this,m_name.c_str());

}


public :

worker(std :: string const&姓名)

:m_name(姓名){

std :: printf("(%p) - > worker(%s):: my_thread()\ n",

(void *)this,m_name.c_str());

}


~worker(){

std :: printf("(%p) - > worker(%s)::〜my_thread()\ n",

(void *)这个,m_name.c_str());

}

};

class another_worker:public thread_base {

unsigned const m_id;

std :: string const m_name;


void on_active(){

std :: printf(" ;(%p) - > another_worker(%u /%s):: on_active()\ n",

(void *)this,m_id,m_name.c_str()); < br $>
}


public:

another_worker(unsigned const id,std :: string const& name)

:m_id(id),m_name(name){

}

};

int main(void){

{

worker w1(Amy);

worker w2(Kim);

worker w3( Chris);

another_worker aw 1(123,Kelly);

another_worker aw2(12345,Tim);

another_worker aw3(87676," John");


有效< thread_base> :: guard w12_aw12 [] = {

w1,w2,w3,

aw1,aw2,aw3

};


有效< workerworkers [] = {

" Jim",

" ; Dave",

" Regis"

};


有效< another_workerother_workers [] = {

有效< another_worker>(999," Jane"),

有效< another_worker>(888," Ben"),

有效< another_worker>( 777,拉里)

};

}


std :: puts(" \ nn \ n\ __________________ \ nhit< ENTER退出...");

std :: fflush(stdout);

std :: getchar();

返回0;

}

__________________________________________________ _______________



注意位于main中的以下代码段:

worker w1(Amy);

worker w2(Kim);

worker w3(" Chris");

another_worker aw1(123," Kelly");

another_worker aw2(12345," Tim" );

another_worker aw3(87676," John");


有效< thread_base> :: guard w12_aw12 [] = {

w1,w2,w3,

aw1,aw2,aw3

};

这显示了如何使用后卫宾语。构建的对象完全构成了,它们允许你继续做任何你需要做的事情

与它们_before_你实际运行/加入它们。这可以是一个非常方便的

能力。

One addition I forgot to add would be creating an explict `guard'' helper
object within the `active'' helper object so that one can create objects and
intervene between its ctor and when it actually gets ran... Here is full
example code showing this moment:
__________________________________________________ _______________
/* Simple Thread Object
__________________________________________________ ____________*/
#include <pthread.h>
extern "C" void* thread_entry(void*);

class thread_base {
pthread_t m_tid;
friend void* thread_entry(void*);
virtual void on_active() = 0;

public:
virtual ~thread_base() = 0;

void active_run() {
pthread_create(&m_tid, NULL, thread_entry, this);
}

void active_join() {
pthread_join(m_tid, NULL);
}
};

thread_base::~thread_base() {}

void* thread_entry(void* state) {
reinterpret_cast<thread_base*>(state)->on_active();
return 0;
}
template<typename T>
struct active : public T {
struct guard {
T& m_object;

guard(T& object) : m_object(object) {
m_object.active_run();
}

~guard() {
m_object.active_join();
}
};

active() : T() {
this->active_run();
}

~active() {
this->active_join();
}

template<typename T_p1>
active(T_p1 p1) : T(p1) {
this->active_run();
}

template<typename T_p1, typename T_p2>
active(T_p1 p1, T_p2 p2) : T(p1, p2) {
this->active_run();
}

// [and on and on for more params...]
};


/* Simple Usage Example
__________________________________________________ ____________*/
#include <string>
#include <cstdio>
class worker : public thread_base {
std::string const m_name;

void on_active() {
std::printf("(%p)->worker(%s)::on_active()\n",
(void*)this, m_name.c_str());
}

public:
worker(std::string const& name)
: m_name(name) {
std::printf("(%p)->worker(%s)::my_thread()\n",
(void*)this, m_name.c_str());
}

~worker() {
std::printf("(%p)->worker(%s)::~my_thread()\n",
(void*)this, m_name.c_str());
}
};
class another_worker : public thread_base {
unsigned const m_id;
std::string const m_name;

void on_active() {
std::printf("(%p)->another_worker(%u/%s)::on_active()\n",
(void*)this, m_id, m_name.c_str());
}

public:
another_worker(unsigned const id, std::string const& name)
: m_id(id), m_name(name) {
}
};
int main(void) {
{
worker w1("Amy");
worker w2("Kim");
worker w3("Chris");
another_worker aw1(123, "Kelly");
another_worker aw2(12345, "Tim");
another_worker aw3(87676, "John");

active<thread_base>::guard w12_aw12[] = {
w1, w2, w3,
aw1, aw2, aw3
};

active<workerworkers[] = {
"Jim",
"Dave",
"Regis"
};

active<another_workerother_workers[] = {
active<another_worker>(999, "Jane"),
active<another_worker>(888, "Ben"),
active<another_worker>(777, "Larry")
};
}

std::puts("\n\n\n__________________\nhit <ENTERto exit...");
std::fflush(stdout);
std::getchar();
return 0;
}
__________________________________________________ _______________


Take notice of the following code snippet residing within main:
worker w1("Amy");
worker w2("Kim");
worker w3("Chris");
another_worker aw1(123, "Kelly");
another_worker aw2(12345, "Tim");
another_worker aw3(87676, "John");

active<thread_base>::guard w12_aw12[] = {
w1, w2, w3,
aw1, aw2, aw3
};
This shows how one can make use of the guard object. The objects are fully
constructed, and they allow you do go ahead and do whatever you need to do
with them _before_ you actually run/join them. This can be a very convenient
ability.


这篇关于围绕pthread_t的一个非常简单的C ++抽象......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆