Topic: bugs in stl strings / threads?


Author: jpotter@falcon.lhup.edu (John Potter)
Date: 1999/05/11
Raw View
Gerhard Kutzelnigg <gerhard@uni-x.net> wrote:

: string s;
: if (s.length ())
:   cout << "Here I am and the content of the string is: (" << s << ")\n";

: Sometimes the program comes to a part of code like this and it enters
: the line with the cout statement. But then the string is _empty_. The
: output is (), so that I can really be sure there is no space character
: that I could miss.

: My question is: How can the program go to the "then" part of the if
: statement and print nothing, since I am checking the string length just
: before?

How about:

#include <iostream>
#include <string>
int main () {
    string s(5, '\0');
    cout << "s.length() = " << s.length() << " s = ("
            << s << ")\n";
    }

Strings can contain nul characters which do not take space when output
to most screens.

John
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: James.Kanze@dresdner-bank.com
Date: 1999/05/11
Raw View
In article <373448CD.B57692C4@uni-x.net>,
  Gerhard Kutzelnigg <gerhard@uni-x.net> wrote:

> I am using a lot of strings in a multithreaded application. But
> unfortunately I am discovering errors in the string class in a new,
> different way. For example:

> string s;
> if (s.length ())
>   cout << "Here I am and the content of the string is: (" << s <<
")\n";

> Sometimes the program comes to a part of code like this and it enters
> the line with the cout statement. But then the string is _empty_. The
> output is (), so that I can really be sure there is no space character
> that I could miss.

Are you accessing s from a different thread.  If so, there is nothing
surprising in this behavior; the other thread runs between the
s.length() and the operator<<, and changes the contents of s.

--
James Kanze                         mailto:
James.Kanze@dresdner-bank.com
Conseils en informatique orient   e objet/
                        Beratung in objekt orientierter
Datenverarbeitung
Ziegelh   ttenweg 17a, 60598 Frankfurt, Germany  Tel. +49 (069) 63 19 86
27


--== Sent via Deja.com http://www.deja.com/ ==--
---Share what you know. Learn what you don't.---


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1999/05/11
Raw View
Note: Threads are not part of the C++ standard. But as I'm not sure
if the appropriate group would be gnu.g++.help or
comp.programming.threads, I leave it here for the moment.
(Note to moderators: If you know which group is more appropriate,
please add crosspost and followup to the appropriate group. Thanks.)

[ moderator's note: We do not modify the list of newsgroups in a
  submitted article. If the article is not appropriate for this
  newsgroup, we simply return it with an explanation. The sender
  can then decide where else to post it. If you want an article
  to appear in other newsgroups, you must decide where to send
  it.  We tried other approaches in the past, and this set of
  procedures seems to work best. -sdc ]

Gerhard Kutzelnigg wrote:
>
> Hi,
>
> I am using the strings class from the standard template library (latest
> libstdc++2.8.1 / gcc 2.8.1). I noticed that I have to define -D_PTHREADS
> to make the STL thread-safe. Now after having compiled the new gcc and
> libstdc++ my program doesn't crash because of allocation errors in the
> string class when using threads.
>
> I am using a lot of strings in a multithreaded application. But
> unfortunately I am discovering errors in the string class in a new,
> different way. For example:
>
> string s;
> if (s.length ())
>   cout << "Here I am and the content of the string is: (" << s << ")\n";
>
> Sometimes the program comes to a part of code like this and it enters
> the line with the cout statement. But then the string is _empty_. The
> output is (), so that I can really be sure there is no space character
> that I could miss.
>
> My question is: How can the program go to the "then" part of the if
> statement and print nothing, since I am checking the string length just
> before?
>
> Could it be that the STL string class is still buggy even if I set the
> define for multithreading (-D_PTHREADS)?
>
> Is there a new version of the STL somewhere out there? I've been looking
>
> at gnu.org, but this is the latest.
>
> Does anybody have ideas?
>
> Thank you very much!

There are many possible reasons:

- One of the reasons already given by other posters

- The string class (or operator<<) you are using is buggy.
  I don't think so for the stdc++ 2.8.1 string class, though.

- The string class you are using is not thread safe (I don't know if
  this is the case with libstdc++ 2.8.1, maybe upgrading to egcs
  helps; alternatively get the newest SGI STL - possibly you need
  an adapted version for g++ 2.8.1 - the SGI STL does contain a
  thread safe string).

- Your code is not thread safe. For example, if you have the following
  code:
  Thread 1:
    ...
    s="";
    ...
  Thread 2:
    ...
    if (s.length())
      cout << "(" << s << ")" << endl;
    ...
  then you can get into the following situation:

  Thread 2 executes with s being non-empty. The if (s.length())
  executes and the condition evaluates to true. But before the
  cout is executed, a context switch occurs, and Thread 1 executes
  the s=""; so when the context switches again to Thread 2, s is
  empty, and "()" is printed.
  Also note that even for a thread safe class, concurrent writing
  to the same object, or writing and reading from the same object
  at the same time are not possible (classes which allow this are
  called "monitored"). So generally you have to protect your write
  accesses anyway.


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Gerhard Kutzelnigg <gerhard@uni-x.net>
Date: 1999/05/10
Raw View
Hi,

I am using the strings class from the standard template library (latest
libstdc++2.8.1 / gcc 2.8.1). I noticed that I have to define -D_PTHREADS

to make the STL thread-safe. Now after having compiled the new gcc and
libstdc++ my program doesn't crash because of allocation errors in the
string class when using threads.

I am using a lot of strings in a multithreaded application. But
unfortunately I am discovering errors in the string class in a new,
different way. For example:

string s;
if (s.length ())
  cout << "Here I am and the content of the string is: (" << s << ")\n";

Sometimes the program comes to a part of code like this and it enters
the line with the cout statement. But then the string is _empty_. The
output is (), so that I can really be sure there is no space character
that I could miss.

My question is: How can the program go to the "then" part of the if
statement and print nothing, since I am checking the string length just
before?

Could it be that the STL string class is still buggy even if I set the
define for multithreading (-D_PTHREADS)?

Is there a new version of the STL somewhere out there? I've been looking

at gnu.org, but this is the latest.

Does anybody have ideas?

Thank you very much!

greetings
gerry
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Yannick de Kercadio <kercadio@limsi.fr>
Date: 1999/05/10
Raw View
Gerhard Kutzelnigg wrote:
>
> string s;
> if (s.length ())
>   cout << "Here I am and the content of the string is: (" << s << ")\n";
>
> Sometimes the program comes to a part of code like this and it enters
> the line with the cout statement. But then the string is _empty_. The
> output is (), so that I can really be sure there is no space character
> that I could miss.
>
> My question is: How can the program go to the "then" part of the if
> statement and print nothing, since I am checking the string length just
> before?
>

It is very likely that you don't see the contents of the string because
of a synchronization problem. Does your program really displays the
closing parenthesis?

Try the following:

string s;
if(s.length() > 0)
  cout << "Here I am and the content of the string is: (" << s <<
    ")  size = " << s.length() << endl;

endl will insert a '\n', and will also *flush* the stream. This is
probably the problem.


--
Yannick de Kercadio
kercadio@limsi.fr


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]