473,501 Members | 3,293 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::string and pthread_mutex_lock / pthread_mutex_lock

I've got a multithreaded application using std::string in Linux.
Performance is not very good so I ran Quantify(tm) to look at what is
happening.
Most of the time my app was calling pthread_mutex_lock and
pthread_mutex_unlock from different std::string methods. Since the
strings I'm using are never used in another thread I was wondering
whether I could get rid of these mutex calls altogether? Are there
some compiler options available (like in Solaris) to change the
behaviour of std::string?
<>bb

Jul 23 '05 #1
14 2646
"brad" wrote
I've got a multithreaded application using std::string in Linux.
Performance is not very good so I ran Quantify(tm) to look at what is
happening.
Most of the time my app was calling pthread_mutex_lock and
pthread_mutex_unlock from different std::string methods. Since the
strings I'm using are never used in another thread I was wondering
whether I could get rid of these mutex calls altogether? Are there
some compiler options available (like in Solaris) to change the
behaviour of std::string?


Very curios. I was thinking that pthreads is an optional library.
So STL's std::string should know nothing about pthreads or any threads.
Actually, it is the user's job to synchronize simultanous access from
multiple threads, the STL implementation should not try to do that for
the user because the library cannot know what the application logic will
be for obvious reasons.
I would say it's a bad STL implementation.
Jul 23 '05 #2

brad wrote:
I've got a multithreaded application using std::string in Linux.
Performance is not very good so I ran Quantify(tm) to look at what is
happening.
Most of the time my app was calling pthread_mutex_lock and
pthread_mutex_unlock from different std::string methods. Since the
strings I'm using are never used in another thread I was wondering
whether I could get rid of these mutex calls altogether? Are there
some compiler options available (like in Solaris) to change the
behaviour of std::string?
<>bb


Are the lock and unlock code inside your code, or in the std::string
implemenation?

Where exactly is the lock/unlock code residnig?

Jul 23 '05 #3
Uenal Mutlu wrote:

Very curios. I was thinking that pthreads is an optional library.
So STL's std::string should know nothing about pthreads or any threads.


Implementations of the standard library can (and do) use
system-dependent support functions. One reason for putting things in the
standard library is that they can't be implemented portably.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #4
"Pete Becker" wrote
Uenal Mutlu wrote:

Very curios. I was thinking that pthreads is an optional library.
So STL's std::string should know nothing about pthreads or any threads.


Implementations of the standard library can (and do) use
system-dependent support functions. One reason for putting things in the
standard library is that they can't be implemented portably.


Sure, but it should not be in this particular case because
it seems to be an unnecessary overhead built into
implementation which slows down the application.
What is your opinion on what the OP asked?
Jul 23 '05 #5

Uenal Mutlu wrote:
"brad" wrote
I've got a multithreaded application using std::string in Linux.
Performance is not very good so I ran Quantify(tm) to look at what is happening.
Most of the time my app was calling pthread_mutex_lock and
pthread_mutex_unlock from different std::string methods. Since the
strings I'm using are never used in another thread I was wondering
whether I could get rid of these mutex calls altogether? Are there
some compiler options available (like in Solaris) to change the
behaviour of std::string?
Very curios. I was thinking that pthreads is an optional library.
So STL's std::string should know nothing about pthreads or any

threads. Actually, it is the user's job to synchronize simultanous access from
multiple threads, the STL implementation should not try to do that for the user because the library cannot know what the application logic will be for obvious reasons.
I would say it's a bad STL implementation.


Not necessarily. The implementation code be using a lock/unlock to
handle shared variables like a reference counter.

Jul 23 '05 #6
Uenal Mutlu wrote:
"Pete Becker" wrote
Uenal Mutlu wrote:
Very curios. I was thinking that pthreads is an optional library.
So STL's std::string should know nothing about pthreads or any threads.
Implementations of the standard library can (and do) use
system-dependent support functions. One reason for putting things in the
standard library is that they can't be implemented portably.

Sure, but it should not be in this particular case because
it seems to be an unnecessary overhead built into
implementation which slows down the application.


