Note that there are some explanatory texts on larger screens.

plurals
  1. POwhen should I use 'lock' in multi-thread programing?
    primarykey
    data
    text
    <p>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 ?</p> <pre><code>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, &amp;status) /* access share data thread_id, lock or not? */ exit. other threads will: access share_data, /* lock or not? */ modify share_data, /* lock */ exit. </code></pre> <p>Thanks for your attention, and If you have more time, The more detail about real code:</p> <pre><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 &lt;pthread.h&gt; #include &lt;unistd.h&gt; #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; /* #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(&amp;mutex); tid = pthread_self(); while (p-&gt;tid!=tid &amp;&amp; p-&gt;next!=NULL) { q = p; p = p-&gt;next; } tid = p-&gt;tid; torder = p-&gt;torder; /* p-&gt;status = 0; */ pthread_mutex_unlock(&amp;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", &amp;num_threads); printf ("\n"); printf ("Main thread id: %08x\n", pthread_self()); pthread_mutex_init(&amp;mutex, NULL); pthread_attr_init(&amp;attr); pthread_attr_setdetachstate(&amp;attr, PTHREAD_CREATE_JOINABLE); tlist.head=tlist.tail=NULL; /* create and initial tlist */ for (t=0; t&lt;num_threads; t++) { pthread_mutex_lock(&amp;mutex); tdata_p = (tdata_t *) malloc (sizeof (tdata_t)); pthread_create (&amp;tdata_p-&gt;tid, &amp;attr, tfunc, NULL); /* tdata_p-&gt;tid = thread[t]; */ tdata_p-&gt;torder = t; tdata_p-&gt;status = 1; /* for 0,finished the work. for 1,not*/ tdata_p-&gt;next = NULL; if(tlist.head == NULL) { tlist.head = tlist.tail = tdata_p; } else { tlist.tail-&gt;next = tdata_p; tlist.tail = tdata_p; } pthread_mutex_unlock(&amp;mutex); } /* Join child threads */ pthread_attr_destroy(&amp;attr); pthread_mutex_lock (&amp;mutex); tdata_t *p; tdata_t *q; void *status; p = tlist.head; while (p != NULL) { q = p-&gt;next; pthread_join(p-&gt;tid, &amp;status); p = q; } pthread_mutex_unlock (&amp;mutex); pthread_mutex_destroy(&amp;mutex); /* delete the list */ p = tlist.head; while (p != NULL) { q = p-&gt;next; free (p); p = q; } tlist.head = tlist.tail = NULL; printf ("Main exit.\n"); pthread_exit(NULL); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload