Linux process and thread Cheng (locking--unlock)

Source: Internet
Author: User
Tags mutex

The thread shares the memory space of the process, opens the file descriptor, and the global variable. When there are multiple threads colleagues accessing a piece of memory space or a variable, a file descriptor, if not controlled, then there may be unexpected results.
Atomic operations for our high-level language (C language, java,c#), a common code is usually composed of multiple assembly statements, the computer CPU each time the execution is a assembly instruction, a assembly instruction can not be split again, So the computer CPU can only execute one assembly instruction at a time is an atomic operation.
Mutual Exclusion (mutex) is a mutually exclusive meaning, it is a lock or a semaphore. Mutexes are used to protect data and structures shared by multiple threads from being modified by coworkers, and a mutex can have only two states --locked    locking --unlocked    Jianga locks are mutually exclusive and not accessible to other threads. There can only be one thread at a time to master a mutex lock. A lock operation is an atomic operation . If you attempt to lock on a mutex that is already locked, the thread will be suspended until the locked thread releases the mutex. emphasis: Locking unlocks a variable of type pthread_mutex_t, as long as there is a place to lock, even if there is a lock code in another thread, that thread will be suspended,
only when a variable of type pthread_mutex_t is unlocked can other threads continue to lock the variable of type pthread_mutex_t. Note: In case of mutual exclusion, a deadlock may result if a process that is using a lock resource is canceled with the Pthread_cancel function.
//Line Cheng--pthread_mutex_lock#include <stdio.h>#include<stdlib.h>#include<string.h>#include<unistd.h>#include<errno.h>#include<pthread.h>//to define a global mutex variablepthread_mutex_t Mutex =Pthread_mutex_initializer;intCount =0;void* MYFUNC (void*Arg) {    if(arg = =NULL) {printf ("param isn't allow null!\n"); returnNULL; }    //Locking-Global variable lock that all threads can accessPthread_mutex_lock (&mutex); int* Pnumx = (int*) Arg; inti =0;  while(I <Ten) {printf ("thread%d count=%d\n", *pnumx, count++); Sleep (1); I++; }    //UnlockPthread_mutex_unlock (&mutex); returnNULL;}void* MYFUNC2 (void*Arg) {    if(arg = =NULL) {printf ("param isn't allow null!\n"); returnNULL; }    //The lock is the same as the lock in the MyFunc, the MyFunc is locked, and it is locked.Pthread_mutex_lock (&mutex); int* Pnumx = (int*) Arg; inti =0;  while(I <Ten) {printf ("thread%d count=%d\n", *pnumx, count++); Sleep (1); I++; }    //UnlockPthread_mutex_unlock (&mutex); returnNULL;}intMainintArgChar*args[])    {pthread_t thr1, THR2;    pthread_attr_t attr; Pthread_attr_init (&attr); //to set a process as separablePthread_attr_setdetachstate (&attr, pthread_create_detached); intA =1, B =2; //Creating Threads    if(Pthread_create (&AMP;THR1, &attr, MyFunc, &a)! =0) {printf ("Create thread is failed! Error message:%s\n", Strerror (errno)); return-1; }    //releasing a Process Property objectPthread_attr_destroy (&attr); if(Pthread_create (&AMP;THR2, NULL, MyFunc, &b)! =0) {printf ("Create thread is failed! Error message:%s\n", Strerror (errno)); return-1;    } pthread_join (THR2, NULL); printf ("Main end\n"); return 0;}

Linux process and thread Cheng (locking--unlock)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.