[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: pthreads help needed to diagnose #98866



On Mon, May 28, 2001 at 01:24:35AM -0400, Matt Zimmerman wrote:
> On Mon, May 28, 2001 at 12:23:28AM -0400, Matt Zimmerman wrote:
> 
> > WORKS1 and WORKS2 demonstrate two ways I have found to keep the problem from
> > happening.  WORKS1 has the thread wait on a semaphore before executing.
> > WORKS2 overrides the Thread::Start method with its own version, using exactly
> > the same code.  Both of these methods cause the problem to go away.  Where is
> > the bug?
> 
> Another note (already sent to the bug address): if I change thread.cpp to
> create the thread with PTHREAD_CREATE_DETACHED instead of
> PTHREAD_CREATE_JOINABLE, and rebuild the shared library, it works.  Of course,
> this breaks other functionality in the library, such as the ability to
> terminate a thread (uses pthread_join).  Why should there be a difference
> between using PTHREAD_CREATE_DETACHED, and calling pthread_detach?
> 
For your WORKS1 situation, I found it ran ps ux twice under the same PID
and 20 times under unique PIDs. I'm not sure this is what you want for
threads?

The attached diff for your threadtest.cc, will cause it to run 22 times
for the same PID. From my understanding of the docs, you should
implement derived(Thread)::Run and call that, unless you need to 'specify
the running priority of a newly created thread relative to the "parent"
thread which is the thread that is executing when the constructor is
called.'

The patch 'works for me':
--- threadtest.cc       Mon May 28 02:07:35 2001
+++ threadtest_mine.cc  Mon May 28 02:07:48 2001
@@ -24,7 +24,10 @@
 public:
   ThreadTest(Semaphore *sem): Thread(sem) { };
   ~ThreadTest() { Terminate(); };
-  void Run() { };
+  void Run()
+  {
+    system("ps ux | grep threadtest | grep -v grep");
+  };
   void Final() { delete this; };

@@ -56,7 +59,7 @@
     // For the semaphore case, this posts to the semaphore and lets
     // the thread continue.  For the non-semaphore case, this creates
     // the thread.  See code for Thread::Start below.
-    thread->Start();
+    thread->Run();
   }
   sleep(5); // Allow threads to exit
   system("ps ux | grep threadtest | grep -v grep");
  

-- 
Gordon Sadler



Reply to: