简体   繁体   中英

Why is the same argv[] printed twice in the two threads?

I just started programming in multiple threading and tinkering with some templates around, I produced this possibly horrible code:

#include <stdio.h>

#include <pthread.h>

#include <assert.h>

#include <stdlib.h>

#include <string.h>

typedef struct __myarg_t //argument struct?
{

    int a;

    char *b;

} myarg_t;

typedef struct __myret_t //return thread struct?
{

    int x;

    char *y;

} myret_t;

char cat[100] = ""; //used for concatenation of argv 

void *mythread(void *arg)
{

    myarg_t *m = (myarg_t *)arg;

    int print_index = 0;
    printf("THREAD ID:  %lu\n", pthread_self());
    for (print_index = 0; print_index < m->a; print_index++)
    {
        printf("Printing %d th character %c\n", print_index, *(m->b + print_index));//spelling the words 
    }

    myret_t *rfin = malloc(sizeof(myret_t));

    rfin->x = 1;
    strcat(cat, m->b);//concatenating argv with
    strcat(cat, " "); //a space to cat
    rfin->y = cat;//reassigning the new sentence to rfin
    return (void *)rfin;
}

int main(int argc, char *argv[])
{
    if (argc == 1)
    {
        printf("Enter a word as argument before commandline.\nExiting...\n");
        exit(0);
    }

    int rc = 0;

    pthread_t p[argc - 1]; //total threads

    myret_t *m; //return thread

    myarg_t args; //argument thread?

    int i = 1;
    while (i < argc) //creating threads:
    {
        printf("THREAD:\t%d\n", i);
        args.a = strlen(argv[i]);

        args.b = (char *)malloc(strlen(argv[i]));

        strcpy(args.b, argv[i]);

        pthread_create(&p[i - 1], NULL, mythread, &args);

        i++;
    }
    i = 1;
    while (i < argc) // Wait for threads to complete
    {
        pthread_join(p[i - 1], (void **)&m);
        i++;
    }

    printf("returned %d %s\n", m->x, m->y); //end concatenation result for myret_t *m;

    return 0;
}

so when I execute it:

gcc -g task2.c -o thread -Wall -pthread
./thread hello there

It gives me the following as the result:

THREAD: 1
THREAD: 2
THREAD ID:      139848453601024
Printing 0 th character t
Printing 1 th character h
Printing 2 th character e
Printing 3 th character r
Printing 4 th character e
THREAD ID:      139848445208320
Printing 0 th character t
Printing 1 th character h
Printing 2 th character e
Printing 3 th character r
Printing 4 th character e
returned 1 there there 

Instead of hello there . Is there some problem with the way I am concatenating? Should I be using another thread to do it?

Should I lock the concatenation part of the code because different threads are using it at the same time?

Any idea as to where I am going wrong? Any help/advice/criticism is more than welcome.

You have a race condition in your code -- you pass a pointer to args to the first thread, and then the main() function immediately overwrites the member-variables in the object that the pointer points to, while the first thread is running in parallel, so the first thread (usually) doesn't get a chance to see the original args.a or args.b values before they got overwritten. To avoid this, you could allocate a separate args struct for each thread, instead:

    myarg_t * args = (myarg_t *) malloc(sizeof(myarg_t));
    args->a = strlen(argv[i]);
    args->b = (char *)malloc(strlen(argv[i])+1);  // +1 for NUL terminator byte
    strcpy(args->b, argv[i]);
    pthread_create(&p[i - 1], NULL, mythread, args);

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