This has very little to do with your original non sequitur.
What is your opinion on what the OP asked?


He didn't provide enough details for anyone to base an opinion on. Nor
did he ask for one.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #7
"Axter" wrote
Uenal Mutlu wrote:
"brad" wrote
I've got a multithreaded application using std::string in Linux.
Performance is not very good so I ran Quantify(tm) to look at what is happening.
Most of the time my app was calling pthread_mutex_lock and
pthread_mutex_unlock from different std::string methods. Since the
strings I'm using are never used in another thread I was wondering
whether I could get rid of these mutex calls altogether? Are there
some compiler options available (like in Solaris) to change the
behaviour of std::string?


Very curios. I was thinking that pthreads is an optional library.
So STL's std::string should know nothing about pthreads or any

threads.
Actually, it is the user's job to synchronize simultanous access from
multiple threads, the STL implementation should not try to do that

for
the user because the library cannot know what the application logic

will
be for obvious reasons.
I would say it's a bad STL implementation.


Not necessarily. The implementation code be using a lock/unlock to
handle shared variables like a reference counter.


Hmm.. but, are there such shared variables inside the implementation of std::string?
I cannot see for what they might be needed esp. in std::string.
Jul 23 '05 #8
Uenal Mutlu wrote:
"Axter" wrote
Uenal Mutlu wrote:
"brad" wrote
> I've got a multithreaded application using std::string in Linux. > Performance is not very good so I ran Quantify(tm) to look at what
is
> happening.
> Most of the time my app was calling pthread_mutex_lock and
> pthread_mutex_unlock from different std::string methods. Since
the > strings I'm using are never used in another thread I was wondering > whether I could get rid of these mutex calls altogether? Are there > some compiler options available (like in Solaris) to change the
> behaviour of std::string?

Very curios. I was thinking that pthreads is an optional library.
So STL's std::string should know nothing about pthreads or any threads.
Actually, it is the user's job to synchronize simultanous access from multiple threads, the STL implementation should not try to do

that for
the user because the library cannot know what the application
logic will
be for obvious reasons.
I would say it's a bad STL implementation.


Not necessarily. The implementation code be using a lock/unlock to
handle shared variables like a reference counter.


Hmm.. but, are there such shared variables inside the implementation

of std::string? I cannot see for what they might be needed esp. in std::string.


Yes.
The C++ standard allows for the implementation to have reference
counters.
If you have an std::string implementation that has a referance counter,
then you are sharing data between multiple instances of std::string in
such code as the following:

std::string data1 = "Hello World";
std::string data2(data1);
std::string data3 = data1;

In above code all three std::string variables are pointing to the same
buffer when using an implementation that uses reference counters.
When these objects go out of scope, they'll attempt to modify the
reference counter by decreasing it by one. (--refcount)
For a thread safe std::string version, this has to be synchronized.

Jul 23 '05 #9
"Axter" wrote> Uenal Mutlu wrote:
"Axter" wrote
Uenal Mutlu wrote:
> "brad" wrote
> > I've got a multithreaded application using std::string in Linux.
> > Performance is not very good so I ran Quantify(tm) to look at .... > > Most of the time my app was calling pthread_mutex_lock and
> > pthread_mutex_unlock from different std::string methods. Since .... > Very curios. I was thinking that pthreads is an optional library.
> So STL's std::string should know nothing about pthreads or any threads.
> Actually, it is the user's job to synchronize simultanous access

Not necessarily. The implementation code be using a lock/unlock to
handle shared variables like a reference counter.


Hmm.. but, are there such shared variables inside the implementation

of std::string?
I cannot see for what they might be needed esp. in std::string.


Yes.
The C++ standard allows for the implementation to have reference
counters.
If you have an std::string implementation that has a referance counter,
then you are sharing data between multiple instances of std::string in
such code as the following:

std::string data1 = "Hello World";
std::string data2(data1);
std::string data3 = data1;

In above code all three std::string variables are pointing to the same
buffer when using an implementation that uses reference counters.
When these objects go out of scope, they'll attempt to modify the
reference counter by decreasing it by one. (--refcount)
For a thread safe std::string version, this has to be synchronized.


