Boost.Interprocess使用手册翻译之六:同步机制(Synchronization mechanisms)

六.          同步机制

同步机制概述

互斥量

条件变量

信号量

升级互斥量

通过移动语义转移锁

文件锁

消息队列

 

同步机制概述

具名和匿名同步机制

同步机制类型

如前所述,如果对内存的访问不能有效的同步,则通过内存映射文件或共享内存对象在进程间共享内存的能力就不是非常有用了。与需要在进程间共享堆栈和全局变量的进程间同步机制遇到的问题一样,访问这些资源一般需要使用互斥量或条件变量进行同步。Boost.Threads在同一进程的线程间实现了这些同步机制。Boost.Interprocess则在不同的进程间实现了类似的机制。

 

具名和匿名同步机制

Boost.Interprocess提供了两种同步对象:

  • 具名实用工具:若两个进程想创建此种类型的对象,它们必须使用相同的名字创建一个对象。这与创建或打开文件类似:一个进程使用采用filename的fstream创建一个文件,另一个进程使用另一个采用同样的名字为参数的fstream打开文件。虽然每个进程都使用一个不同的对象来访问资源,但这些进程均使用相同的底层资源。
  • 匿名实用工具:因为这些实用工具没有名字,因此两个进程必须通过共享内存或内存映射文件共享同一个对象。这与传统的线程间同步对象类似:所有进程共享同一对象。与全局变量和堆栈内存在同一进程的线程间共享的线程同步不同,在不同进程的两个线程间共享对象仅能通过映射区域进行,该映射区域映射了相同的可映射资源(例如,共享内存或内存映射文件)。

以上两种方式各有优缺点:

  • 具名实用工具在处理简单的同步任务时要简单些,因为进程不需要创建共享内存区域以及构建同步机制。
  • 当使用内存映射对象获得同步工具的自动持久化属性时,匿名实用工具可以被序列化至磁盘。你可以在内存映射文件上构建一个同步工具,重启系统,再次映射此文件,从而再次使用此同步化工具。这在具名实用工具的方式下是不行的。

具名和匿名实用工具在接口上的不同主要反映在构造函数上。一般而言,匿名实用工具仅有一个构造函数,而具名实用工具有多个构造函数,它们的第一个参数是一个需要创建或打开底层资源的特殊类型:

  
  
  1. using namespace boost::interprocess;  
  2.    
  3. //Create the synchronization utility. If it previously  
  4. //exists, throws an error  
  5. NamedUtility(create_only, ...)  
  6.    
  7. //Open the synchronization utility. If it does not previously  
  8. //exist, it's created.  
  9. NamedUtility(open_or_create, ...)  
  10.    
  11. //Open the synchronization utility. If it does not previously  
  12. //exist, throws an error.  
  13. NamedUtility(open_only, ...)  
  14. 另一方面,匿名同步实用工具仅能被创建,并且进程间必须使用创建此实用工具的其他同步机制进行同步:  
  15. using namespace boost::interprocess;  
  16.    
  17. //Create the synchronization utility.  
  18. AnonymousUtility(...)  

 

同步机制类型

除了其具名/匿名特性,Boost.Interprocess提供了一下同步实用工具:

  • 互斥量(具名/匿名)
  • 条件变量(具名/匿名)
  • 信号量(具名/匿名)
  • 升级互斥类型
  • 文件锁

 

互斥量

什么是一个互斥量

互斥量操作

Boost.Interprocess互斥量类型和头文件

局部锁

匿名互斥量示例

具名互斥量示例

 

什么是一个互斥量

互斥量代表相互排斥并且它是进程间不同的最基本形式。互斥量保证仅有一个线程能够锁定一个给定的互斥量。如果一个代码片段被一个互斥量锁定或解锁,它保证一次仅有一个线程执行此段代码。当这个线程解锁了互斥量,另一个线程方可进入那段代码区域:

  
  
  1. //The mutex has been previously constructed  
  2.    
  3. lock_the_mutex();  
  4.    
  5. //This code will be executed only by one thread  
  6. //at a time.  
  7.    
  8. unlock_the_mutex();  

一个互斥量可以是递归非递归的:

  • 递归互斥量可以被同一个线程锁多次。为了完全解锁互斥量,此线程必须解锁同样的次数。
  • 非递归互斥量不能被同一个线程锁多次。如果一个互斥量被一个线程锁了两次,结果是未定义的,可能会抛出一个错误或是线程被死锁。

 

互斥量操作

所有Boost.Interprocess的互斥量类型均有如下操作:

void lock()

效果:调用线程尝试获取互斥量的所有权,并且如果其他线程已经拥有了此互斥量的所有权,它等待直到能获得所有权为止。如果一个线程拥有了此互斥量的所有权,此互斥量必须被同一个线程解锁。如果互斥量支持递归锁定,则互斥量必须解锁同样的次数。

抛出:interprocess_exception错误。

bool try_lock()

效果:调用线程尝试获取互斥量的所有权,并且如果其他线程已经拥有了此互斥量的所有权,函数立刻返回。如果互斥量支持递归锁定,则互斥量必须解锁同样的次数。

返回:如果线程获取了互斥量的所有权,返回true;如果其他线程已经拥有了此互斥量的所有权,返回false。

抛出:interprocess_exception错误。

booltimed_lock(const boost::posix_time::ptime &abs_time)

效果:调用线程尝试获取互斥量的独占权限直到达到指定的时间。如果互斥量支持递归锁定,则互斥量必须解锁同样的次数。

返回:如果线程获取了互斥量的所有权,返回true,如果超时,返回false。

抛出:interprocess_exception错误。

void unlock()

前提条件:线程必须已经拥有了互斥量的独占权。

效果:调用线程释放互斥量的独占权。如果互斥量支持递归锁定,则互斥量必须解锁同样的次数。

抛出:一个派生自interprocess_exception的异常。

 

Boost.Interprocess互斥量类型和头文件

Boost.Interprocess提供了如下互斥量类型:

  
  
  1. #include <boost/interprocess/sync/interprocess_mutex.hpp>  
  • interprocess_mutex: 一个非递归的、匿名的互斥量,它能够被置于共享内存和内存映射文件中。
  
  
  1. #include <boost/interprocess/sync/interprocess_recursive_mutex.hpp>  
#include <boost/interprocess/sync/named_mutex.hpp>
#include <boost/interprocess/sync/named_recursive_mutex.hpp>

局部锁

当进程已经完成了读写数据后及时解锁互斥量,这是非常重要的。这一点在遇到异常时,是比较困难的。因此,互斥量一般是带局部锁使用,局部锁能保证互斥量一直能被解锁,哪怕有异常发生。为使用一个局部锁,仅需包含:

#include <boost/interprocess/sync/scoped_lock.hpp>

基本上,一个局部锁在其析构函数中调用 unlock(),因此一个互斥量在异常发生时,总能被解锁。局部锁有许多构造函数用于lock, try_lock, timed_lock一个互斥量或者不锁定。

  
  
  1. using namespace boost::interprocess;  
  2.    
  3. //Let's create any mutex type:  
  4. MutexType mutex;  
  5.    
  6. {  
  7.    //This will lock the mutex  
  8.    scoped_lock<MutexType> lock(mutex);  
  9.    
  10.    //Some code  
  11.    
  12.    //The mutex will be unlocked here  
  13. }  
  14.    
  15. {  
  16.    //This will try_lock the mutex  
  17.    scoped_lock<MutexType> lock(mutex, try_to_lock);  
  18.    
  19.    //Check if the mutex has been successfully locked  
  20.    if(lock){  
  21.       //Some code  
  22.    }  
  23.    
  24.    //If the mutex was locked it will be unlocked  
  25. }  
  26.    
  27. {  
  28.    boost::posix_time::ptime abs_time = ...  
  29.    
  30.    //This will timed_lock the mutex  
  31.    scoped_lock<MutexType> lock(mutex, abs_time);  
  32.    
  33.    //Check if the mutex has been successfully locked  
  34.    if(lock){  
  35.       //Some code  
  36.    }  
  37.    
  38.    //If the mutex was locked it will be unlocked  
  39. }  

更多详情,参考scoped_lock'sreference.

 

匿名互斥量示例

想象一下两个进程需要写入跟踪数据至一个建立在共享内存上的循环缓冲区。每个进程需要获得循环缓冲区的独占访问,写跟踪数据然后继续。

为了保护循环缓冲区,我们可以存储一个进程共享互斥量在循环缓冲区上。每个进程将在写数据前锁此互斥量,并且在结束写跟踪数据后将写一个标记(头文件doc_anonymous_mutex_shared_data.hpp):

  
  
  1. #include <boost/interprocess/sync/interprocess_mutex.hpp>  
  2.    
  3. struct shared_memory_log  
  4. {  
  5.    enum { NumItems = 100 };  
  6.    enum { LineSize = 100 };  
  7.    
  8.    shared_memory_log()  
  9.       :  current_line(0)  
  10.       ,  end_a(false)  
  11.       ,  end_b(false)  
  12.    {}  
  13.    
  14.    //Mutex to protect access to the queue  
  15.    boost::interprocess::interprocess_mutex mutex;  
  16.    
  17.    //Items to fill  
  18.    char   items[NumItems][LineSize];  
  19.    int    current_line;  
  20.    bool   end_a;  
  21.    bool   end_b;  
  22. };  

这是进程的主要过程。创建共享内存,构建循环缓冲区,然后开始写跟踪数据:

  
  
  1. #include <boost/interprocess/shared_memory_object.hpp>  
  2. #include <boost/interprocess/mapped_region.hpp>  
  3. #include <boost/interprocess/sync/scoped_lock.hpp>  
  4. #include "doc_anonymous_mutex_shared_data.hpp"  
  5. #include <iostream>  
  6. #include <cstdio>  
  7.    
  8. using namespace boost::interprocess;  
  9.    
  10. int main ()  
  11. {  
  12.    try{  
  13.       //Remove shared memory on construction and destruction  
  14.       struct shm_remove  
  15.       {  
  16.          shm_remove() { shared_memory_object::remove("MySharedMemory"); }  
  17.          ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }  
  18.       } remover;  
  19.    
  20.       //Create a shared memory object.  
  21.       shared_memory_object shm  
  22.          (create_only               //only create  
  23.          ,"MySharedMemory"          //name  
  24.          ,read_write   //read-write mode  
  25.          );  
  26.    
  27.       //Set size  
  28.       shm.truncate(sizeof(shared_memory_log));  
  29.    
  30.       //Map the whole shared memory in this process  
  31.       mapped_region region  
  32.          (shm                       //What to map  
  33.          ,read_write   //Map it as read-write  
  34.          );  
  35.    
  36.       //Get the address of the mapped region  
  37.       void * addr       = region.get_address();  
  38.    
  39.       //Construct the shared structure in memory  
  40.       shared_memory_log * data = new (addr) shared_memory_log;  
  41.    
  42.       //Write some logs  
  43.       for(int i = 0; i < shared_memory_log::NumItems; ++i){  
  44.          //Lock the mutex  
  45.          scoped_lock<interprocess_mutex> lock(data->mutex);  
  46.          std::sprintf(data->items[(data->current_line++) % shared_memory_log::NumItems]  
  47.                   ,"%s_%d""process_a", i);  
  48.          if(i == (shared_memory_log::NumItems-1))  
  49.             data->end_a = true;  
  50.          //Mutex is released here  
  51.       }  
  52.    
  53.       //Wait until the other process ends  
  54.       while(1){  
  55.          scoped_lock<interprocess_mutex> lock(data->mutex);  
  56.          if(data->end_b)  
  57.             break;  
  58.       }  
  59.    }  
  60.    catch(interprocess_exception &ex){  
  61.       std::cout << ex.what() << std::endl;  
  62.       return 1;  
  63.    }  
  64.    return 0;  
  65. }  

第二个进程打开共享内存,获取对循环缓冲区的访问,然后开始写跟踪数据:

  
  
  1. #include <boost/interprocess/shared_memory_object.hpp>  
  2. #include <boost/interprocess/mapped_region.hpp>  
  3. #include <boost/interprocess/sync/scoped_lock.hpp>  
  4. #include "doc_anonymous_mutex_shared_data.hpp"  
  5. #include <iostream>  
  6. #include <cstdio>  
  7.    
  8. using namespace boost::interprocess;  
  9.    
  10. int main ()  
  11. {  
  12.    //Remove shared memory on destruction  
  13.    struct shm_remove  
  14.    {  
  15.       ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }  
  16.    } remover;  
  17.    
  18.    //Open the shared memory object.  
  19.    shared_memory_object shm  
  20.       (open_only                    //only create  
  21.       ,"MySharedMemory"              //name  
  22.       ,read_write  //read-write mode  
  23.       );  
  24.    
  25.    //Map the whole shared memory in this process  
  26.    mapped_region region  
  27.       (shm                       //What to map  
  28.       ,read_write //Map it as read-write  
  29.       );  
  30.    
  31.    //Get the address of the mapped region  
  32.    void * addr       = region.get_address();  
  33.    
  34.    //Construct the shared structure in memory  
  35.    shared_memory_log * data = static_cast<shared_memory_log*>(addr);  
  36.    
  37.    //Write some logs  
  38.    for(int i = 0; i < 100; ++i){  
  39.       //Lock the mutex  
  40.       scoped_lock<interprocess_mutex> lock(data->mutex);  
  41.       std::sprintf(data->items[(data->current_line++) % shared_memory_log::NumItems]  
  42.                ,"%s_%d""process_a", i);  
  43.       if(i == (shared_memory_log::NumItems-1))  
  44.          data->end_b = true;  
  45.       //Mutex is released here  
  46.    }  
  47.    
  48.    //Wait until the other process ends  
  49.    while(1){  
  50.       scoped_lock<interprocess_mutex> lock(data->mutex);  
  51.       if(data->end_a)  
  52.          break;  
  53.    }  
  54.    return 0;  
  55. }  

如我们所看到的,一个互斥量可用于保护数据,但是没有向另一个进程发事件通知。基于此,我们需要一个条件变量,我们将在下一节阐述它。

 

具名互斥量示例

现在想象一个两个进程想写一份跟踪数据至一个文件中。首先,它们写它们的名字,然后它们写此消息。因为操作系统能够在任意时刻中断一个进程,我们可能会在两个进程中混淆此消息的部分,因此我们需要一种原子写整个消息至文件中的方法。为达此目的,我们可以使用一个具名互斥量以便每个进程在写数据前锁定此互斥量:

  
  
  1. #include <boost/interprocess/sync/scoped_lock.hpp>  
  2. #include <boost/interprocess/sync/named_mutex.hpp>  
  3. #include <fstream>  
  4. #include <iostream>  
  5. #include <cstdio>  
  6.    
  7.    
  8. int main ()  
  9. {  
  10.    using namespace boost::interprocess;  
  11.    try{  
  12.       struct file_remove  
  13.       {  
  14.          file_remove() { std::remove("file_name"); }  
  15.          ~file_remove(){ std::remove("file_name"); }  
  16.       } file_remover;  
  17.       struct mutex_remove  
  18.       {  
  19.          mutex_remove() { named_mutex::remove("fstream_named_mutex"); }  
  20.          ~mutex_remove(){ named_mutex::remove("fstream_named_mutex"); }  
  21.       } remover;  
  22.    
  23.       //Open or create the named mutex  
  24.       named_mutex mutex(open_or_create, "fstream_named_mutex");  
  25.    
  26.       std::ofstream file("file_name");  
  27.    
  28.       for(int i = 0; i < 10; ++i){  
  29.    
  30.          //Do some operations...  
  31.    
  32.          //Write to file atomically  
  33.          scoped_lock<named_mutex> lock(mutex);  
  34.          file << "Process name, ";  
  35.          file << "This is iteration #" << i;  
  36.          file << std::endl;  
  37.       }  
  38.    }  
  39.    catch(interprocess_exception &ex){  
  40.       std::cout << ex.what() << std::endl;  
  41.       return 1;  
  42.    }  
  43.    return 0;  
  44. }  

 

条件变量

什么是一个条件变量

Boost.Interprocess条件变量类型和头文件

匿名条件变量示例

 

什么是一个条件变量

在之前的例子中,互斥量被用来锁定,但直到条件变量出现前,我们并不能有效使用它。一个条件变量可以做两件事情:

  • 等:线程被阻塞直到另一个线程通知它继续,因为导致等待的条件已经消失了。
  • 通知:线程发送一个信号至一个阻塞的线程或者至所有阻塞的线程,告诉它们导致它们等待的条件已经消失了。

在条件变量中等待总是会关联一个互斥量。在等待条件之前,互斥量需要预先被锁定。当在条件变量中等待时,线程解锁互斥量然后原子等待。

当线程从等待函数(例如可能由于一个信号或超时)返回后,互斥量对象再次被锁定。

 

Boost.Interprocess条件变量类型和头文件

Boost.Interprocess提供如下的条件类型:

#include <boost/interprocess/sync/interprocess_condition.hpp>
#include <boost/interprocess/sync/named_condition.hpp>

具名条件与匿名条件类似,但它们结合具名互斥量使用。有时,我们不希望存储带同步数据的同步对象:

  • 我们想采用同样的数据改变同步方式(从进程间变为进程内,或不适用任何同步方式)。存储带共享数据的进程间共享匿名同步对象将禁止这样做。
  • 我们想通过网络或其他通信方式发送同步数据。发送进程共享的同步对象没有任何意义。

 

匿名条件变量示例

想象一下,一个进程写一份跟踪数据至简单共享内存缓冲区,另一个进程一个接一个打印出来。第一个进程写跟踪数据,然后等待直到另一个进程打印出这份数据。为达到此目的,我们可以使用两个条件变量:第一个用于阻塞发送者直到第二个进程打印出此消息,第二个用于阻塞接收者直到缓冲区中有数据供打印。

共享内存跟踪数据缓冲区(doc_anonymous_condition_shared_data.hpp):

  
  
  1. #include <boost/interprocess/sync/interprocess_mutex.hpp>  
  2. #include <boost/interprocess/sync/interprocess_condition.hpp>  
  3.    
  4. struct trace_queue  
  5. {  
  6.    enum { LineSize = 100 };  
  7.    
  8.    trace_queue()  
  9.       :  message_in(false)  
  10.    {}  
  11.    
  12.    //Mutex to protect access to the queue  
  13.    boost::interprocess::interprocess_mutex      mutex;  
  14.    
  15.    //Condition to wait when the queue is empty  
  16.    boost::interprocess::interprocess_condition  cond_empty;  
  17.    
  18.    //Condition to wait when the queue is full  
  19.    boost::interprocess::interprocess_condition  cond_full;  
  20.    
  21.    //Items to fill  
  22.    char   items[LineSize];  
  23.    
  24.    //Is there any message  
  25.    bool message_in;  
  26. };  

这是进程的主要过程。建立共享内存、放置缓冲区在其上然后开始一个接一个写消息直到它写了一个"last message"表明没有更多消息供打印:

  
  
  1. #include <boost/interprocess/shared_memory_object.hpp>  
  2. #include <boost/interprocess/mapped_region.hpp>  
  3. #include <boost/interprocess/sync/scoped_lock.hpp>  
  4. #include <iostream>  
  5. #include <cstdio>  
  6. #include "doc_anonymous_condition_shared_data.hpp"  
  7.    
  8. using namespace boost::interprocess;  
  9.    
  10. int main ()  
  11. {  
  12.    
  13.    //Erase previous shared memory and schedule erasure on exit  
  14.    struct shm_remove  
  15.    {  
  16.       shm_remove() { shared_memory_object::remove("MySharedMemory"); }  
  17.       ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }  
  18.    } remover;  
  19.    
  20.    //Create a shared memory object.  
  21.    shared_memory_object shm  
  22.       (create_only               //only create  
  23.       ,"MySharedMemory"           //name  
  24.       ,read_write                //read-write mode  
  25.       );  
  26.    try{  
  27.       //Set size  
  28.       shm.truncate(sizeof(trace_queue));  
  29.    
  30.       //Map the whole shared memory in this process  
  31.       mapped_region region  
  32.          (shm                       //What to map  
  33.          ,read_write //Map it as read-write  
  34.          );  
  35.    
  36.       //Get the address of the mapped region  
  37.       void * addr       = region.get_address();  
  38.    
  39.       //Construct the shared structure in memory  
  40.       trace_queue * data = new (addr) trace_queue;  
  41.    
  42.       const int NumMsg = 100;  
  43.    
  44.       for(int i = 0; i < NumMsg; ++i){  
  45.          scoped_lock<interprocess_mutex> lock(data->mutex);  
  46.          if(data->message_in){  
  47.             data->cond_full.wait(lock);  
  48.          }  
  49.          if(i == (NumMsg-1))  
  50.             std::sprintf(data->items, "%s""last message");  
  51.          else  
  52.             std::sprintf(data->items, "%s_%d""my_trace", i);  
  53.    
  54.          //Notify to the other process that there is a message  
  55.          data->cond_empty.notify_one();  
  56.    
  57.          //Mark message buffer as full  
  58.          data->message_in = true;  
  59.       }  
  60.    }  
  61.    catch(interprocess_exception &ex){  
  62.       std::cout << ex.what() << std::endl;  
  63.       return 1;  
  64.    }  
  65.    
  66.    return 0;  
  67. }  

第二个进程打开共享内存,然后打印每个消息直到 "last message" 消息:

  
  
  1. #include <boost/interprocess/shared_memory_object.hpp>  
  2. #include <boost/interprocess/mapped_region.hpp>  
  3. #include <boost/interprocess/sync/scoped_lock.hpp>  
  4. #include <iostream>  
  5. #include <cstring>  
  6. #include "doc_anonymous_condition_shared_data.hpp"  
  7.    
  8. using namespace boost::interprocess;  
  9.    
  10. int main ()  
  11. {  
  12.    //Create a shared memory object.  
  13.    shared_memory_object shm  
  14.       (open_only                    //only create  
  15.       ,"MySharedMemory"              //name  
  16.       ,read_write                   //read-write mode  
  17.       );  
  18.    
  19.    try{  
  20.       //Map the whole shared memory in this process  
  21.       mapped_region region  
  22.          (shm                       //What to map  
  23.          ,read_write //Map it as read-write  
  24.          );  
  25.    
  26.       //Get the address of the mapped region  
  27.       void * addr       = region.get_address();  
  28.    
  29.       //Obtain a pointer to the shared structure  
  30.       trace_queue * data = static_cast<trace_queue*>(addr);  
  31.    
  32.       //Print messages until the other process marks the end  
  33.       bool end_loop = false;  
  34.       do{  
  35.          scoped_lock<interprocess_mutex> lock(data->mutex);  
  36.          if(!data->message_in){  
  37.             data->cond_empty.wait(lock);  
  38.          }  
  39.          if(std::strcmp(data->items, "last message") == 0){  
  40.             end_loop = true;  
  41.          }  
  42.          else{  
  43.             //Print the message  
  44.             std::cout << data->items << std::endl;  
  45.             //Notify the other process that the buffer is empty  
  46.             data->message_in = false;  
  47.             data->cond_full.notify_one();  
  48.          }  
  49.       }  
  50.       while(!end_loop);  
  51.    }  
  52.    catch(interprocess_exception &ex){  
  53.       std::cout << ex.what() << std::endl;  
  54.       return 1;  
  55.    }  
  56.    
  57.    return 0;  
  58. }  

采用条件变量,如果一个线程不能继续工作,它能够阻塞,并且当遇到可以继续的条件时,另一个线程能够唤醒它。

 

信号量

什么是一个信号量

Boost.Interprocess信号量类型和头文件

匿名信号量示例

 

什么是一个信号量

信号量是一种基于内部计数的进程间同步机制,它提供了两种基本操作:

  • 等待:测试信号量计数值,如果值小于等于0,则等待。否则,减1信号量计数值。
  • 委派(Post):增1信号量计数值。如果有进程被阻塞了,则其中之一被唤醒。

如果信号量计数值被初始化为1,则等待操作与互斥锁等价,委派操作与互斥解锁等价。这种类型的信号量被称为二进制信号量

尽管信号量可以像互斥量一样使用,它有一个独特的特性:与互斥量不同,委派操作不需要由执行等待操作的那个相同的线程/进程来执行。

 

Boost.Interprocess信号量类型和头文件

Boost.Interprocess提供如下类型的信号量:

#include <boost/interprocess/sync/interprocess_semaphore.hpp>
#include <boost/interprocess/sync/named_semaphore.hpp>

 

匿名信号量示例

我们将在共享内存中实现一个整型数组,此数组将被用来从一个进程向另一个进程传递数据。第一个进程将向数组内写一些整数,并且如果数组满了,进程将阻塞。

第二个进程将拷贝传输过来的数据至其自身的buffer中,如果buffer中没有新数据了,则此进程阻塞。

以下是共享整型数组(doc_anonymous_semaphore_shared_data.hpp):

  
  
  1. #include <boost/interprocess/sync/interprocess_semaphore.hpp>  
  2.    
  3. struct shared_memory_buffer  
  4. {  
  5.    enum { NumItems = 10 };  
  6.    
  7.    shared_memory_buffer()  
  8.       : mutex(1), nempty(NumItems), nstored(0)  
  9.    {}  
  10.    
  11.    //Semaphores to protect and synchronize access  
  12.    boost::interprocess::interprocess_semaphore  
  13.       mutex, nempty, nstored;  
  14.    
  15.    //Items to fill  
  16.    int items[NumItems];  
  17. };  

以下是主进程。创建共享内存、放置整型数组至共享内存上,然后开始一个个填充整数,如果数组满了,则阻塞。

  
  
  1. #include <boost/interprocess/shared_memory_object.hpp>  
  2. #include <boost/interprocess/mapped_region.hpp>  
  3. #include <iostream>  
  4. #include "doc_anonymous_semaphore_shared_data.hpp"  
  5.    
  6. using namespace boost::interprocess;  
  7.    
  8. int main ()  
  9. {  
  10.    //Remove shared memory on construction and destruction  
  11.    struct shm_remove  
  12.    {  
  13.       shm_remove() { shared_memory_object::remove("MySharedMemory"); }  
  14.       ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }  
  15.    } remover;  
  16.    
  17.    //Create a shared memory object.  
  18.    shared_memory_object shm  
  19.       (create_only                  //only create  
  20.       ,"MySharedMemory"              //name  
  21.       ,read_write  //read-write mode  
  22.       );  
  23.    
  24.    //Set size  
  25.    shm.truncate(sizeof(shared_memory_buffer));  
  26.    
  27.    //Map the whole shared memory in this process  
  28.    mapped_region region  
  29.       (shm                       //What to map  
  30.       ,read_write //Map it as read-write  
  31.       );  
  32.    
  33.    //Get the address of the mapped region  
  34.    void * addr       = region.get_address();  
  35.    
  36.    //Construct the shared structure in memory  
  37.    shared_memory_buffer * data = new (addr) shared_memory_buffer;  
  38.    
  39.    const int NumMsg = 100;  
  40.    
  41.    //Insert data in the array  
  42.    for(int i = 0; i < NumMsg; ++i){  
  43.       data->nempty.wait();  
  44.       data->mutex.wait();  
  45.       data->items[i % shared_memory_buffer::NumItems] = i;  
  46.       data->mutex.post();  
  47.       data->nstored.post();  
  48.    }  
  49.    
  50.    return 0;  
  51. }  

第二个进程打开共享内存,然后拷贝接收到的整数至其自身的共享内存:

  
  
  1. #include <boost/interprocess/shared_memory_object.hpp>  
  2. #include <boost/interprocess/mapped_region.hpp>  
  3. #include <iostream>  
  4. #include "doc_anonymous_semaphore_shared_data.hpp"  
  5.    
  6. using namespace boost::interprocess;  
  7.    
  8. int main ()  
  9. {  
  10.    //Remove shared memory on destruction  
  11.    struct shm_remove  
  12.    {  
  13.       ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }  
  14.    } remover;  
  15.    
  16.    //Create a shared memory object.  
  17.    shared_memory_object shm  
  18.       (open_only                    //only create  
  19.       ,"MySharedMemory"              //name  
  20.       ,read_write  //read-write mode  
  21.       );  
  22.    
  23.    //Map the whole shared memory in this process  
  24.    mapped_region region  
  25.       (shm                       //What to map  
  26.       ,read_write //Map it as read-write  
  27.       );  
  28.    
  29.    //Get the address of the mapped region  
  30.    void * addr       = region.get_address();  
  31.    
  32.    //Obtain the shared structure  
  33.    shared_memory_buffer * data = static_cast<shared_memory_buffer*>(addr);  
  34.    
  35.    const int NumMsg = 100;  
  36.    
  37.    int extracted_data [NumMsg];  
  38.    
  39.    //Extract the data  
  40.    for(int i = 0; i < NumMsg; ++i){  
  41.       data->nstored.wait();  
  42.       data->mutex.wait();  
  43.       extracted_data[i] = data->items[i % shared_memory_buffer::NumItems];  
  44.       data->mutex.post();  
  45.       data->nempty.post();  
  46.    }  
  47.    return 0;  
  48. }  

同样的进程间通信也可以采用条件变量和互斥量来完成,但这几个同步模式中,信号量比互斥量/条件变量组合更高效。

 

升级互斥量

什么是一个升级互斥量

升级互斥量的操作

Boost.Interprocess升级互斥量类型和头文件

共享锁和升级锁

 

什么是一个升级互斥量

升级互斥量是一种特殊的互斥量,它比普通的互斥量提供了更多的锁定可能性。有时,我们能够区分读数据和修改数据。如果仅有某些线程需要修改数据,并且一个普通的互斥量被用来保护数据被并行访问,并发性是相当有限的:两个仅读取数据的线程会被序列化,而不是被并发执行。

如果我们允许对仅读取数据的线程的并发访问,但是我们避免对读/写线程的并发访问,我们就能够提升性能。这在如下应用中尤其明显:当读数据比修改数据更频繁并且同步数据读取的代码需要一定的时间来执行。采用升级互斥量,我们能够获得3种锁类型:

  • 独占锁:与普通互斥量类似。如果一个线程获取了一个独占锁,其他任何线程都不能获取此锁(互斥或其他方式)直到解锁。如果某一线程有一个共享或升级锁,则另一个企图获取独占锁的线程会阻塞。这种锁将被会修改数据的线程所拥有。
  • 共享锁:如果一个线程获取了一个共享锁,其他线程也可以获取一个共享锁或升级锁。如果任一线程获取了独占锁,则另一企图获取共享锁的线程会被阻塞。这种锁被仅需要读取数据的线程执行。
  • 升级锁:获取一个升级锁与获取一个特权共享锁类似。如果一个进程获取了一个升级锁,另外的线程可以获取一个共享锁。如果任一线程已经获取了独占锁或升级锁,则其他企图获取升级锁的线程会被阻塞。当其他已经获取了共享锁的线程释放了此锁,一个已经获取一个升级锁的线程被保证能够原子获取一个独占锁。它被用于如下线程中:此线程可能需要修改数据,但通常情况下它仅需要读取数据。这种线程获取升级锁,其他线程能获取共享锁。如果升级的线程读数据并且它需要修改数据时,此线程能够被提升至获取独占锁:当所有共享线程已经释放了共享锁时,升级锁被原子提升至独占锁。这个新被提升的线程能够修改数据,并且能够保证在过渡时,没有其他线程对数据修改。仅仅一个线程能获取升级(特权读)锁。

总结一下:

表 12.5. 锁定的可能性

如果一个线程已经获取…

其他线程能够获取…

共享锁

许多共享锁和一个升级锁

升级锁

许多共享锁

独占锁

一个已经获取了一个锁的线程能够尝试原子获取另一类型的锁。所有锁的转换不保证能成功。尽管一个转换能保证成功,一些转换将阻塞线程直到其他线程释放了共享锁。原子操作意味着没有其他线程在转换中能够获得一个升级或独占锁,因此数据能够保证未被修改:

表 12.6. 转换的可能性

如果一个线程已经获取…

它能原子释放之前的锁,并且…

共享锁

尝试立刻获取(不保证成功)独占锁,如果没有其他线程占有互斥或升级锁

共享锁

尝试立刻获取(不保证成功)升级锁,如果没有其他线程占有互斥或升级锁

升级锁

当所有共享锁被释放后,获取独占锁

升级锁

立刻获取共享锁

独占锁

立刻获取升级锁

独占锁

立刻获取共享锁

正如我们所看到的,升级互斥量是一个强大的同步工具,它能够提升并发性能。然后,如果大多数情况下我们都需要修改数据,或同步代码片段非常短,则使用普通互斥量效率更高,因为它具有较小的开销。当同步代码片段比较大并且读比写多时,升级锁才能发挥出它的光芒。

 

升级互斥量的操作

独占锁定

共享锁定

升级锁定

降级

升级

所有Boost.Interprocess中的升级互斥类型均可以执行如下操作:

独占锁定

void lock()

效果:调用者线程尝试获取互斥量的独占所有权,如果另一个线程已经有这个互斥量的独占、共享或升级所有权,则线程等待直到它获取所有权为止。

抛出:interprocess_exception异常。

bool try_lock()

效果:调用者线程尝试获取互斥量的独占所有权而不等待。如果没有其他线程具有此互斥量的独占、共享或升级所有权,则获取成功。

返回:如果能立即获得独占所有权,则返回true。如果需要等待,返回false。

抛出:interprocess_exception异常。

bool timed_lock(constboost::posix_time::ptime &abs_time)

效果:调用者线程尝试获取互斥量的独占所有权直到没有其他线程具有此互斥量的独占、共享或升级所有权或者是达到abs_time。

返回:如果获得独占所有权,则返回true。如果需要等待,返回false。

抛出:interprocess_exception异常。

