Brief introduction
In a multiprocessor architecture for shared memory, you can use threads to implement parallelism. For Unix systems, the IEEE POSIX 1003.1c Standard prescribes the standard for the C language Threading programming interface. The implementation of this standard is POSIX
in the same process, no other thread can obtain the lock again.
int pthread_mutex_lock(pthread_mutex_t *mutex)int pthread_mutex_unlock(pthread_mutex_t *mutex)int pthread_mutex_trylock(pthread_mutex_t *mutex)
The semantics of pthread_mutex_trylock () is similar to that of pthread_mutex_lock (). The difference is that EBUSY is returned when the lock is occupied rather than waiting.
4. Miscellaneous
The implementation of POSIX thread loc
key value: idx1st = key/pthread_key_2ndlevel_sizeidx2nd = key% pthread_key_2ndlevel_sizeIn other words, the data is stored in a 32x32 sparse matrix. Similarly, the access time is calculated by the key value by a similar calculation of the location index of the data, and then take out the contents of the return.Back to top of pageUsage examplesThe following example has no practical meaning, just how to use it, and the ability to use this mechanism to store thread-private data.#include To create
process (): set the thread-specific binding pthread_getspecific () for the specified thread-specific data key: Get the Key Binding of the call thread, and store the binding in the position pointed by the value pthread_key_delete (): destroy the specific data key of an existing thread. For more information, see Posix thread private data.
Vi. Tool FunctionsInt pthread_equal (pthread_t threadid1, pthread_t thread2) checks whether two thread IDs are equa
Linux inter-process communication (IPC) programming practices (8) use of shared memory-POSIX shared memory (API)
1. Posix provides two methods to share memory areas between unrelated processes:
(1) memory ing file: open the function first, and then call the mmap function to map the obtained descriptor to a file in the current process address space (this is used i
Thread Creation
1.1 threads and processes
A thread is a concept closer to the execution body. It can share data with other threads in the same process, but has its own stack space and independent execution sequence. Threads and processes are introduced on the basis of a serial program to improve program concurrency and program running efficiency and response time
implements the inter-process mutex function:#include The results of the operation are as follows:Well-known semaphores enable inter-process synchronization (Print2 print first, then print1 print):The PRINT1.C code is as follows:#include The print2.c code is as follows:#include The example code for deleting a known semaphore is as follows:#include The makefile code is as follows:ALL:GCC sem_del.c-o sem_del-lpthreadgcc print1.c-o print1-lpthreadgcc print2.c-o print2-lpthreadclean:rm sem_del pri N
Concepts and functions
In a single-threaded program, we often use global variables to share data among multiple functions. In a multi-threaded environment, global variables are shared by all threads because data space is shared. However, sometimes it is necessary to provide a global variable private to the thread in the application design, which is only valid in a thread, but can be accessed across multiple functions. For example, the program may requ
I. POSIX thread attributes
The POSIX thread library defines the thread attribute object pthread_attr_t, which encapsulates the thread attributes that can be accessed and modified by the thread creator. It mainly includes the following attributes:
1. Scope)
2. stack size)
3. Stack address)
4. Priority)
5. detached state)
6. Scheduling Policy and Parameters)
The thread property object can be associated with
then read from memory:Next, do a fault-tolerant processing:In fact, the mapping failure has a special macro definition, which can be learned from the man help:So, instead of the-1 program will be more readable:Compile run:What is this for? Or from the man help to find the answer:So the cause of the problem is found, then modify the open mode to either:Compile and run again:There is no successful write, you need to write a read program to verify:Compile run:You can actually use the shell command
variable represents the thread running priority.(What is the priority granularity ?). This parameter is only used when the scheduling policy is real-time (that is, sched_rrOr sched_fifo ).And can be changed through the pthread_setschedparam () function at runtime. The default value is 0.
_ Inheritsched. Two values are available: pthread_explicit_sched and pthread_inherit_sched. The former tableIndicates that the new thread explicitly specifies the scheduling policy and scheduling parameters (t
Semaphores are primitives used to provide synchronization between different threads of different processes or a given process. There are three types: POSIX-known semaphores , using POSIX IPC name identifiers,POSIX memory-based semaphores , stored in shared memory areas, andSystem v semaphores , maintained in the kernel
);
When the slow peripheral I/O operation ends, the program can execute other computations to provide more effective and natural development methods for program concurrency;
A modular programming model that clearly expresses the relationships between independent events in a program;
Disadvantages of Multithreading
Computing load, such as the time required for synchronization between threads;
Go
Original article link
Concepts and functions
In a single-threaded program, we often use global variables to share data among multiple functions. In a multi-threaded environment, global variables are shared by all threads because data space is shared. But sometimesIt is necessary to provide a global variable private to the thread in the application design. It is valid only in a thread, but can be accessed across multiple functions.For example, the pro
#include This routine is also a sample of how the workflow is described in the POSIX multithreaded programming book, which has been repeated many times before figuring out the logic, especially the relationship between the two conditional variables ready avail and the Data_ready flag.There are a few things to note:1> because the workflow creates 10 units of work by default, each unit of work has a single th
produced a consumer consumption of a a producer produced a B consumer consumption a B producer produced a C consumer consumption a C producer produced a D consumer consumption a D producer produced an e consumer consumption an e producer produced a f consumer consumption an F producer produced a G consumer Cost A G producer produced an H consumer spending an H producer produced a I consumer consuming a I producer produced a J consumer consumption a J producer produced a K consumer consumption a
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:
[email protected]
and provide relevant evidence. A staff member will contact you within 5 working days.