I'm afraid such a reference counted STL implementation
would IMO give for some operations a different result than
a standard STL implementation. Therefore I doubt it could
be called STL.
Jul 23 '05 #10

Uenal Mutlu wrote:
"Axter" wrote> Uenal Mutlu wrote:
"Axter" wrote
> Uenal Mutlu wrote:
> > "brad" wrote
> > > I've got a multithreaded application using std::string in Linux. > > > Performance is not very good so I ran Quantify(tm) to look at
...
> > > Most of the time my app was calling pthread_mutex_lock and
> > > pthread_mutex_unlock from different std::string methods.
Since
... > > Very curios. I was thinking that pthreads is an optional
library. > > So STL's std::string should know nothing about pthreads or any threads. > > Actually, it is the user's job to synchronize simultanous access >
> Not necessarily. The implementation code be using a lock/unlock to > handle shared variables like a reference counter.

Hmm.. but, are there such shared variables inside the

implementation of std::string?
I cannot see for what they might be needed esp. in std::string.


Yes.
The C++ standard allows for the implementation to have reference
counters.
If you have an std::string implementation that has a referance counter, then you are sharing data between multiple instances of std::string in such code as the following:

std::string data1 = "Hello World";
std::string data2(data1);
std::string data3 = data1;

In above code all three std::string variables are pointing to the same buffer when using an implementation that uses reference counters.
When these objects go out of scope, they'll attempt to modify the
reference counter by decreasing it by one. (--refcount)
For a thread safe std::string version, this has to be synchronized.


I'm afraid such a reference counted STL implementation
would IMO give for some operations a different result than
a standard STL implementation. Therefore I doubt it could
be called STL.


Most STL implementations use reference counting for std::string.

Most major compilers like GNU, VC++ 6.0, and 7.x come with std::string
that has reference counting in the implementation.

Implementations that don't have reference counting are the exception,
and not the rule.

Jul 23 '05 #11
In article <11**********************@z14g2000cwz.googlegroups .com>,
"Axter" <te**@axter.com> wrote:
Most STL implementations use reference counting for std::string.

Most major compilers like GNU, VC++ 6.0, and 7.x come with std::string
that has reference counting in the implementation.

Implementations that don't have reference counting are the exception,
and not the rule.


Last time I counted, there were 4 major independent implementations.
Half use a refcounted string and half use the sso (short string
optimization). Dinkumware and Metrowerks (current versions) are the
ones using sso.

-Howard
Jul 23 '05 #12
Howard Hinnant wrote:
In article <11**********************@z14g2000cwz.googlegroups .com>,
"Axter" <te**@axter.com> wrote:
Most STL implementations use reference counting for std::string.

Most major compilers like GNU, VC++ 6.0, and 7.x come with std::string that has reference counting in the implementation.

Implementations that don't have reference counting are the exception, and not the rule.
Last time I counted, there were 4 major independent implementations.

Half use a refcounted string and half use the sso (short string
optimization). Dinkumware and Metrowerks (current versions) are the
ones using sso.

-Howard


I believe there are more then 4 major independent implement, and some
versions allow you to turn off the reference counter. (See Scott
Meyers Effective STL)

However, most of the major compilers come with versions that use
reference counters.

Jul 23 '05 #13
In article <11**********************@g14g2000cwa.googlegroups .com>,
"Axter" <te**@axter.com> wrote:
Howard Hinnant wrote:
In article <11**********************@z14g2000cwz.googlegroups .com>,
"Axter" <te**@axter.com> wrote:
Most STL implementations use reference counting for std::string.

Most major compilers like GNU, VC++ 6.0, and 7.x come with std::string that has reference counting in the implementation.

Implementations that don't have reference counting are the exception, and not the rule.


Last time I counted, there were 4 major independent implementations.

Half use a refcounted string and half use the sso (short string
optimization). Dinkumware and Metrowerks (current versions) are the
ones using sso.

-Howard


I believe there are more then 4 major independent implement, and some
versions allow you to turn off the reference counter. (See Scott
Meyers Effective STL)

However, most of the major compilers come with versions that use
reference counters.


