简体   繁体   English

具有ptrhead的奇怪重复模板模式

[英]Curiously recurring template pattern with ptrhead

So I am trying to figure out if you can use curiously recurring template pattern to get around the limitations of pthread use with class methods and even class creation by doing some thing like. 因此,我试图弄清楚您是否可以使用奇怪的重复模板模式来解决pthread在类方法甚至类创建中的局限性,例如执行一些操作。

template <class T>
class thread_helper
{
  static void* create(void *input)
  {
     T * output=new T;
     return static_cast<void*>(output);
  }

  static void* using(void *input)
  {
    T::use(input);
  }
};

class user : public thread_helper<user>
{
  void method(int x)
  {
    //does something
  }
  static void use(void* input)
  {
    this->method(static_cast<int>(input));
  }
};

Then you can use pthread to call class creation using 然后,您可以使用pthread调用类的创建

pthread_create(thread_variable, NULL, thread_helper::create<some_class>, void);

and for the other call 和另一个电话

pthread_create(thread_variable, NULL, user::using(), void);

note:There are a lot of errors in the code above. 注意:上面的代码中有很多错误。 Please don't rip me apart for them. 请不要为他们撕裂我。 I am really just trying to paint a picture of what I am trying to do. 我真的只是想画我想做的事。 I am also trying to figure out if there is a better way of doing this operation. 我还试图找出是否有更好的方法来执行此操作。

Additionally is the second pthread_create method really necessary couldn't I just use the constructor one for that as well? 另外,第二个pthread_create方法真的必要吗,我是否也可以只使用一个构造函数呢?

I am going to post a working test code. 我将发布一个有效的测试代码。 It involves a ton of boiler plate code. 它涉及大量的锅炉板代码。 It is really ugly looking but works. 它看起来确实很丑,但是可​​以工作。 Note: I will probably change thread_helper's create method at a later date so it cannot be used for threading. 注意:我可能会在以后更改thread_helper的create方法,因此它不能用于线程化。

#include <iostream>
#include "pthread.h"

using namespace std;

template <class I>
struct helper
{
    void *(I::*method)(void *); //enables you to call and get information from any non static class method
    I *ptr; //this is the class pointer used to call the member pointer
    void *info; //this is what you pass into the member pointer
};

template <class T>
class thread_helper
{
public:
    static void *create(void *in)
    {
        T * output=new T;
        return static_cast<void*>(output);
    }

    static void *method(void *in)
    {
        helper<T> *utilize = static_cast<helper<T> *>(in);
        return (utilize->ptr->*utilize->method)(utilize->info);
    }
};

class test: public thread_helper<test>
{
public:
    void *test_method(void *in)
    {
        int *val = (int *)in;//note: not static_casting since this is not a class
        *val *= 4;
        return val; //auto casts to void *
    }
};

int main()
{
    //initialize test
    test * second = static_cast<test*>(test::create(NULL));

    //initialize helper
    helper<test> first;
    first.ptr = second;
    first.method = &test::test_method;
    int val = 4;
    first.info = &val;//

    //setup threads
    pthread_t thread;
    void *ans;

    //call test_method and retrieve answer
    pthread_create(&thread, NULL, test::method, static_cast<void *>(&first));
    pthread_join(thread, &ans);

    cout << "the answer is "<< *(int *)ans <<endl;
    return 0;
}

As was pointed out this solution works if you are using pthreads. 如前所述,如果您正在使用pthreads,则此解决方案有效。 And is a generic replacement for writing either a function or a static method to call a class's methods. 并且是编写函数或静态方法以调用类的方法的通用替代方法。

If you are using boost or c++11 threads than this method is not needed and probably should not be used as it requires lots of boiler plate code(pointer casting). 如果使用boost或c ++ 11线程,则不需要此方法,并且可能不应该使用此方法,因为它需要大量样板代码(指针转换)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM