Re: C++ Threads, what's the status quo?
Le Chaud Lapin wrote:
I showed in response to same problem put forth in a different post that
this is a problem more related to optimization than multi-threading.
IOW, the problem would be present whether the application were
multi-threaded or single-threaded. Yes, it affects multi-threading,
but there are many "anomalies" in C++ that affect many things. An
orthogonal approach to ameliorating the language might be better.
There are strata of problem domains that must be understood to
facilitate multi-threaded programming in C++. I do not think this
particular one should be grouped with the others.
The problem is not optimization; the problem is the C++ specification
does not prohibit the standards conformant compiler to optimize your
multithreaded code into oblivion.
I posted the following example code in this very forum 8 years ago:
http://groups.google.ca/group/comp.lang.c++.moderated/browse_thread/thread/867a5476d747c391/59cb90e0ef99ae22?lnk=st&q=jeffk%40jdkoftinoff.com+c%2B%2B+threads&rnum=2#59cb90e0ef99ae22
This code uses a standards conforming c++ library and if you run
thread1() and thread2() in different threads you will get crashes
and/or memory leaks. More recent compilers have thread-safe
std::string, but nowhere in the c++ standard is there any guarantee
that std::string C.O.W. reference counting is guaranteed to be thread
safe. You are free to say "On compiler version 4.1.x on platform
aaa-bbb-ccc with the options pthread-stdc++ this code is thread safe"
but you cannot say that this code is "thread safe c++ code"
#include <string>
void f( const std::string &s )
{
std::string my_copy(s);
// do anything or nothing with my_copy here
}
std::string my_label( "testing" );
void thread1()
{
while(1)
{
f( my_label );
}
}
void thread2()
{
while(1)
{
f( my_label );
}
}
--jeffk++
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]