void unlock()

前提条件:线程必须有互斥量的独占所有权。

效果:调用者线程释放互斥量的独占所有权。

抛出:一个派生自interprocess_exception的异常。

 

共享锁定

void lock_sharable()

效果:调用者线程尝试获取互斥量的共享所有权,并且如果另一个线程已经有这个互斥量的独占或升级所有权,则线程等待直到它获取所有权为止。

抛出:interprocess_exception异常。

bool try_lock_sharable()

效果:调用者线程尝试获取互斥量的共享所有权而不等待。如果没有其他线程具有此互斥量的独占或升级所有权,则获取成功。

返回:如果能立即获得独占所有权,则返回true。如果需要等待,返回false。

抛出:interprocess_exception异常。

bool timed_lock_sharable(constboost::posix_time::ptime &abs_time)

效果:调用者线程尝试获取互斥量的共享所有权直到没有其他线程具有此互斥量的独占或升级所有权或者是达到abs_time。

返回:如果获得共享所有权,则返回true。否则返回false。

抛出:interprocess_exception异常。

void unlock_sharable()

前提条件:线程必须有互斥量的共享所有权。

效果:调用者线程释放互斥量的共享所有权。

抛出:一个派生自interprocess_exception的异常。

 

升级锁定

void lock_upgradable()

效果:调用者线程尝试获取互斥量的升级所有权,并且如果另一个线程已经有这个互斥量的独占或升级所有权,则线程等待直到它获取所有权为止。

抛出:interprocess_exception异常。

bool try_lock_upgradable ()

效果:调用者线程尝试获取互斥量的升级所有权而不等待。如果没有其他线程具有此互斥量的独占或升级所有权,则获取成功。

返回:如果能立即获得升级所有权,则返回true。如果需要等待,返回false。

抛出:interprocess_exception异常。

booltimed_lock_upgradable(const boost::posix_time::ptime &abs_time)

效果:调用者线程尝试获取互斥量的升级所有权直到没有其他线程具有此互斥量的独占或升级所有权或者是达到abs_time。

返回:如果获得升级所有权,则返回true。否则返回false。

抛出:interprocess_exception异常。

void unlock_upgradable()

前提条件:线程必须有互斥量的升级所有权。

效果:调用者线程释放互斥量的升级所有权。

抛出:一个派生自interprocess_exception的异常。

 

降级

voidunlock_and_lock_upgradable()

前提条件:线程必须有互斥量的独占所有权。

效果:线程原子释放独占所有权并且获得升级所有权。此操作不阻塞。

抛出:一个派生自interprocess_exception的异常。

void unlock_and_lock_sharable()

前提条件:线程必须有互斥量的独占所有权。

效果:线程原子释放独占所有权并且获得共享所有权。此操作不阻塞。

抛出:一个派生自interprocess_exception的异常。

voidunlock_upgradable_and_lock_sharable()

前提条件:线程必须有互斥量的升级所有权。

效果:线程原子释放升级所有权并且获得共享所有权。此操作不阻塞。

抛出:一个派生自interprocess_exception的异常。

 

升级

voidunlock_upgradable_and_lock()

前提条件:线程必须有互斥量的升级所有权。

效果:线程原子释放升级所有权并且获得独占所有权。此操作将阻塞直到所有具有共享所有权的线程释放它为止。

抛出:一个派生自interprocess_exception的异常。

booltry_unlock_upgradable_and_lock()

前提条件:线程必须有互斥量的升级所有权。

效果:线程原子释放升级所有权并且尝试获得独占所有权。此操作将失败,如果有线程具有共享所有权。但是它会保持升级所有权。

返回:如果获得独占所有权,返回true。否则返回false。

抛出:一个派生自interprocess_exception的异常。

booltimed_unlock_upgradable_and_lock(const boost::posix_time::ptime &abs_time)

前提条件:线程必须有互斥量的升级所有权。

效果:线程原子释放升级所有权并且尝试获得独占所有权,如果需要,等待直到abs_time。此操作将失败,如果有线程具有共享所有权或超时。但是它会保持升级所有权。

返回:如果获得独占所有权,返回true。否则返回false。

抛出:一个派生自interprocess_exception的异常。

booltry_unlock_sharable_and_lock()

前提条件:线程必须有互斥量的共享所有权。

效果:线程原子释放共享所有权并且尝试获得独占所有权。此操作将失败,如果有线程具有共享或升级所有权。但是它会保持共享所有权。

返回:如果获得独占所有权,返回true。否则返回false。

抛出:一个派生自interprocess_exception的异常。

booltry_unlock_sharable_and_lock_upgradable()

前提条件:线程必须有互斥量的共享所有权。

效果:线程原子释放共享所有权并且尝试获得升级所有权。此操作将失败,如果有线程具有共享或升级所有权。但是它会保持共享所有权。

返回:如果获得升级所有权,返回true。否则返回false。

抛出:一个派生自interprocess_exception的异常。

 

Boost.Interprocess升级互斥量类型和头文件

Boost.Interprocess提供如下的升级互斥量类型:

#include <boost/interprocess/sync/interprocess_upgradable_mutex.hpp>
#include <boost/interprocess/sync/named_upgradable_mutex.hpp>

 

共享锁和升级锁

共享锁和升级锁头文件

和普通互斥量一样,甚至在发生异常时,也能及时释放锁是很重要的。 Boost.Interprocess互斥量最好使用scoped_lock工具,并且这个类仅提供独占锁定。

因为我们有采用升级互斥量的共享锁定和升级锁定,因此我们有两种新工具:sharable_lock 和 upgradable_lock。这两个类都与scoped_lock类似,但是 sharable_lock在构造时需要共享锁,upgradable_lock在构造时需要升级锁。

这两个工具能与任何能够提供需要操作的同步对象一起使用。例如,一个用户自定义的无升级锁特征的互斥量类型能够使用sharable_lock ,如果同步对象提供lock_sharable() 和unlock_sharable()操作。

 

共享锁和升级锁头文件
  
  
  1. #include <boost/interprocess/sync/sharable_lock.hpp>  
  2. #include <boost/interprocess/sync/upgradable_lock.hpp>  

共享锁在析构函数中调用unlock_sharable(),升级锁在析构函数中调用 unlock_upgradable(),因此当有异常发生时,升级互斥量也总能解锁。局部锁有许多构造函数来lock, try_lock, timed_lock 一个互斥量或解锁。

  
  
  1. using namespace boost::interprocess;  
  2.    
  3. //Let's create any mutex type:  
  4. MutexType mutex;  
  5.    
  6. {  
  7.    //This will call lock_sharable()  
  8.    sharable_lock<MutexType> lock(mutex);  
  9.    
  10.    //Some code  
  11.    
  12.    //The mutex will be unlocked here  
  13. }  
  14.    
  15. {  
  16.    //This won't lock the mutex()  
  17.    sharable_lock<MutexType> lock(mutex, defer_lock);  
  18.    
  19.    //Lock it on demand. This will call lock_sharable()  
  20.    lock.lock();  
  21.    
  22.    //Some code  
  23.    
  24.    //The mutex will be unlocked here  
  25. }  
  26.    
  27. {  
  28.    //This will call try_lock_sharable()  
  29.    sharable_lock<MutexType> lock(mutex, try_to_lock);  
  30.    
  31.    //Check if the mutex has been successfully locked  
  32.    if(lock){  
  33.       //Some code  
  34.    }  
  35.    //If the mutex was locked it will be unlocked  
  36. }  
  37.    
  38. {  
  39.    boost::posix_time::ptime abs_time = ...  
  40.    
  41.    //This will call timed_lock_sharable()  
  42.    scoped_lock<MutexType> lock(mutex, abs_time);  
  43.    
  44.    //Check if the mutex has been successfully locked  
  45.    if(lock){  
  46.       //Some code  
  47.    }  
  48.    //If the mutex was locked it will be unlocked  
  49. }  
  50.    
  51. {  
  52.    //This will call lock_upgradable()  
  53.    upgradable_lock<MutexType> lock(mutex);  
  54.    
  55.    //Some code  
  56.    
  57.    //The mutex will be unlocked here  
  58. }  
  59.    
  60. {  
  61.    //This won't lock the mutex()  
  62.    upgradable_lock<MutexType> lock(mutex, defer_lock);  
  63.    
  64.    //Lock it on demand. This will call lock_upgradable()  
  65.    lock.lock();  
  66.    
  67.    //Some code  
  68.    
  69.    //The mutex will be unlocked here  
  70. }  
  71.    
  72. {  
  73.    //This will call try_lock_upgradable()  
  74.    upgradable_lock<MutexType> lock(mutex, try_to_lock);  
  75.    
  76.    //Check if the mutex has been successfully locked  
  77.    if(lock){  
  78.       //Some code  
  79.    }  
  80.    //If the mutex was locked it will be unlocked  
  81. }  
  82.    
  83. {  
  84.    boost::posix_time::ptime abs_time = ...  
  85.    
  86.    //This will call timed_lock_upgradable()  
  87.    scoped_lock<MutexType> lock(mutex, abs_time);  
  88.    
  89.    //Check if the mutex has been successfully locked  
  90.    if(lock){  
  91.       //Some code  
  92.    }  
  93.    //If the mutex was locked it will be unlocked  
  94. }  

