简体   繁体   English

需要帮助模板化由多个线程访问的结构

[英]Need help templatizing structure accessed by multiple threads

The problem requires implementing a ring buffer into which a producer writes and from which a consumer reads. 问题需要实现一个环形缓冲区,生产者可以在其中写入数据,消费者可以从中读取数据。 I have done this for a data type. 我已经针对数据类型执行了此操作。 I want to extend this so that it will work for any primitive data type but have not been able to figure out a good way to do this. 我想对此进行扩展,以使其适用于任何原始数据类型,但还没有找到一种好的方法。 I want the program to take inputs from the command line like so "program_name data_type size_of_buffer". 我希望程序从命令行像“ program_name data_type size_of_buffer”那样接受输入。
I could templatize the buffer.start pointer and pass the data type around but I don't know of a way to assign the data type name to a variable. 我可以对buffer.start指针进行模板化,并传递数据类型,但是我不知道将数据类型名称分配给变量的方法。 Anyone have any ideas? 有人有想法么?

struct buffer{
    int * start;
    int size;
}buffer;

int * producer=NULL;
int * consumer=NULL;
bool donewriting;
bool sleeping;

void *mywrite(void *);
void *myread(void *);

void *mywrite(void * ){
    do{
        cout<<"In Thread 1"<<endl;
        static int x=0;
        *producer=x;
        cout<<"Write thread: wrote value "<<x<<" into buffer"<<endl;

        producer++;
        if(producer==buffer.start+10)
        {   producer=buffer.start;
            donewriting=true;
        }

        if(x==5)
        {
            cout<<"Thread 1 going to sleep"<<endl;
            Sleep(2000);
        }
        x++;
    } while(producer!=buffer.start);
}

void *myread(void *){
    while(!donewriting)
    {   //cout<<"In Thread 2"<<endl;
        if(consumer<producer)
        {  cout<<"Read thread: read value "<<*consumer<<" from buffer"<<endl;
            consumer++;
        }
    }
}

int main()
{
    buffer.size=10;
    buffer.start=new int(10);
    producer=buffer.start;
    consumer=buffer.start;
    donewriting=false;

    cout<<"In main"<<endl;
    pthread_t writeThread,readThread;
    pthread_create(&writeThread,NULL,mywrite,NULL);
    pthread_create(&readThread,NULL,myread,NULL);

    pthread_join(writeThread,NULL);
    pthread_join(readThread,NULL);
    return 0;
}

Unfortunately, your goal is not possible given the scenario you have described. 不幸的是,鉴于您所描述的情况,您的目标是不可能的。 Internally, C++ programs compile templated code for each datatype used with the template . 在内部, C ++程序会为template所使用的每种数据类型编译模板化的代码 In other words, if you have a templated function x() and you call 换句话说,如果您具有模板化函数x()并调用

x<int>();
x<float>();

the compiler will produce two copies of the templated function: one that uses the int datatype and another that uses the float datatype. 编译器将生成模板函数的两个副本:一个使用int数据类型,另一个使用float数据类型。 Before you call the function, no code is produced at all. 在调用该函数之前,根本不会产生任何代码。 Therefore, you cannot build the program and pass arbitrary data types to it. 因此,您不能生成程序并将任何数据类型传递给它。

Of course, if the command-line call is only intended as a test for the code, and you'll use it in a library in future, you're ok - but you'll still have to specify the type you're using in your code. 当然,如果命令行调用仅是为了测试代码,并且将来您将在库中使用它,那么您可以-但您仍然必须指定要使用的类型在您的代码中。

The current way you're doing this, using void * is probably the best way. 当前执行此操作的方法,使用void *可能是最好的方法。

It might be better to specify the size in bytes of the type you wish to use, rather than the specific type itself. 最好指定要使用的类型的大小(以字节为单位),而不是指定特定类型本身。 As long as your final producer and consumer share knowledge of the type in question, you'll be fine. 只要您的最终生产者和消费者共享有关类型的知识,就可以了。

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

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