Hi,
I have some problem with ACE_Barrier...
Can you help me please?
Thanks.
Hubert
ACE VERSION: 6.0.4
HOST MACHINE and OPERATING SYSTEM:
Windows 7
TARGET MACHINE and OPERATING SYSTEM, if different from HOST:
COMPILER NAME AND VERSION (AND PATCHLEVEL):
CONTENTS OF $ACE_ROOT/ace/config.h [if you use a link to a platform-
specific file, simply state which one]:
#define ACE_HAS_ICMP_SUPPORT 1
#define __ACE_INLINE__ 1
#include "ace/config-win32.h"
CONTENTS OF $ACE_ROOT/include/makeinclude/platform_macros.GNU [if you
use a link to a platform-specific file, simply state which one
(unless this isn't used in this case, e.g., with Microsoft Visual
C++)]:
CONTENTS OF $ACE_ROOT/bin/MakeProjectCreator/config/default.features
(used by MPC when you generate your own makefiles):
LEVEL OF URGENCY (LOW, MEDIUM, or HIGH):
HIGH
AREA/CLASS/EXAMPLE AFFECTED:
[What example failed? What module failed to compile?]
Please, see below for the source code.
DOES THE PROBLEM AFFECT:
COMPILATION?
LINKING?
On Unix systems, did you run make realclean first?
EXECUTION?
OTHER (please specify)?
[Please indicate whether ACE, your application, or both are affected.]
EXECUTION
SYNOPSIS:
[Brief description of the problem]
Program crashes.
DESCRIPTION:
[Detailed description of problem. Don't just say "<blah>
doesn't work, here's a fix," explain what your program does
to get to the <blah> state. ]
I'm trying to use a barrier with threads having arguments.
The program does not always work. Sometimes the program crashes.
REPEAT BY:
[What you did to get the error; include test program or session
transcript if at all possible. ]
SAMPLE FIX/WORKAROUND:
[If available ]
Source code:
//
http://oss.org.cn/ossdocs/ace_tao/ACE-5.4+TAO-1.4+CIAO-0.4/ACE/tutorials/Chap_4/ex07.html
#include "ace/Thread.h"
#include "ace/OS.h"
#include "ace/Synch.h"
#include "ace/Log_Msg.h"
static int number=0;
static int seed=0;
class Args
{
public:
Args(ACE_Barrier *barrier, long i)
: barrier_(barrier)
, i_(i)
{
}
ACE_Barrier *barrier_;
long i_;
};
static void * worker(void *arguments)
{
Args *arg= (Args*)arguments;
ACE_DEBUG((LM_DEBUG,"Thread (%t) Created to do some work: %d\n", arg->i_));
::number++;
//Work
//ACE_OS::sleep(ACE_OS::rand()%2);
//Exiting now
ACE_DEBUG((LM_DEBUG,
"\tThread (%t) Done! \n\tThe number is now: %d\n",number));
//Let the barrier know we are done.
ACE_DEBUG((LM_DEBUG,"Thread (%t) is exiting \n"));
arg->barrier_->wait();
return 0;
}
int main(int argc, char *argv[])
{
if(argc<2)
{
ACE_DEBUG((LM_DEBUG,"Usage: <program_name> <number of threads>\n"));
ACE_OS::exit(1);
}
int n_threads=ACE_OS::atoi(argv[1]);
//ACE_DEBUG((LM_DEBUG,"Preparing to spawn %d threads",n_threads));
//Setup the random number generator
ACE_OS::srand(::seed);
//Setup arguments for threads
ACE_Barrier barrier(n_threads);
Args arg1(&barrier, 1);
Args arg2(&barrier, 2);
Args arg3(&barrier, 3);
Args arg4(&barrier, 4);
Args vect[] = {arg1, arg2, arg3, arg4};
//Spawn off n_threads number of threads
for(int i=0; i<n_threads; i++){ if(ACE_Thread::spawn((ACE_THR_FUNC)worker,(void*)&vect[i],THR_DETACHED|THR_NEW_LWP)==-1)
ACE_DEBUG((LM_DEBUG,"Error in spawning thread\n"));
}
//Wait for all the other threads to let the main thread
// know that they are done using hte barrier
barrier.wait();
return 0;
}