when should I use 'lock' in multi-thread programing?
Asked Answered
F

2

6

when should I use 'lock' in multi-thread programing? Just lock the area which each thread will modify or lock the area which each thread can access even it will not be modified ?

struct share_data {
    /* share data */
    thread_id;
}

thread 1 will exceute main() function:
   Initial share data. /* need lock */

   join all thread(share_data.thread_id, &status)   /* access share data thread_id, lock or not? */
   exit.

other threads will:
   access share_data, /* lock or not? */
   modify share_data, /* lock */
   exit.

Thanks for your attention, and If you have more time, The more detail about real code:

/* 
the number of threads will be input by user. Structure "tdata" and "tlist" stores 
information of each thread, including: "tid" - thread id which is gaven by 1st argument 
of pthread_create(), "torder" which is the order of calling pthread_create() for each 
thead, "status" stores work status of each thread. I allocate memory for "tlist" 
dynamically. Now, we assume that the number of threads is NUM_THREADS(5). I do not use 
the 4th argument to pass data to each thread, I use global variable "tdata", "tlist" as 
shared data to them. "mutex" variable is used to make sure those threads share data safely,
but it seems not works correctly.

I wanna each thread can get "torder", and maybe modify the status before calling 
pthread_exit().
*/
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

/* #define NUM_THREADS 5 */
struct tdata {
        pthread_t tid;
        int torder;
        int status;
        struct tdata *next;
};
typedef struct tdata tdata_t;
struct tdatalist {
        tdata_t *head;
        tdata_t *tail;
}tlist;

pthread_mutex_t mutex;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void *tfunc() {
        tdata_t *p, *q;
        unsigned long int tid;
        int torder;

        p = tlist.head;
        q = NULL;
        pthread_mutex_lock(&mutex);

        tid = pthread_self();

        while (p->tid!=tid && p->next!=NULL) {
                q = p;
                p = p->next;
        }
        tid = p->tid;
        torder = p->torder;
        /* p->status = 0; */
        pthread_mutex_unlock(&mutex);
        /* printf ("I am thread %lu, myorder %d, thread_exit.\n", tid, torder); */
        printf ("I am thread %0x, myorder %d, thread_exit.\n", tid, torder);

        pthread_exit((void *)torder);
}

int main (int argc, char *argv[]) {
/*        pthread_t thread[NUM_THREADS]; */

        pthread_attr_t attr;
        int t;
        tdata_t *tdata_p;
        int num_threads;
        printf ("Input number of threads:");
        scanf ("%d", &num_threads);
        printf ("\n");

        printf ("Main thread id: %08x\n", pthread_self());

        pthread_mutex_init(&mutex, NULL);
        pthread_attr_init(&attr);
        pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

        tlist.head=tlist.tail=NULL;     /* create and initial tlist */
        for (t=0; t<num_threads; t++) {

                pthread_mutex_lock(&mutex);
                tdata_p = (tdata_t *) malloc (sizeof (tdata_t));
                pthread_create (&tdata_p->tid, &attr, tfunc, NULL);
                /* tdata_p->tid = thread[t]; */
                tdata_p->torder = t;
                tdata_p->status = 1;          /* for 0,finished the work.  for 1,not*/
                tdata_p->next = NULL;

                if(tlist.head == NULL) {
                        tlist.head = tlist.tail = tdata_p;
                }
                else {
                        tlist.tail->next = tdata_p;
                        tlist.tail = tdata_p;
                }
                pthread_mutex_unlock(&mutex);

        }

        /*  Join child threads */
        pthread_attr_destroy(&attr);
        pthread_mutex_lock (&mutex);
        tdata_t *p;
        tdata_t *q;
        void *status;
        p = tlist.head;
        while (p != NULL) {
                q = p->next;
                pthread_join(p->tid, &status);
                p = q;
        }

        pthread_mutex_unlock (&mutex);

        pthread_mutex_destroy(&mutex);
        /* delete the list */
        p = tlist.head;
        while (p != NULL) {
                q = p->next;
                free (p);
                p = q;
        }
        tlist.head = tlist.tail = NULL;

        printf ("Main exit.\n");
        pthread_exit(NULL);

        return 0;
}
Feodor answered 12/7, 2012 at 2:6 Comment(2)
You need to lock on all read/write access to the shared data.Heyduck
In general, you should lock whenever you need to access mutable data, and you want to insure that no other thread accesses or modifies the data while you are accessing or modifying it. In practice that means that, once you lock a critical region of code, any other threads entering the same critical region (i.e. accessing the same data) must wait until you release your lock before they can enter the critical region or access the same data. In other words, access share_data: lock; modify share_data: lock; google.com/#q=posix+lock+tutorialDesiccated
M
3

