$include_dir="/home/hyper-archives/boost-users/include"; include("$include_dir/msg-header.inc") ?>
Subject: Re: [Boost-users] shared_lock( ) causes Segmentation Fault in CentOS 5.5 x64?
From: Howard Hinnant (howard.hinnant_at_[hidden])
Date: 2011-03-15 10:11:16
On Mar 14, 2011, at 11:39 PM, DesertFish wrote:
> Dear Pals,
> 
> I wrote a server program, and there is a class called "apInfoMutex",
> which can store client's file descriptor info and what messages they sent;
> the class code is as below:
> ---
> class apInfoTable{
>   public:
>      apInfoTable();
>      boost::shared_mutex rwMutex;
>      void   addFD(int apfd, char *srvceType, vector<string> *codeList);
>      void   delFD(int apfd);
>      bool   getFD(list<int> *des, char *srvceType, char *srvceCode);
> 
>   private:
>      boost::unordered_map<int, boost::unordered_set<pair<string, string> >
>> 
> tableFD;
>      boost::unordered_map<pair<string, string>, boost::unordered_set<int> >
> 
> tablePush;
> };
> ---
> there is just one apInfoTable as global declaration in my program,
> but there would be two or more threads to calling these public 
> method at the same time, and there is a method called "getFD()" in this
> class,
> the code is as below:
> ---
> bool apInfoTable::getFD(list<int> *des, char *type, char *code){
>   bool flag=false;
>   boost::shared_lock<boost::shared_mutex> lock(rwMutex);
>   for(boost::unordered_set<int>::iterator it=tablePush[pair<string, string>
> (type, code)].begin();it!=tablePush[pair<string, string>(type, code)].end
> ();it++){  
>      flag=true;
>      des->push_back(*it);
>   }
>   return flag;
> }
> ---
> I found if I calling getFD() method like this:
> char type[12];
> char code[32];
> list<int> fdList;
> memset(type, 0x00, sizeof(type));
> memset(code, 0x00, sizeof(code));
> memcpy(type, "My Type", sizeof("My Type"));
> memcpy(code, "My Code", sizeof("My Code"));
> fdList.clear();
> bool rc=getFD(&fdList, type, code);
> 
> I will got "Segmentation Fault" problem,
> but if I put different parameter like this:
> bool rc=getFD(&fdList, "My Type", "My Code");
> 
> or using boost::mutex instead of shared_lock() and shared_mutex in getFD(),
> there wouldn't be any problem when running program.
> 
> is there any problem with my code?
> or did I use shared_lock() and shared_mutex incorrectly?
> 
> 
> P.S--I tried using unique_lock or scoped_lock instead of shared_lock(),
> but there is just no any problem when I using normal boost::mutex
It looks like you are locking your structure in "read mode" and then writing to it:
>   boost::shared_lock<boost::shared_mutex> lock(rwMutex);
>   for(boost::unordered_set<int>::iterator it=tablePush[pair<string, string>
> (type, code)].begin();it!=tablePush[pair<string, string>(type, code)].end
> ();it++){  
>      flag=true;
>      des->push_back(*it);
>   }
If you want to lock it in write mode do this:
boost::unique_lock<boost::shared_mutex> lock(rwMutex);
-Howard