简体   繁体   English

如何消除此代码中的全局变量?

[英]How can I eliminate the global variables in this code?

The below code creates two threads. 下面的代码创建两个线程。 One to accept input and another to print out text simultaneously. 一个接受输入,另一个同时打印文本。 From what I've read on the topic, global variables are considered bad form in traditional c++ code. 根据我在该主题上所读的内容,在传统的c ++代码中,全局变量被认为是错误的形式。 Yet I cannot think of a way to make the input/output simultaneous without them. 但是,我想不出一种没有它们就可以同时进行输入/输出的方法。

How can I eliminate these two boolean global variables from my code? 如何从代码中消除这两个布尔全局变量?

bool input_done = 1;
bool output_done = 1;

void* input(void* ptr)
{
    char msg[256];
    cin.getline(msg,256);
    cout << msg << endl;
    input_done = 1;
    pthread_exit(NULL);
}
void* output(void* ptr)
{
    cout << "Hello World" << endl;
    for(long int x=0;x<1000000000;x++) {}
    output_done = 1;
    pthread_exit(NULL);
}

int main()
{
    while(1)
    {

        pthread_t t1,t2;
        if (input_done)
        {
            pthread_create(&t1,NULL,input,NULL);
            input_done = 0;
        }
        if (output_done)
        {
            pthread_create(&t2,NULL,output,NULL);
            output_done = 0;
        }
    }

}

There are pointers passed to your thread functions which you don't use. 有些指针传递给您不使用的线程函数。 Pass pointers to your variables and access them through these pointers, then you'd be able to allocate variables on stack in function main or wherever else. 将指针传递给变量,并通过这些指针访问它们,然后就可以在函数main或其他任何位置的堆栈上分配变量。

So, for example your output function will look like 因此,例如,您的output函数看起来像

void* output(void* output_done)
{
    cout << "Hello World" << endl;
    for(long int x=0;x<1000000000;x++) {}
    *((bool*)output_done) = 1;
    pthread_exit(NULL);
}

and pthread_create call: pthread_create调用:

int main()
{
    bool output_done = 1;

    // ...

    if (output_done)
    {
        pthread_create(&t2,NULL,output, &output_done);
        output_done = 0;
    }

Make input_done and output_done local variables in main , pass pointers to them to the thread functions by using the fourth parameter to pthread_create , and let the thread functions modify them through the pointers they receive. input_doneoutput_done在局部变量main ,通过第四个参数传递指向他们的线程函数pthread_create ,并让该线程功能修改他们通过他们收到的指针。

Example (adjusted for style): 示例(根据样式进行调整):

void* input(void* ptr)
{
    char msg[256];
    cin.getline(msg, 256);

    cout << msg << endl;

    *(bool*)ptr = true;
    pthread_exit(NULL);
}
void* output(void* ptr)
{
    cout << "Hello World" << endl;

    for(long int x = 0; x < 1000000000; ++x) { }

    *(bool*)ptr = true;
    pthread_exit(NULL);
}

int main()
{
    bool input_done = true;
    bool output_done = true;

    while (true)
    {
        pthread_t t1, t2;

        if (input_done)
        {
            pthread_create(&t1, NULL, input, (void*)&input_done);
            input_done = false;
        }

        if (output_done)
        {
            pthread_create(&t2, NULL, output, (void*)&output_done);
            output_done = false;
        }
    }
}

Yes, global variables are not good but not always. 是的,全局变量不是很好,但并非总是如此。 It seems to me that you want two threads one will read the input and other will write your input, for this you need some global variable that can be shared by both the thread, so that input thread can write data in global variable and output thread can read data from global variable. 在我看来,您想要两个线程一个将读取输入,另一个将写入您的输入,为此,您需要一个可以由两个线程共享的全局变量,以便输入线程可以在全局变量和输出线程中写入数据可以从全局变量读取数据。 Here you need to synchronize both threads. 在这里,您需要同步两个线程。

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

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