In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Multi-thread and mutex instance analysis, many novices are not very clear about this, in order to help you solve this problem, the following small series will explain in detail for everyone, there are people who need this to learn, I hope you can gain something.
Thread: A thread is a stream of execution in a program, each thread has its own proprietary registers (stack pointers, program counters, etc.), but code areas are shared, that is, different threads can execute the same function.
Multithreading: Multithreading refers to a program that contains multiple execution streams, that is, multiple different threads can be run simultaneously in a program to perform different tasks, that is, a single program is allowed to create multiple parallel execution threads to complete their respective tasks.
thread creation
int pthread_create(pthread_t*restrict tidp,const pthread_attr_t *restrict attr,void *(*start_rtn)(void),void *restrict arg);
Return value: 0 if thread is successfully established, otherwise error number is returned.
Formal parameters: pthread_t *restrict tidp Thread id pointer to the thread to be created;const pthread_attr_t *restrict attr Thread properties at thread creation;void *(start_rtn)(void) Return value is pointer function of void type;void *restrict arg parameter to start_rtn.
Thread Suspend: The effect of this function is to suspend the current thread and wait for another thread to return before resuming execution. That is to say, when the program runs to this place, the program will stop first, and then wait for the thread with thread id to return, and then the program will execute intermittently.
int pthread_join(pthread_tthread, void **value_ptr);
Parameters are as follows: thread number of thread waiting to exit;value_ptr return value of thread exiting.
Return value: 0 if successful; error number if failed.
thread exits
function prototype: void pthread_exit(void *rval_ptr);
Get current thread id
Function prototype: pthread_t pthread_self(void);
mutex
Create pthread_mutex_init; destroy pthread_mutex_destroy; lock pthread_mutex_lock; unlock pthread_mutex_unlock
A mutex is essentially a lock that provides protected access to shared resources.
1. Initialize:
Under Linux, the thread mutex data type is pthread_mutex_t. To initialize it before use:
For statically allocated mutexes, set it to PTHREAD_MUTEX_INITIALIZER or call pthread_mutex_init.
For dynamically allocated mutexes, pthread_mutex_init is initialized after memory is requested (malloc), and pthread_mutex_destroy is called before memory is freed (free).
Prototype:
int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restric attr);
int pthread_mutex_destroy(pthread_mutex_t *mutex);
Header file:
Return value: 0 for success, error number for error.
Note: To initialize mutex with default attributes, simply set attr to NULL. Other values are explained later.
2. Mutual exclusion operations:
If the mutex is locked, the calling thread blocks until the mutex is unlocked. After accessing the shared resource, the mutex is unlocked.
Let's start with the locking function:
Header file:
Prototype:
int pthread_mutex_lock(pthread_mutex_t *mutex);
int pthread_mutex_trylock(pthread_mutex_t *mutex);
Return value: 0 for success, error number for error.
Description: Specifically, the trylock function, this function is non-blocking call mode, that is, if the mutex is not locked, the trylock function will lock the mutex and obtain access to shared resources; if the mutex is locked, the trylock function will not block waiting and return EBUSY directly, indicating that the shared resource is busy.
Again, the solution function:
Header file:
int pthread_mutex_unlock(pthread_mutex_t *mutex);
Return value: 0 for success, error number for error.
3. Deadlock:
Deadlocks occur primarily when multiple dependent locks exist and when one thread attempts to lock mutexes in the reverse order of another thread. How to avoid deadlocks is something you should pay special attention to when using mutexes.
In general, there are several unwritten basic principles:
Always obtain locks before operating on shared resources.
Be sure to release the lock when you are done.
Keep the lock occupied for as short a time as possible.
If there are multiple locks, such as the acquisition order is ABC chain, the release order should also be ABC.
A thread error should release the lock it acquired when it returns.
conditional lock
Create pthread_cond_init; destroy pthread_cond_destroy; trigger pthread_cond_signal; broadcast pthread_cond_broadcast; wait for pthread_cond_wait Supplement later
Here is a demo program to understand,
#include#include#include#include#includevoid * print_a(void *);void * print_b(void *);static pthread_mutex_t testlock;int main(void){ pthread_t t0; pthread_t t1; pthread_mutex_init(&testlock,NULL); //create thread a if(pthread_create(&t0,NULL,print_a,NULL)){ puts("fail to create pthread a"); exit(1); } //create thread b if(pthread_create(&t1,NULL,print_b,NULL)){ puts("fail to create pthread b"); exit(1); } void *result; if(pthread_join(t0,&result)==-1){ puts("fail to reconnect to"); exit(1); } if(pthread_join(t1,&result)==-1){ puts("fail to reconnect t1"); exit(1); } pthread_mutex_destroy(&testlock); return 0;}void * print_a(void *a){ int i=0; pthread_mutex_lock(&testlock); for(i=0;i
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.