Shared-Memory Programming with Pthreads

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.

#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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值