mkmojo / googletest

Automatically exported from code.google.com/p/googletest
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

Need a real implementation of ThreadLocal and Mutex such that gtest can be used in multi-threaded tests #148

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Use gtest on a true multi-threaded platform to which ThreadLocal was ported.
2. Call EXPECT_EQ in a non-main thread.
3. UnitTestImpl::GetTestPartResultReporterForCurrentThread() accesses
UnitTestImpl::per_thread_test_part_result_reported from a thread different
from the one in which it was initialized.

What is the expected output? What do you see instead?

Core dumps because
UnitTestImpl::GetTestPartResultReporterForCurrentThread() access
ThreadLocal UnitTestImpl::per_thread_test_part_result_reported on a thread
different from the one in which it was initialized.

What version of the product are you using? On what operating system?

Google Test 1.3.0
gcc 4.1.1, pthread, STL
Linux 2.6.18

Please provide any additional information below.

I may well have somehow botched the port of ThreadLocal to our own
pthread-based system, but this seems independent of my own incompetence
(that is, it seems wrong on any multi-threaded platform). I had good luck
with this change in gtest.cc and implementing a bool have() check in
ThreadLocal.

// Returns the test part result reporter for the current thread.
TestPartResultReporterInterface*
UnitTestImpl::GetTestPartResultReporterForCurrentThread() {
  if (per_thread_test_part_result_reporter_.have()) {
    return per_thread_test_part_result_reporter_.get();
  }
  return GetGlobalTestPartResultReporter();
}

Original issue reported on code.google.com by covercl...@diag.com on 14 May 2009 at 10:21

GoogleCodeExporter commented 9 years ago
ThreadLocal is supposed to be initialized in the main thread only.  Its get() 
method 
will return a different value for each thread that calls it.  The value is 
created 
on-demand.  If you call get() twice in the same thread, you get the same value. 
 If 
you call get() from different threads, you get different values.

We use gtest in many multi-threaded tests where we have a full implementation 
of 
ThreadLocal, and it works fine.

I'm changing the issue title to reflect the real problem.

Original comment by zhanyong...@gmail.com on 15 May 2009 at 5:30

GoogleCodeExporter commented 9 years ago
Albert Wong wrote:

> It's not a 100% match, but you might look in the chromium code base under
> the "base/" directory for "lock.h" and "thread_local.h" as a starting point.
>  I think you'll find analogues for Mutex, MutexLock (called Lock and
> AutoLock), and ThreadLocal.  I don't know about GetThreadCount...

Original comment by zhanyong...@gmail.com on 8 Oct 2009 at 11:55

GoogleCodeExporter commented 9 years ago
I've uploaded an implementation for pthreads to review at: 
http://codereview.appspot.com/129067/show.

Original comment by FazekasM...@gmail.com on 11 Oct 2009 at 5:55

GoogleCodeExporter commented 9 years ago
Fixed in the trunk for platforms with pthread, by Miklos and Vlad.

Original comment by w...@google.com on 6 Mar 2010 at 5:56