Shared-Memory Programming with Pthreads
2018-01-16 16:39
357 查看
In shared-memory programming, an instance of a program running on a processor is usually called a thread(unlike MPI, where it’s called a process).
POSIX is a standard for Unix-like operating systems–for example, Linux and Mac OS X. It specified a variety of facilities that should be available in such systems. In particular, it specifies an application programming interface(API) for multithreaded programming.
Sepmaphores can be thought of as a special type of unsigned int, so they can take on the values 0,1,2….
Note that sempahores are not part of Pthreads. Hence, it’s necessary to add the following preprocessor directive to any any program that uses them.
A thread that executes sem_wait will block if the semaphore is 0. If the semaphore is not nonzero, it will decrement the semaphore and proceed. After executing the code in the critical section, a thread calls sem_post, which increments the semaphore, and a thread waiting in sem_wait can proceed.
There are three ways to implement barriers. They are busy-waiting and a mutex, semaphores and condition variables. A condition variable can be used to implement barriers. A condition variable is a data object that allows a thread to suspend execution until a certain event or condition occurs. When the event or condition occurs another thread can signal the thread to “wake up.” A condition variable is always associated with a mutex.
POSIX is a standard for Unix-like operating systems–for example, Linux and Mac OS X. It specified a variety of facilities that should be available in such systems. In particular, it specifies an application programming interface(API) for multithreaded programming.
Sepmaphores can be thought of as a special type of unsigned int, so they can take on the values 0,1,2….
Note that sempahores are not part of Pthreads. Hence, it’s necessary to add the following preprocessor directive to any any program that uses them.
#include <semaphore.h>
A thread that executes sem_wait will block if the semaphore is 0. If the semaphore is not nonzero, it will decrement the semaphore and proceed. After executing the code in the critical section, a thread calls sem_post, which increments the semaphore, and a thread waiting in sem_wait can proceed.
4.8 Barriers and Condition Variables
Synchronzing the threads by making sure that they all are the same point in a program. Such a point of synchronization is called a barrier because no thread can proceed beyond the barrier until all the threads have reached it.There are three ways to implement barriers. They are busy-waiting and a mutex, semaphores and condition variables. A condition variable can be used to implement barriers. A condition variable is a data object that allows a thread to suspend execution until a certain event or condition occurs. When the event or condition occurs another thread can signal the thread to “wake up.” A condition variable is always associated with a mutex.
相关文章推荐
- Shared-Memory Programming with OpenMP
- nginx: [emerg] the size 10485760 of shared memory zone "cache_one" conflicts with already declared size 0
- Use Named Pipes and Shared Memory for inter process communication with a child process or two
- 多线程进程间通讯共享内存(Shared Memory with IPC with threads)
- [emerg] the size 10485760 of shared memory zone “tmpcache” conflicts with already declared size 0 in
- Use Named Pipes and Shared Memory for inter process communication with a child process or two
- When to use volatile with shared CUDA Memory
- Using OpenMP: Portable Shared Memory Parallel Programming
- IPC之 - C#用 Shared Memory with IPC with threads
- 数据缓存.NET 4.0 MemoryCache with SqlChangeMonitor
- ORA-27100:shared memory realm already exists 解决方法
- No Memory Alignment with GCC
- Building MFC application with /MD[d] (CRT dll version)requires MFC shared dll version~~~~
- ORA-00371: not enough shared pool memory解决方法
- Programming Windows with MFC - Capter 3. Mouse and keybord
- CORBA Programming with TAO - 4.Basic Functions(常用基本方法解析)
- Building MFC application with /MD[d] (CRT dll version) requires MFC shared dll version
- Programming Windows with MFC - Chapter 12 Toolbars
- 使用vertx共享数据 Using Shared Data with Vert.x
- 《Object-Oriented Programming With ANSI-C》之第四章(继承——代码重用和改进)