void    PhysicsServerExample::exitPhysics()
{
		for (int i=0;i<MAX_MOTION_NUM_THREADS;i++)
		{
			m_args[i].m_cs->lock();
			m_args[i].m_cs->setSharedParam(0,eRequestTerminateMotion);
			m_args[i].m_cs->unlock();
		}
		int numActiveThreads = MAX_MOTION_NUM_THREADS;

		while (numActiveThreads)
                {
			int arg0,arg1;
                        if (m_threadSupport->isTaskCompleted(&arg0,&arg1,0))
                        {
                                numActiveThreads--;
                                printf("numActiveThreads = %d\n",numActiveThreads);

                        } else
                        {
							b3Clock::usleep(1000);
                        }
                };

		printf("stopping threads\n");

		delete m_threadSupport;   
		m_threadSupport = 0;

		//m_physicsServer.resetDynamicsWorld();
		
}
    virtual void    exitPhysics()
    {
        b3Printf("exitPhysics, stopping threads");
       	bool blockingWait =false;
       	 int arg0,arg1;
       	   
        args.m_cs->lock();
        //terminate all threads
        args.m_cs->setSharedParam(1,MAGIC_RESET_NUMBER);
        args.m_cs->unlock();
       	   
        if (blockingWait)
        {
            for (int i=0;i<m_numThreads;i++)
            {
                m_threadSupport->waitForResponse(&arg0,&arg1);
                printf("finished waiting for response: %d %d\n", arg0,arg1);
            }
        } else
        {
            int numActiveThreads = m_numThreads;
            while (numActiveThreads)
            {
                if (m_threadSupport->isTaskCompleted(&arg0,&arg1,0))
                {
                    numActiveThreads--;
                    printf("numActiveThreads = %d\n",numActiveThreads);

                } else
                {
    //				printf("polling..");
                }
            };
        }
        
        delete m_threadSupport;
        
        b3Printf("Threads stopped");
        for (int i=0;i<m_jobs.size();i++)
        {
         delete m_jobs[i];
        }
        m_jobs.clear();
        	
    }