what' the flaw in code

L

layman

class Dictionary
{
public :
Dictionary()
{
pthread_mutex_init(&mutex_,0)
}
virtual ~Dictionary()
{
pthread_mutex_destroy(&mutex_);
}
std::String transalate(std::string word) throw (std::string)
{
pthread_mutex_lock(&mutex_);
if (dict.find(word) == dict.end())
{
throw std::string(word + "not found");
}
std::string transalation = dict[MH:word];
pthread_mutex_unlock(&mutex_);
return transalation;
}
void addToDictionary (std::string word,std::string
transalation) throw (std::string)
{
pthread_mutex_lock(&mutex_);
if (dict.find(word)!=dict.end())
{
throw std::string(word + "already exits");
}
dict[MH:word]=transalation;
pthread_mutex_unlock(&mutex_);
}
private:
typedef std::map<std::string,std ::string>dictType;
dictType dict;
pthread_mutex_t mutex_;
}

Here would like to know.What this class actually intended for?
What is the flaw in the class & what is the fix?
How to improve the perfomance in the concurrent environment,or how to
better perfomance.
what will be the ideal test which can demonstrate perfomance
improvement in the concurrent environment
 
F

Francesco S. Carta

layman said:
class Dictionary
{
  public :
        Dictionary()
                       {
                         pthread_mutex_init(&mutex_,0)
                       }
        virtual ~Dictionary()
                        {
                         pthread_mutex_destroy(&mutex_);
                        }
        std::String transalate(std::string word) throw (std::string)
                    {
                      pthread_mutex_lock(&mutex_);
                      if (dict.find(word) == dict.end())
                         {
                           throw std::string(word + "not found");
                         }
                      std::string transalation = dict[MH:word];
                      pthread_mutex_unlock(&mutex_);
                      return transalation;
                    }
         void addToDictionary (std::string word,std::string
transalation) throw (std::string)
              {
                      pthread_mutex_lock(&mutex_);
                      if (dict.find(word)!=dict.end())
                          {
                            throw std::string(word + "already exits");
                          }
                      dict[MH:word]=transalation;
                      pthread_mutex_unlock(&mutex_);
               }
       private:
           typedef std::map<std::string,std ::string>dictType;
           dictType dict;
           pthread_mutex_t mutex_;
  }

Here would like to know.What this class actually intended for?
What is the flaw in the class & what is the fix?
How to improve the perfomance in the concurrent environment,or how to
better perfomance.
what will be the ideal test which can demonstrate perfomance
improvement in the concurrent environment

But moreover: what is the name of the teacher that assigned this
homework?

You're not going to have it solved by someone else than you - at
least, not here - start by feeding it to the compiler and examine the
errors (if any). Reading the C++ FAQ is a must - you'll eventually
find the solution to many if not all of your questions there in the
FAQ.
 
F

Francesco S. Carta

Stuart Golodetz said:
layman said:
class Dictionary
{
  public :
        Dictionary()
                       {
                         pthread_mutex_init(&mutex_,0)
                       }
        virtual ~Dictionary()
                        {
                         pthread_mutex_destroy(&mutex_);
                        }
        std::String transalate(std::string word) throw (std::string)
                    {
                      pthread_mutex_lock(&mutex_);
                      if (dict.find(word) == dict.end())
                         {
                           throw std::string(word + "not found");
                         }
                      std::string transalation = dict[MH:word];
                      pthread_mutex_unlock(&mutex_);
                      return transalation;
                    }
         void addToDictionary (std::string word,std::string
transalation) throw (std::string)
              {
                      pthread_mutex_lock(&mutex_);
                      if (dict.find(word)!=dict..end())
                          {
                            throw std::string(word + "already exits");
                          }
                      dict[MH:word]=transalation;
                      pthread_mutex_unlock(&mutex_);
               }
       private:
           typedef std::map<std::string,std ::string>dictType;
           dictType dict;
           pthread_mutex_t mutex_;
  }
Here would like to know.What this class actually intended for?
What is the flaw in the class & what is the fix?
How to improve the perfomance in the concurrent environment,or how to
better perfomance.
what will be the ideal test which can demonstrate perfomance
improvement in the concurrent environment

A hint is to Google for RAII - if you do due diligence, that should help
you figure out the flaw at least. No-one here's going to do your
homework for you :)

Cheers,
Stu

Oh, the OP ran away really fast... the post disappeared from GG in a
couple of minutes, scary group this one, isn't it?
 
Ö

Öö Tiib

Oh, the OP ran away really fast... the post disappeared from GG in a
couple of minutes, scary group this one, isn't it?

Probably he realized that whoever assigned him the task might read
same groups and discover cheating. Erasing from google does not erase
it from usenet however.
 
C

cpp4ever

layman said:
class Dictionary
{
public :
Dictionary()
{
pthread_mutex_init(&mutex_,0)
}
virtual ~Dictionary()
{
pthread_mutex_destroy(&mutex_);
}
std::String transalate(std::string word) throw (std::string)
{
pthread_mutex_lock(&mutex_);
if (dict.find(word) == dict.end())
{
throw std::string(word + "not found");
}
std::string transalation = dict[MH:word];
pthread_mutex_unlock(&mutex_);
return transalation;
}
void addToDictionary (std::string word,std::string
transalation) throw (std::string)
{
pthread_mutex_lock(&mutex_);
if (dict.find(word)!=dict.end())
{
throw std::string(word + "already exits");
}
dict[MH:word]=transalation;
pthread_mutex_unlock(&mutex_);
}
private:
typedef std::map<std::string,std ::string>dictType;
dictType dict;
pthread_mutex_t mutex_;
}

Here would like to know.What this class actually intended for?
What is the flaw in the class & what is the fix?
How to improve the perfomance in the concurrent environment,or how to
better perfomance.
what will be the ideal test which can demonstrate perfomance
improvement in the concurrent environment

But moreover: what is the name of the teacher that assigned this
homework?

You're not going to have it solved by someone else than you - at
least, not here - start by feeding it to the compiler and examine the
errors (if any). Reading the C++ FAQ is a must - you'll eventually
find the solution to many if not all of your questions there in the
FAQ.

Moreover, if you want to learn C++ or gain some benefit from this
exercise, then you'll learn a lot more by finding and fixing the
mistakes and problems yourself. If you want to make a career writing and
modifying software then getting experience of doing this type of thing
is invaluable. Nobody will employ you to do so if all you do is get
others to do it for you.

JB
 
F

Francesco S. Carta

Öö Tiib said:
Probably he realized that whoever assigned him the task might read
same groups and discover cheating. Erasing from google does not erase
it from usenet however.

Furthermore, since people usually reply quoting the original posts,
erasing them wouldn't be effective even if GG was the only way to
access these groups... let's hope the OP is still reading our replies,
that will make it think twice before posting such kind of questions.

Actually, there is nothing wrong in trying to solve a problem by
seeking help in places like this - as far as I can see, even
professionals come here to get help - the issue arises when the
problem's holder doesn't make any attempt to solve it on its own :)
 
R

Ron Francis

<quote>
Actually, there is nothing wrong in trying to solve a problem by
seeking help in places like this - as far as I can see, even
professionals come here to get help - the issue arises when the
problem's holder doesn't make any attempt to solve it on its own :)
</quote>

I think that is the best and clearest advice given so far.

Regards
Ron Francis
www.RonaldFrancis.com
 
F

Francesco S. Carta

<quote>
Actually, there is nothing wrong in trying to solve a problem by
seeking help in places like this - as far as I can see, even
professionals come here to get help - the issue arises when the
problem's holder doesn't make any attempt to solve it on its own :)
</quote>

I think that is the best and clearest advice given so far.

Thank you very much, but other's advices aren't that bad ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,965
Messages
2,570,148
Members
46,710
Latest member
FredricRen

Latest Threads

Top