[ace-users] [ace-user] ACE_Thread_Manager::wait

Jeong Ho Lee friend25kr at gmail.com
Wed Jul 11 21:48:36 CDT 2007


Dear, all.

Please help me to improve my understanding on ACE_Thread_Manager.

   ACE VERSION: 5.5.4

   HOST MACHINE and OPERATING SYSTEM:
       Host Machine: Intel Centrio Duo 2GHz meory 1.5GB
       OS: Windows Professional SP2

   TARGET MACHINE and OPERATING SYSTEM: Same with Host Machine and OS

   THE $ACE_ROOT/ace/config.h FILE

        #define ACE_HAS_STANDARD_CPP_LIBRARY 1
        #define ACE_HAS_MFC 1
        #define ACE_NO_INLINE
        #include "ace/config-win32.h"

   AREA/CLASS/EXAMPLE AFFECTED:
       ACE_Thread_Manager

   DOES THE PROBLEM AFFECT:
       EXECUTION

   SYNOPSIS:
      ACE_Thread_Manager::wait function behavior

   DESCRIPTION:
     What I understand on the fuction of ACE_Thread_Manager:wait is that
     it blocks until there are no more threads running in this thread manager
     or timeout expires.
     However, it seems not working in my simple program.

    I spawn 10 threads in my main function.
    Each thread has its own identifier (number) assigned when the
thread is spawned.
    With that identifier, the thread prints out its id on the screen.

   In the main function, just after finishing spawning all threads, I
call the function
   ACE_Thread_Manager::wait with the timeout value of 60 seconds.

   What I expect is that my process will not return untile all my
spawned threads
   are finish or 60 second-timeout expires. (I assume that 60 seconds are quite
   enough time for all the spawned threads finish their assigned job.)

   However, my process returnes before that.
   Please help me to understand how it happneds.

   However it is not working.
   My program is listed below.


int main(int argc, char* argv[])
{
	ACE::init();
	CThread *pthread[10];
	for(int i = 0; i < 10; i++)
	{
		pthread[i] = new CThread();
		pthread[i]->start(i);
	}

	ACE_Time_Value timeout(600);
	ACE_Thread_Manager::instance()->wait(&timeout, false, true);

	return 0;
}

CThread::CThread(void)
{
}

CThread::~CThread(void)
{
}

void* CThread::run_svc(void* arg)
{
	Thread_Args *thread_args = ACE_static_cast(Thread_Args*, arg);

	ACE_Thread_Manager *tm = ACE_Thread_Manager::instance();
	ACE_thread_t me = ACE_OS::thr_self();
	
	m.acquire();
	for(int i = 0; i < thread_args->id_; i++)
	{
		cout << thread_args->id_ << "(" << i << ")" << " is running" << endl;
	}
	m.release();

	delete thread_args;
	return 0;
}

void CThread::start(unsigned short id)
{
	Thread_Args *thread_args = new Thread_Args(id, this);
	m.acquire();
	cout << id << " is about to start" << endl;
	m.release();
	if(ACE_Thread_Manager::instance()->spawn((ACE_THR_FUNC)CThread::run_svc,
ACE_static_cast(void*, thread_args), THR_DETACHED | THR_SCOPE_PROCESS)
== -1)
		return;
}

Thanks.
- Jeong Ho.



More information about the Ace-users mailing list