Any time you are going to read or write data, you need to lock it. This prevents data from attempting to read data that isn't done being written yet.

Another way to word this is any data that is shared between threads or processes should be locked before altering or reading.

Moan answered 12/7, 2012 at 2:15 Comment(3)
but when I join threads using pthread, the first argument of pthread_join() is stored in shared data structrue. If I lock pthread_join(), dead lock occur, since pthread_join() called in main thread will suspend for waiting thread terminate and the thread maybe wait lock which the main thread host. how should I do?Feodor
you should not need to worry about deadlocking when calling pthread_join. pthread_join should only be called by the terminating thread, which means by the time pthread_close is called, all of the data from main that the other thread is relying on should have been written and the lock removed. if you ever gave the thread you are joining to waiting for the variable the result fron pthread join is being stored in you are guarenteed to have a deadlock. if you need to check if another thread is waiting for a thread by checking that value you should use a conditional statement.Moan
I understand why you need to use a lock on shared data when writing to the data but I dont understand why you need to lock for reading. During reading, if two threads are accessing the same data at the same time, they are not changing the data. Isn't the lock in this case not necessary since no change in data is occurring?Perseid
F
0

Outside of short-term locks on higher-level inter-thread comms like producer-consumer queues, I would say as an answer "As infrequently as you can get away with". Locks generate deadlocks and the chances of deadlocks multiplies in a super-linear fashion with more locks.

Also, a good number of pthread_join() calls in an app is 0. <0 is impossible and 1 or more far too many.

Fictionalize answered 12/7, 2012 at 7:14 Comment(6)
how to wait other thread in main thread without calling pthread_join()?Feodor
Locks do not generate deadlocks, improper use of locks and race conditions generate deadlocks. Not using locks causes segmentation faults and can make debugging a nightmare. Whenever you have shared memory that more than one thread will access, it needs to be locked. If you are wanting your code to be maintainable its a good idea to lock ALL shared memory that way when you add additions to your code later on down the road, you are already protected from race conditions and don't have to worry about going through your old code to find where you need to lock and unlock things.Moan
The point is join all thread(share_data.thread_id, &status) /* access share data thread_id, lock or not? */ accessing "share_data.thread_id" , I cant lock the code like this pthread_mutex_lock(&mutex); pthread_join(share_data.thread_id,&status );pthread_mutex_unlock(&mutex)Feodor
Oh, I see what you are saying. If you are trying to pass a shared value to a blocking function and once it has been written it will not change (i.e. a thread id will remain constant for the life of the thread) and you don't want to cause a deadlock you can do the following (psuedo code): declare new local temp variable lock strcpy(temp,id) unlock //safe to use temp hereMoan
Thanks, Crackers. That should be helped.Feodor
'Locks do not generate deadlocks'. Syllogism: 'Code with no locks never deadlocks. Code with locks can deadlock. Therefore, locks generate deadlocks' :) 'improper use of locks generate deadlocks' OK, yes, but the point is that the more locks you have, the greater the number of possible paths through the locks and the more likely it is that something will go wrong from the improper use of them.Fictionalize

© 2022 - 2024 — McMap. All rights reserved.