Sorry, I counted wrong:

Dinkumware
gcc
Metrowerks
Rogue Wave
STLport

gcc and STLport have a somewhat intertwined heritage and I mistakenly
counted them as one.

Scott's ESTL counted 4, and diplomatically named them A - D. Note that
this fine book is copyrighted 2001.

I can tell you for sure that B is Metrowerks Windows Pro 6, released in
2000 (which was refcounted with an internal mutex at the time). And D
is Dinkumware (the one with short string optimization), not sure of the
version number. Since this book was written, Metrowerks changed to a
short string optimization (making somewhat different design tradeoffs
than implementation D). To the best of my knowledge the other
implementations are still reference counted, but my information may not
be up to date.

-Howard
Jul 23 '05 #14
Howard Hinnant wrote:
In article <11**********************@g14g2000cwa.googlegroups .com>,
"Axter" <te**@axter.com> wrote:
Howard Hinnant wrote:
In article <11**********************@z14g2000cwz.googlegroups .com>, "Axter" <te**@axter.com> wrote:

> Most STL implementations use reference counting for std::string. >
> Most major compilers like GNU, VC++ 6.0, and 7.x come with std::string
> that has reference counting in the implementation.
>
> Implementations that don't have reference counting are the

exception,
> and not the rule.

Last time I counted, there were 4 major independent implementations.
Half use a refcounted string and half use the sso (short string
optimization). Dinkumware and Metrowerks (current versions) are
the ones using sso.

-Howard


I believe there are more then 4 major independent implement, and

some versions allow you to turn off the reference counter. (See Scott
Meyers Effective STL)

However, most of the major compilers come with versions that use
reference counters.


Sorry, I counted wrong:

Dinkumware
gcc
Metrowerks
Rogue Wave
STLport

gcc and STLport have a somewhat intertwined heritage and I mistakenly

counted them as one.

Scott's ESTL counted 4, and diplomatically named them A - D. Note that this fine book is copyrighted 2001.


FYI:
He keeps an updated Errata, with last update 25 April 2005
See following link:
http://www.aristeia.com/BookErrata/e...ta_frames.html

I agree, it's a fine book.
Moreover, I consider his complete set of Effective C++, More C++, & STL
to be the best three C++ books I've ever read.
I recommend these three books over any other C++ books, to include
books by Stroustrup and Sutter.

Jul 23 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
8146
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
11
3621
by: Christopher Benson-Manica | last post by:
Let's say I have a std::string, and I want to replace all the ',' characters with " or ", i.e. "A,B,C" -> "A or B or C". Is the following the best way to do it? int idx; while(...
22
13211
by: Jason Heyes | last post by:
Does this function need to call eof after the while-loop to be correct? bool read_file(std::string name, std::string &s) { std::ifstream in(name.c_str()); if (!in.is_open()) return false; ...
19
6117
by: Erik Wikström | last post by:
First of all, forgive me if this is the wrong place to ask this question, if it's a stupid question (it's my second week with C++), or if this is answered some place else (I've searched but not...
8
9162
by: Patrick Kowalzick | last post by:
Dear NG, I would like to change the allocator of e.g. all std::strings, without changing my code. Is there a portable solution to achieve this? The only nice solution I can think of, would be...
6
11477
by: Nemok | last post by:
Hi, I am new to STD so I have some questions about std::string because I want use it in one of my projects instead of CString. 1. Is memory set dinamicaly (like CString), can I define for...
2
5471
by: FBergemann | last post by:
if i compile following sample: #include <iostream> #include <string> int main(int argc, char **argv) { std::string test = "hallo9811111z"; std::string::size_type ret;
84
15770
by: Peter Olcott | last post by:
Is there anyway of doing this besides making my own string from scratch? union AnyType { std::string String; double Number; };
11
2877
by: Jacek Dziedzic | last post by:
Hi! I need a routine like: std::string nth_word(const std::string &s, unsigned int n) { // return n-th word from the string, n is 0-based // if 's' contains too few words, return "" //...
0
7171
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7238
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6942
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5531
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4963
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3133
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1472
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
706
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
342
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.