简体   繁体   中英

Does it require mutex locking for arguments to be passed to pthread?

I have a function that will be called multiple times and this function will create a new pthread every it gets called. And i need to pass some arguments using struct to the pthread every it gets created.

Does it require to have a mutex locking for those struct (in the function that create the pthread) whenever passing the arguments to the pthread?

Please advise.

i need to pass some arguments using struct to the pthread every it gets created.

I suppose you must mean something along these lines:

struct args {
    int arg1;
    double arg2;
    char *arg3;
};

void *thread_func(void *p) {
    struct args *args = p;

    // ... do something with args ...

    return some_value;
}

// ...

void foo() {
    pthread_t tid;
    struct args *args;
    // ...
    args = some_expression;
    pthread_create(&tid, NULL, thread_func, args);
    // ...
}

Does it require to have a mutex locking for those struct (in the function that create the pthread) whenever passing the arguments to the pthread?

Using a mutex in function foo() alone would do no good for anyone. Mutexes provide for synchronization only between two different pieces of code that both successfully lock the same one (necessarily at different times).

More generally, launching a new thread synchronizes all actions in the new thread with all actions preceding the thread launch in the parent thread. In particular, the new thread can read the contents of the argument structure without any special measures and without thereby causing a data race, provided that no other thread modifies that structure, and the structure's lifetime does not expire. The new thread can even modify the argument structure without a data race, provided that no other thread accesses it at all, and its lifetime does not expire.

There are definitely still ways you could get into trouble, however, as the above provisos suggest. Appropriate bi- or multi-lateral use of mutexes would resolve some such issues, but not others. For example, if the args structure presented to the new thread is in fact an automatic variable of the function that launches the thread, and the thread in fact accesses the structure via the provided pointer, then there is a potential data race between the new thread's access(es) to the structure and the end of its lifetime. For example,

void foo() {
    pthread_t tid;
    struct args args;  // an automatic variable
    // ...
    pthread_create(&tid, NULL, thread_func, &args);
    // ...
    // the lifetime of args ends when control reaches here
}

In such a case, you need to ensure that the new thread performs all its accesses to the args structure before foo() (running in a different thread) returns. A mutex (alone) cannot do that for you -- instead, you want a condition variable (which must be used together with a mutex), or a semaphore, or one of a variety of other alternatives.

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