简体   繁体   中英

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. 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.

So, for example your output function will look like

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:

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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