Mutex lock threads

前端 未结 4 1543
逝去的感伤
逝去的感伤 2020-12-13 04:15

Am new to multi threaded/processs programming. So here\'s what I need to clarify.

Process A code

pthread_mutex_lock()
    pthread_create(fooAPI(share         


        
4条回答
  •  难免孤独
    2020-12-13 04:33

    A process consists of at least one thread (think of the main function). Multi threaded code will just spawn more threads. Mutexes are used to create locks around shared resources to avoid data corruption / unexpected / unwanted behaviour. Basically it provides for sequential execution in an asynchronous setup - the requirement for which stems from non-const non-atomic operations on shared data structures.

    A vivid description of what mutexes would be the case of people (threads) queueing up to visit the restroom (shared resource). While one person (thread) is using the bathroom easing him/herself (non-const non-atomic operation), he/she should ensure the door is locked (mutex), otherwise it could lead to being caught in full monty (unwanted behaviour)

提交回复
热议问题