upgradable_lock 和sharable_lock 提供更多的特性和操作,更多详情可查阅它们的参考手册。

 

通过移动语义转移锁

简单锁转移

锁转移概要

转移未锁定的锁

转移失败

进程间通信为不支持右值引用的编译器使用它自己的转移语义仿真代码。这只是一个临时解决方案直到Boost转移语义库被接受。

局部所以及类似的工具提供了简单资源管理的可能性,但是采用例如升级互斥量这样的高级互斥量类型,就有如下操作:一个获取的锁类型被释放,另一个锁类型被原子获取。这由升级锁操作,例如unlock_and_lock_sharable()完成。

使用锁转移操作,这些操作能够有效的进行。一个锁转移操作显式指出一个锁拥有的互斥量通过执行原子解锁/加锁操作被转移到另一个锁。

 

简单锁转移

考虑一个线程在开始的时候会修改一些数据,但之后,它在很长的时间里仅读取数据。代码可以获取独占锁,修改数据然后原子释放独占锁并且获取共享锁。通过这一系列操作,我们可以保证没有其他线程在转移中能修改数据,并且更多的读线程能获取共享锁,从而提高并发性。如果没有锁转移操作,代码将编写为如下:

  
  
  1. using boost::interprocess;  
  2. interprocess_upgradable_mutex mutex;  
  3.    
  4. //Acquire exclusive lock  
  5. mutex.lock();  
  6.    
  7. //Modify data  
  8.    
  9. //Atomically release exclusive lock and acquire sharable lock.  
  10. //More threads can acquire the sharable lock and read the data.  
  11. mutex.unlock_and_lock_sharable();  
  12.    
  13. //Read data  
  14.    
  15. //Explicit unlocking  
  16. mutex.unlock_sharable();  

这很简单,但是如果出现异常,就很难知道当异常抛出的时候互斥量拥有什么类型的锁,以及我们应该调用什么函数去解锁它。

  
  
  1. try{  
  2.    //Mutex operations  
  3. }  
  4. catch(...){  
  5.    //What should we call? "unlock()" or "unlock_sharable()"  
  6.    //Is the mutex locked?  
  7. }  

我们可以使用锁转移来简化所有的这些管理:

  
  
  1. using boost::interprocess;  
  2. interprocess_upgradable_mutex mutex;  
  3.    
  4. //Acquire exclusive lock  
  5. scoped_lock s_lock(mutex);  
  6.    
  7. //Modify data  
  8.    
  9. //Atomically release exclusive lock and acquire sharable lock.  
  10. //More threads can acquire the sharable lock and read the data.  
  11. sharable_lock(move(s_lock));  
  12.    
  13. //Read data  
  14.    
  15. //The lock is automatically unlocked calling the appropriate unlock  
  16. //function even in the presence of exceptions.  
  17. //If the mutex was not locked, no function is called.  

如我们所看到的,无论在任何时候抛出异常,互斥量都能调用适当的unlock()或unlock_sharable()方法自动解锁。

 

锁转移概要

转移到局部锁

转移到升级锁

转移到共享锁

有许多锁转移操作,我们可以根据出现在升级互斥量操作中的操作来分类:

  • 保证成功和非阻塞:任何从更严格的锁至次严格的锁。局部的->升级的、局部的->共享的、升级的->共享的。
  • 不保证成功:如果没有其他线程已经获取此升级或独占锁,则从共享的->独占的操作可能成功。此操作是一个尝试操作。
  • 如果无限等待,保证能成功:从升级的->局部的转换将成功,但需要等待所有共享锁被释放。由于这是一个阻塞操作,我们也可以选择不无限等待而仅仅尝试或仅等待超时即可。

 

转移到局部锁

转移到局部锁仅在从upgradable_lock且请求阻塞操作情况下保证成功,这源于此操作需要等待直到所有的共享锁被释放的事实。使用者可以使用”try”或”timed”转移来避免无限锁定,但成功性得不到保证。

从sharable_lock 的转换从来得不到保证,因此仅允许尝试操作:

  
  
  1. //Conversions to scoped_lock  
  2. {  
  3.    upgradable_lock<Mutex>  u_lock(mut);  
  4.    //This calls unlock_upgradable_and_lock()  
  5.    scoped_lock<Mutex>      e_lock(move(u_lock));  
  6. }  
  7. {  
  8.    upgradable_lock<Mutex>  u_lock(mut);  
  9.    //This calls try_unlock_upgradable_and_lock()  
  10.    scoped_lock<Mutex>      e_lock(move(u_lock, try_to_lock));  
  11. }  
  12. {  
  13.    boost::posix_time::ptime t = test::delay(100);  
  14.    upgradable_lock<Mutex>  u_lock(mut);  
  15.    //This calls timed_unlock_upgradable_and_lock()  
  16.    scoped_lock<Mutex>      e_lock(move(u_lock));  
  17. }  
  18. {  
  19.    sharable_lock<Mutex>    s_lock(mut);  
  20.    //This calls try_unlock_sharable_and_lock()  
  21.    scoped_lock<Mutex>      e_lock(move(s_lock, try_to_lock));  
  22. }  

 

转移到升级锁

仅当从 scoped_lock转移为upgradable_lock 是保证成功的,因为局部锁是比升级锁更严格的锁定。此操作非阻塞。

从sharable_lock 的转换不保证成功,因此仅允许尝试操作:

  
  
  1. //Conversions to upgradable  
  2. {  
  3.    sharable_lock<Mutex>    s_lock(mut);  
  4.    //This calls try_unlock_sharable_and_lock_upgradable()  
  5.    upgradable_lock<Mutex>  u_lock(move(s_lock, try_to_lock));  
  6. }  
  7. {  
  8.    scoped_lock<Mutex>      e_lock(mut);  
  9.    //This calls unlock_and_lock_upgradable()  
  10.    upgradable_lock<Mutex>  u_lock(move(e_lock));  
  11. }  

 

转移到共享锁

所有转移至sharable_lock都保证是成功的,因为升级锁和局部所都比共享锁严格。这些操作非阻塞:

  
  
  1. //Conversions to sharable_lock  
  2. {  
  3.    upgradable_lock<Mutex>  u_lock(mut);  
  4.    //This calls unlock_upgradable_and_lock_sharable()  
  5.    sharable_lock<Mutex>    s_lock(move(u_lock));  
  6. }  
  7. {  
  8.    scoped_lock<Mutex>      e_lock(mut);  
  9.    //This calls unlock_and_lock_sharable()  
  10.    sharable_lock<Mutex>    s_lock(move(e_lock));  
  11. }  

 

转移未锁定的锁

在之前的例子中,使用在转移操作中的互斥量预先被锁定了:

  
  
  1. Mutex mut;  
  2.   
  3.  //This calls mut.lock()  
  4.  scoped_lock<Mutex>      e_lock(mut);  
  5.   
  6.  //This calls unlock_and_lock_sharable()  
  7.  sharable_lock<Mutex>    s_lock(move(e_lock));  

但是使用一个未锁定的源来执行转移是可能的,基于显式解锁、try/timed或defer_lock构造函数:

  
  
  1. //These operations can leave the mutex unlocked!  
  2.    
  3. {  
  4.    //Try might fail  
  5.    scoped_lock<Mutex>      e_lock(mut, try_to_lock);  
  6.    sharable_lock<Mutex>    s_lock(move(e_lock));  
  7. }  
  8. {  
  9.    //Timed operation might fail  
  10.    scoped_lock<Mutex>      e_lock(mut, time);  
  11.    sharable_lock<Mutex>    s_lock(move(e_lock));  
  12. }  
  13. {  
  14.    //Avoid mutex locking  
  15.    scoped_lock<Mutex>      e_lock(mut, defer_lock);  
  16.    sharable_lock<Mutex>    s_lock(move(e_lock));  
  17. }  
  18. {  
  19.    //Explicitly call unlock  
  20.    scoped_lock<Mutex>      e_lock(mut);  
  21.    e_lock.unlock();  
  22.    //Mutex was explicitly unlocked  
  23.    sharable_lock<Mutex>    s_lock(move(e_lock));  
  24. }  

如果源互斥量未被锁定:

  • 目标锁不执行原子unlock_xxx_和_lock_xxx操作。
  • 目标锁未被锁定。
  • 源锁被释放并且互斥量的所有权被转移至目标。
  
  
  1. {  
  2.    scoped_lock<Mutex>      e_lock(mut, defer_lock);  
  3.    sharable_lock<Mutex>    s_lock(move(e_lock));  
  4.    
  5.    //Assertions  
  6.    assert(e_lock.mutex() == 0);  
  7.    assert(s_lock.mutex() != 0);  
  8.    assert(e_lock.owns()  == false);  
  9. }  

 

转移失败

当执行锁转移时,操作可能失败:

  • 被执行的原子互斥量解锁加锁函数可能会抛出异常。
  • 被执行的原子函数可能是”try”或”timed”函数,它们可能会失败。

在第一种情况中,互斥量的所有权没有转移并且源锁的析构函数会解锁互斥量:

  
  
  1. {  
  2.    scoped_lock<Mutex>      e_lock(mut, defer_lock);  
  3.    
  4.    //This operations throws because  
  5.    //"unlock_and_lock_sharable()" throws!!!  
  6.    sharable_lock<Mutex>    s_lock(move(e_lock));  
  7.    
  8.    //Some code ...  
  9.    
  10.    //e_lock's destructor will call "unlock()"  
  11. }  

在第二种情况中,如果一个内部的 "try"或"timed"操作失败(返回”false”),则互斥量的所有权没有转移,源锁未改变并且目标锁的状态与默认的构造相同:

  
  
  1. {  
  2.    sharable_lock<Mutex>    s_lock(mut);  
  3.    
  4.    //Internal "try_unlock_sharable_and_lock_upgradable()" returns false  
  5.    upgradable_lock<Mutex>  u_lock(move(s_lock, try_to_lock));  
  6.    
  7.    assert(s_lock.mutex() == &mut);  
  8.    assert(s_lock.owns()  == true);  
  9.    assert(u_lock.mutex() == 0);  
  10.    assert(u_lock.owns()  == false);  
  11.    
  12.    //u_lock's destructor does nothing  
  13.    //s_lock's destructor calls "unlock()"  
  14. }  

 

文件锁

什么是文件锁

文件锁操作

文件锁定中的局部锁和共享锁

小心:同步限制

小心iostream写入

 

什么是文件锁?

文件锁是一种进程间通信机制,它使用一个嵌入至文件中的互斥量来保护文件读写的并发操作。这种嵌入式互斥量具有共享锁和独占锁的能力。采用文件锁,现有的文件可以做为一个互斥量,而不需要创建额外的同步对象来控制文件读写的并发性。

一般来说,我们有两种文件锁定能力:

  • 咨询锁定:操作系统内核维护一个已被锁定文件的列表。但是甚至一个进程已经获得了一个共享锁,也不阻止向这些文件写数据或当一个进程已经获取了独占锁,也不阻止从文件中读数据。任何进程可以忽略一个咨询锁。这意味着咨询锁是用于合作进程的,进程间彼此信任。这与保护共享内存片内数据的互斥量类似:任何连接至那块内存的进程可以覆盖数据,但是合作的进程使用互斥量来保护数据首先获取互斥锁。
  • 强制锁定:操作系统内核检查每次读写请求,从而校验操作能按照获取的锁来执行。读/写均被阻塞直到锁被释放。

Boost.Interprocess 基于可移植性的原因使用咨询锁定。这意味着所有并发访问文件的进程需要合作使用文件锁来同步访问。

在一些系统中,文件锁定甚至能被进一步完善成记录锁定,在此情况下使用者能在文件中指定一个字节范围,然后在其上使用锁定。这允许数个进程的写操作能够并发进行,如果它们需要的只是对文件不同部分的访问。Boost.Interprocess目前提供记录锁定,但将来可能会提供。要使用文件锁,仅需包含:

#include <boost/interprocess/sync/file_lock.hpp>

文件锁定是一个具有进程生命周期的类。这意味着如果一个拥有文件锁的进程结束或崩溃了,操作系统会自动解锁它。这个特性在某些情况下非常有用,例如我们想保证甚至当进程崩溃的情况下自动解锁从而避免在系统中留下锁定了的资源。文件锁使用文件名为参数进行构造:

  
  
  1. #include <boost/interprocess/sync/file_lock.hpp>  
  2.    
  3. int main()  
  4. {  
  5.    //This throws if the file does not exist or it can't  
  6.    //open it with read-write access!  
  7.    boost::interprocess::file_lock flock("my_file");  
  8.    return 0;  
  9. }  

 

文件锁操作

文件锁具有通常的互斥量操作,同时具有共享锁的能力。这意味着我们可以有很多读线程拥有共享锁,同时很多写线程拥有独占锁等待直到读线程完成它们的任务。

然后,文件锁支持升级锁定或升级或降级(锁转移),因此它比升级锁受限多。下面是操作:

void lock()

效果:调用者线程尝试获取文件锁的独占所有权。如果其他线程具有互斥量的独占或共享所有权,它等待直到能够获取所有权为止。

抛出:interprocess_exception异常。

bool try_lock()

效果:调用者线程尝试获取文件锁的独占所有权而不等待。如果没有其他线程具有互斥量的独占或共享所有权,此操作成功。

返回:如果立刻获取到了独占所有权返回true。如果需要等待,返回false。

抛出:interprocess_exception异常。

bool timed_lock(constboost::posix_time::ptime &abs_time)

效果:调用者线程尝试获取文件锁的独占所有权,等待直到没有其他线程拥有此文件锁的独占或共享所有权,或达到abs_time

返回:如果获取到了独占所有权返回true。否则返回false。

抛出:interprocess_exception异常。

void unlock()

前提条件:线程必须有文件锁的独占所有权。

效果:调用者线程释放文件锁的独占所有权。

抛出:一个派生自interprocess_exception的异常。

void lock_sharable()

效果:调用者线程尝试获取文件锁的共享所有权。如果其他线程具有互斥量的独占所有权,它等待直到能够获取所有权为止。

抛出:interprocess_exception异常。

bool try_lock_sharable()

效果:调用者线程尝试获取文件锁的共享所有权而不等待。如果没有其他线程具有互斥量的独占所有权,此操作成功。

返回:如果立刻获取到了共享所有权返回true。如果需要等待,返回false。

抛出:interprocess_exception异常。

booltimed_lock_sharable(const boost::posix_time::ptime &abs_time)

效果:调用者线程尝试获取文件锁的共享所有权,等待直到没有其他线程拥有此文件锁的独占所有权,或达到abs_time

返回:如果获取到了共享所有权返回true。否则返回false。

抛出:interprocess_exception异常。

void unlock_sharable()

前提条件:线程必须有文件锁的共享所有权。

效果:调用者线程释放文件锁的共享所有权。

抛出:一个派生自interprocess_exception的异常。

更多的文件锁方法,参考file_lock reference

 

文件锁定中的局部锁和共享锁

当出现异常时,scoped_locksharable_lock 能使文件锁定更容易,就好像与互斥量一样:

  
  
  1. #include <boost/interprocess/sync/file_lock.hpp>  
  2. #include <boost/interprocess/sync/sharable_lock.hpp>  
  3. //...  
  4.    
  5. using namespace boost::interprocess;  
  6. //This process reads the file  
  7. //    ...  
  8. //Open the file lock  
  9. file_lock f_lock("my_file");  
  10.    
  11. {  
  12.    //Construct a sharable lock with the filel lock.  
  13.    //This will call "f_lock.sharable_lock()".  
  14.    sharable_lock<file_lock> sh_lock(f_lock);  
  15.    
  16.    //Now read the file...  
  17.    
  18.    //The sharable lock is automatically released by  
  19.    //sh_lock's destructor  
  20. }  
  21. #include <boost/interprocess/sync/file_lock.hpp>  
  22. #include <boost/interprocess/sync/scoped_lock.hpp>  
  23.    
  24. //...  
  25.    
  26. using namespace boost::interprocess;  
  27. //This process writes the file  
  28. //    ...  
  29. //Open the file lock  
  30. file_lock f_lock("my_file");  
  31.    
  32. {  
  33.    //Construct a sharable lock with the filel lock.  
  34.    //This will call "f_lock.lock()".  
  35.    scoped_lock<file_lock> e_lock(f_lock);  
  36.    
  37.    //Now write the file...  
  38.    
  39.    //The exclusive lock is automatically released by  
  40.    //e_lock's destructor  
  41. }  

然而,锁转移只允许在同样类型的锁间进行,即:从一个共享锁至另一个共享锁或从一个局部锁至另一个局部锁。从一个局部锁至一个共享锁的转移是不允许的,因为 file_lock没有锁升级或降级函数,例如unlock_and_lock_sharable()。这将导致编译错误:

  
  
  1. //Open the file lock  
  2. file_lock f_lock("my_file");  
  3.    
  4. scoped_lock<file_lock> e_lock(f_lock);  
  5.    
  6. //Compilation error, f_lock has no "unlock_and_lock_sharable()" member!  
  7. sharable_lock<file_lock> e_lock(move(f_lock));  

 

小心:同步限制

如果你打算和使用具名互斥量一样使用文件锁,那要小心了,因为可移植的文件锁有同步限制,这主要是因为不同的实现(POSIX,Windows)提供了不同的保证。进程间文件锁有如下限制:

  • 如果文件锁同步来自同一进程的两个线程,则结果未定义。
  • 如果一个进程能使用两个指向同一文件的file_lock 对象,则结果未定义。

第一个限制主要来自POSIX,因为文件句柄是一个全进程(per-process)属性而不是全线程属性。这意味着如果一个线程使用了一个file_lock对象来锁一个文件,另外的线程将检测到文件被锁定了。然而,Windows文件锁机制提供了进程同步保证,因此一个线程尝试去锁已经被锁定的文件,线程将阻塞。

第二个限制主要来自在Windows下文件锁定同步状态与一个单独的文件描述器绑定的事实。这意味着如果两个file_lock对象被创建为指向同一个文件,则不保证同步。在POSIX下,当两个文件描述符被用于锁一个文件,如果一个描述符关闭了,所有调用者进程设定的文件锁都被清除了。

归纳起来,如果你打算在进程中使用可移植的文件锁,须遵守如下限制:

  • 对每个文件,每个进程使用单独的file_lock对象。
  • 使用同一个线程锁定和解锁文件。
  • 如果你正在使用std::fstream/原生文件句柄写文件,同时使用文件锁在此文件上时,在释放所有文件锁前,不要关闭此文件。

 

小心iostream写入

正如我们所看到的,文件锁在同步两个进程时非常有用,但是在解锁文件锁前,应确保数据已经被写入至此文件。小心iostream类会有一些缓冲的措施,因此如果你想确保其他进程能够“看到”你写入的数据,可以有如下选择:

使用原生的文件函数(Unix系统下的read()/write()和Windows系统下的ReadFile/WriteFile),来代替iostream。

写操作在解锁文件锁前刷新数据,如果你正在使用标准的C函数,可以使用fflush,或者使用C++ iostreams的flush成员函数。在Windows下,你甚至不能使用其他类来访问同一个文件。

  
  
  1. //...  
  2.    
  3. using namespace boost::interprocess;  
  4. //This process writes the file  
  5. //    ...  
  6. //Open the file lock  
  7. fstream file("my_file")  
  8. file_lock f_lock("my_lock_file");  
  9.    
  10. {  
  11.    scoped_lock<file_lock> e_lock(f_lock);  
  12.    
  13.    //Now write the file...  
  14.    
  15.    //Flush data before unlocking the exclusive lock  
  16.    file.flush();  
  17. }  

 

消息队列

什么是消息队列

使用消息队列

 

什么是消息队列

消息队列类似于一个消息的链表。线程可以放置消息至此队列,也能从队列中删除消息。每个消息可以有一个优先级以便高优先级消息在低优先级消息前被读取。每个消息都有一些属性:

  • 优先级。
  • 消息长度。
  • 数据(如果长度大于0)

线程可以使用3种方法往消息队列中发送或读取一条消息:

  • 阻塞:在发送时如果队列已满或接收时队列已空,线程阻塞直到有新消息空间或有新消息。
  • 尝试:在发送时如果队列已满或接收时队列已空,线程马上返回,同时报个错误。
  • 延时:在发送时如果队列已满或接收时队列已空,线程尝试操作直到成功(返回成功状态)或超时(返回一个错误)。

消息队列仅在进程间拷贝原始字节,并且不发送对象。这意味着如果我们想通过消息队列发送一个对象,对象必须首先被二进制序列化。例如,我们可以在进程间发送整数,但不能发送std::string。你应该使用 Boost.Serialization或使用高级Boost.Interprocess通信机制在进程间发送复杂数据。

Boost.Interprocess消息队列是一种具名进程间通信:消息队列具名创建,具名打开,就好像一个文件一样。当创建一个消息队列,用户必须指定最大消息字节大小和消息队列能存储的最大消息数目。这些参数将定义资源(例如,如果使用共享内存,则是执行消息队列的共享内存的大小)。

  
  
  1. using boost::interprocess;  
  2. //Create a message_queue. If the queue  
  3. //exists throws an exception  
  4. message_queue mq  
  5.    (create_only         //only create  
  6.    ,"message_queue"     //name  
  7.    ,100                 //max message number  
  8.    ,100                 //max message size  
  9.    );  
  10. using boost::interprocess;  
  11. //Creates or opens a message_queue. If the queue  
  12. //does not exist creates it, otherwise opens it.  
  13. //Message number and size are ignored if the queue  
  14. //is opened  
  15. message_queue mq  
  16.    (open_or_create      //open or create  
  17.    ,"message_queue"     //name  
  18.    ,100                 //max message number  
  19.    ,100                 //max message size  
  20.    );  
  21. using boost::interprocess;  
  22. //Opens a message_queue. If the queue  
  23. //does not exist throws an exception.  
  24. message_queue mq  
  25.    (open_only           //only open  
  26.    ,"message_queue"     //name  
  27.    );  

消息队列采用调用静态的删除函数显式删除。

  
  
  1. using boost::interprocess;  
  2. message_queue::remove("message_queue");  

如果消息队列仍旧被其他进程使用,此函数将失败。

 

使用消息队列

为了使用消息队列,你必须包含如下头文件:

#include <boost/interprocess/ipc/message_queue.hpp>

在下面的例子中,第一个进程创建消息队列,然后写入一个整型数组。另一个进程读取数组然后校验它们的正确性。下面是第一个进程:

  
  
  1. #include <boost/interprocess/ipc/message_queue.hpp>  
  2. #include <iostream>  
  3. #include <vector>  
  4.    
  5. using namespace boost::interprocess;  
  6.    
  7. int main ()  
  8. {  
  9.    try{  
  10.       //Erase previous message queue  
  11.       message_queue::remove("message_queue");  
  12.    
  13.       //Create a message_queue.  
  14.       message_queue mq  
  15.          (create_only               //only create  
  16.          ,"message_queue"           //name  
  17.          ,100                       //max message number  
  18.          ,sizeof(int)               //max message size  
  19.          );  
  20.    
  21.       //Send 100 numbers  
  22.       for(int i = 0; i < 100; ++i){  
  23.          mq.send(&i, sizeof(i), 0);  
  24.       }  
  25.    }  
  26.    catch(interprocess_exception &ex){  
  27.       std::cout << ex.what() << std::endl;  
  28.       return 1;  
  29.    }  
  30.    
  31.    return 0;  
  32. }  

下面是第二个进程:

  
  
  1. #include <boost/interprocess/ipc/message_queue.hpp>  
  2. #include <iostream>  
  3. #include <vector>  
  4.    
  5. using namespace boost::interprocess;  
  6.    
  7. int main ()  
  8. {  
  9.    try{  
  10.       //Open a message queue.  
  11.       message_queue mq  
  12.          (open_only        //only create  
  13.          ,"message_queue"  //name  
  14.          );  
  15.    
  16.       unsigned int priority;  
  17.       message_queue::size_type recvd_size;  
  18.    
  19.       //Receive 100 numbers  
  20.       for(int i = 0; i < 100; ++i){  
  21.          int number;  
  22.          mq.receive(&number, sizeof(number), recvd_size, priority);  
  23.          if(number != i || recvd_size != sizeof(number))  
  24.             return 1;  
  25.       }  
  26.    }  
  27.    catch(interprocess_exception &ex){  
  28.       message_queue::remove("message_queue");  
  29.       std::cout << ex.what() << std::endl;  
  30.       return 1;  
  31.    }  
  32.    message_queue::remove("message_queue");  
  33.    return 0;  
  34. }  

若要了解更多关于此类的信息和所有的操作,请参考 message_queue

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值