473,888 Members | 1,340 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::string and pthread_mutex_l ock / pthread_mutex_l ock

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_l ock and
pthread_mutex_u nlock 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 2702
"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_l ock and
pthread_mutex_u nlock 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_l ock and
pthread_mutex_u nlock 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_l ock and
pthread_mutex_u nlock 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.
Implementatio ns 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_l ock and
pthread_mutex_u nlock 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_l ock and
> pthread_mutex_u nlock 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_l ock and
> > pthread_mutex_u nlock 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

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

Similar topics

10
8207
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 effectively impossible to forward declare std::string. (Yes I am aware that some libraries have a string_fwd.h header, but this is not portable.) That said, is there any real reason why I can't derive an otherwise empty
11
3681
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( (idx=str.find_first_of(',')) >= 0 ) { str.replace( idx, 1, "" ); str.insert( idx, " or " ); }
22
13350
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; char c; std::string str;
19
6201
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 found anything). Here's the problem, I have two sets of files, the name of a file contains a number which is unique for each set but it's possible (even probable) that two files in different sets have the same numbers. I want to store these...
8
9210
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 a namespace and another typedef to basic_string: namespace my_string {
6
11523
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 example string str1; as a class member and then add text to it. or do I have to specify it's length when defining? 2. How to convert from std::string to LPCSTR
2
5527
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
15960
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
2912
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 "" // 'words' are any sequences of non-whitespace characters // leading, trailing and multiple whitespace characters // should be ignored.
0
9961
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9802
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10780
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10439
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9597
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7991
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4642
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 we have to send another system
2
4248
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3252
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.