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 14 2650
"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.
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?
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)
"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?
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.
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)
"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.
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.
"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.
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.
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
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.
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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(...
|
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;
...
|
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...
|
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...
| |
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...
|
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;
|
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;
};
|
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 ""
//...
|
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,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
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,...
|
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...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |