STL与多线程+写时拷贝

 

STL与多线程+写时拷贝

分类: Linux多线程编程   184人阅读  评论(0)  收藏  举报

       STL并不是线程安全的,当多个线程同时读取STL时没什么问题。当多个线程中有写STL时则非线程安全,导致其它线程的end()检测或迭代器算术操作无意义,修改操作可能导致STL重新分配内存,原来的迭代器可能失效。要实现多线程安全:可以用锁机制,也可以将写操作推后。

      例子:一个线程输出vector元素,另一个容器不断往vector添加元素。最后出现的结果可能时段错误,也可能运行正常。

[cpp]  view plain copy
  1. #include<iostream>  
  2. #include<exception>  
  3. #include<pthread.h>  
  4. #include<unistd.h>  
  5. #include<string>  
  6. #include<vector>  
  7. #include<boost/noncopyable.hpp>  
  8. using namespace std;  
  9. using namespace boost;  
  10. class Mutex:noncopyable{  
  11.     public:  
  12.         Mutex(){  
  13.             pthread_mutex_init(&mutex,NULL);  
  14.         }  
  15.         void lock() const{  
  16.             pthread_mutex_lock(&mutex);  
  17.         }  
  18.         void unlock() const{  
  19.             pthread_mutex_unlock(&mutex);  
  20.         }  
  21.         ~Mutex(){  
  22.             pthread_mutex_destroy(&mutex);  
  23.         }  
  24.     private:  
  25.         mutable pthread_mutex_t mutex;  
  26. };  
  27. class MutexLockGuard:noncopyable{  
  28.     public:  
  29.         explicit MutexLockGuard(Mutex& mutex):mutex_(mutex){//显示构造  
  30.             mutex_.lock();  
  31.         }  
  32.         ~MutexLockGuard(){  
  33.             mutex_.unlock();  
  34.         }  
  35.     private:  
  36.         Mutex& mutex_;  
  37. };  
  38. Mutex mutex;  
  39. void* worker1(void* arg){//线程1输出容器元素  
  40.     vector<int>* vec=(vector<int>*)arg;  
  41.     //sleep(1);  
  42.     //MutexLockGuard guard(mutex);  
  43.     for(vector<int>::iterator it=(*vec).begin();it!=(*vec).end();it++){  
  44.         cout<<*it<<" ";  
  45.     }  
  46.     cout<<endl;  
  47. }  
  48. void* worker2(void* arg){//线程2向容器添加元素  
  49.     vector<int>* vec=(vector<int>*)arg;  
  50.     //MutexLockGuard guard(mutex);  
  51.     for(int i=0;i<100;i++){  
  52.         (*vec).push_back(i*10);  
  53.     }  
  54. }  
  55. int main(){  
  56.     pthread_t pid1,pid2;  
  57.     vector<int> vec;  
  58.     vec.push_back(-1);  
  59.     pthread_create(&pid2,NULL,worker2,&vec);  
  60.     pthread_create(&pid1,NULL,worker1,&vec);  
  61.     pthread_join(pid1,NULL);  
  62.     pthread_join(pid2,NULL);  
  63.     return 0;  
  64. }  

异常的输出结果部分:

 0 0 0 0 0 0 0 0 0 0 0 0 0 0 段错误 (核心已转储)

采用锁机制(代码注释的部分),正常...


采用写时拷贝技术解决上述问题:

        写容器的线程采用写时拷贝技术(写之前值共享内存,写时先拷贝一份再写到那份拷贝中去)

[cpp]  view plain copy
  1. #include<iostream>  
  2. #include<exception>  
  3. #include<pthread.h>  
  4. #include<unistd.h>  
  5. #include<string>  
  6. #include<vector>  
  7. #include<assert.h>  
  8. #include<boost/noncopyable.hpp>  
  9. #include<boost/shared_ptr.hpp>  
  10. using namespace std;  
  11. using namespace boost;  
  12. class Mutex:noncopyable{//互斥量的封装  
  13.     public:  
  14.         Mutex(){  
  15.             pthread_mutex_init(&mutex,NULL);  
  16.         }  
  17.         void lock() const{  
  18.             pthread_mutex_lock(&mutex);  
  19.         }  
  20.         void unlock() const{  
  21.             pthread_mutex_unlock(&mutex);  
  22.         }  
  23.         ~Mutex(){  
  24.             pthread_mutex_destroy(&mutex);  
  25.         }  
  26.     private:  
  27.         mutable pthread_mutex_t mutex;  
  28. };  
  29. class MutexLockGuard:noncopyable{//RAII管理互斥量  
  30.     public:  
  31.         explicit MutexLockGuard(Mutex& mutex):mutex_(mutex){//显示构造  
  32.             mutex_.lock();  
  33.         }  
  34.         ~MutexLockGuard(){  
  35.             mutex_.unlock();  
  36.         }  
  37.     private:  
  38.         Mutex& mutex_;//注意是引用,否则会出错  
  39. };  
  40. Mutex mutex;  
  41. shared_ptr<vector<int> > vec_ptr;  
  42. void* worker1(void* arg){//线程1输出容器元素  
  43.     int i=0;  
  44.     do{  
  45.         cout<<"read vector"<<endl;  
  46.         {//以下操作限制在一个域中  
  47.             shared_ptr<vector<int> > temp_ptr;//作为观察者  
  48.             {  
  49.                 MutexLockGuard guard(mutex);//获取锁  
  50.                 temp_ptr=vec_ptr;//拷贝shared_ptr对象,被管理对象引用计数加1  
  51.                 assert(!temp_ptr.unique());  
  52.             }  
  53.             for(vector<int>::iterator it=temp_ptr->begin();it!=temp_ptr->end();it++){//用观察者shared_ptr作为输出  
  54.                 cout<<*it<<" ";  
  55.             }  
  56.             cout<<endl;  
  57.         }  
  58.         sleep(0.1);  
  59.     }while(++i<5);//5次输出操作  
  60. }  
  61. void* worker2(void* arg){//线程2向容器添加元素  
  62.     int i=0;  
  63.     do{  
  64.         {//限制作用域,退出作用域后临时对象会被析构  
  65.             cout<<"write vector"<<endl;  
  66.             MutexLockGuard guard(mutex);//获取锁  
  67.             if(!vec_ptr.unique()){//若有其它读者,不会有多个写者因为上一句锁已经保证了只有一个写者进入此临界区  
  68.                 vec_ptr.reset(new vector<int>(*vec_ptr));//写时拷贝,此时若有读者,那么读者读取的元素是旧的那个vector,而新的vector将由写者操作  
  69.                 cout<<"copy on write"<<endl;  
  70.             }  
  71.             assert(vec_ptr.unique());  
  72.             vec_ptr->push_back(i*100);//写入操作  
  73.         }  
  74.         sleep(0.1);  
  75.     }while(++i<5);  
  76. }  
  77. int main(){  
  78.     pthread_t pid1,pid2;  
  79.     vec_ptr.reset(new vector<int>(1,-1));  
  80.     pthread_create(&pid1,NULL,worker1,NULL);  
  81.     pthread_create(&pid2,NULL,worker2,NULL);  
  82.     pthread_join(pid1,NULL);  
  83.     pthread_join(pid2,NULL);  
  84.     return 0;  
  85. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值