- Where Developers Learn, Share, & Build Careers
When to use 'lock' in multi thread programming? Just lock that think that each thread will modify or lock that check which can access every thread even though it will not be modified?
struct share_data {/ * share data * / thread_ID; } Thread 1 will start the main () function: initial share data / * lock required / all threads (share_data. Thread_id, and status) / * login share data thread_id, lock or not? */ Go out. Other threads would be: using share_data, / * locked or not? * / Modify share_data, / * lock * / exit Thank you for your attention, and if you have more time, more detail about the actual code:
/ * The number of threads will be user input Structure "tdata" and "tlist" information of each thread, which includes: "tid" - Thread ID, which is the sheath by the first argument of pthread_create (), "trader" which calls pthread_create () for each video The order is "status" stores the work state of each thread, I dynamically allocate memory for "list". Now, we believe that the number of threads is NUM_THREADS (5). I do not use the fourth argument to pass data to each thread, I use the global variable "tdata", "tlist" as the data shared with them. The "Mutex" variable is used to ensure that these threads share data securely, but it seems that it does not work properly. I want each thread to get "trader", and maybe modify the position 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; Int torder; Integer position; Struct tdata * Next; }; Typewrift straight tdata tdata_t; Structure tdatalist {tdata_t * head; Tdata_t * Tails; } Tlist; Pthread_mutex_t mutex; Pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; Zero * tfunc () {tdata_t * p, * q; Unsigned long intervals; Int torder; P = tlist.head; Q = null; Pthread_mutex_lock (& amp; mutex); Tid = pthread_self (); While (p-> tid! = Tid & amp; p;> Next! = NULL) {q = p; P = p-> next; } Tid = p-> Tid; Torder = p-> gt; / * P-> Condition = 0; * / Pthread_mutex_unlock (& amp; mutex); / * Print ("I thread% lu, myorder% d, thread_exit. \ N", tid, torder); * / Printf ("I thread% 0x, myorder% d, thread_exit. \ N", tid, torder); Pthread_exit (Zero *) Trader); } 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 thread:"); Scanf ("% d", and 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; Authr; PTHREAD_CREATE_JOINABLE); Tlist.head = tlist.tail = zero; / * For creating and initial listing * / (t = 0; t & lt; num_threads; t ++) {pthread_mutex_lock (and mutex); Tdata_p = (tdata_t *) maulok (tdata_t)); Pthread_create (& amp; tdata_p- & gt; tid, & amp; ethern, tfunc, NULL); / * Tdata_p- & gt; TD = Thread [T]; * / Tdata_p- & gt; Torder = t; Tdata_p-> Condition = 1; For 0 / *, for work ended 1, no * / tdata_p-> Next = null; If (tlist.head == NULL) {tlist.head = tlist.tail = tdata_p; } And {tlist.tail-> gt; Next = tdata_p; Tlist.tail = tdata_p; } Pthread_mutex_unlock (and mutex); } / * Join the hair thread * / pthread_attr_destroy (& amp; attr); Pthread_mutex_lock (and mutex); Tdata_t * p; Tdata_t * q; Zero * status; P = tlist.head; While (p! = Null) {q = p-> next; Pthread_join (p->; tid, and position); P = q; } Pthread_mutex_unlock (and mutex); Pthread_mutex_destroy (& amp; mutex); / * Delete list * / p = tlist.head; While (p! = Null) {q = p-> next; Free (P); P = q; } Tlist.head = tlist.tail = NULL; Printf ("Exit main. \ N"); Pthread_exit (zero); Return 0; }
At any time you are going to read or write data, you lock it Will happen . This prevents data from attempting to read data that has not been written yet.
Another way of saying the word is that any data is shared between threads or processes or should be locked before changing.
Comments
Post a Comment