|
Boost : |
Subject: Re: [boost] [object_pool]too slow?
From: é±å®è (qbowater_at_[hidden])
Date: 2009-03-10 06:49:25
boost::object_pool is not thread-safe.
2009/3/10 <jon_zhou_at_[hidden]>:
>
> Dear boost members,
>
> anyony can check the code below? the way that I am using boost/pool is correct?
>
> it is too slow in my "linux 2.6+Intel(R) Core(TM)2 Duo CPU Â Â E6850 Â @ 3.00GHz" pc
>
> thanks!
>
>
> *********
> #include "boost/pool/object_pool.hpp"
> #include <sys/time.h>
> #include <pthread.h>
> #include <asm/atomic.h>
> #include <stdlib.h>
>
>
>
>
> class Rational
> {
> public:
> Â Â Rational(int a = 0,int b = 1):n(a),d(b)
> Â Â {
> Â Â }
>
>
> private:
> Â Â int n;
> Â Â int d;
>
> };
>
> atomic_t gv;
> boost::object_pool<Rational> pool;
>
> void *thread_function(void *arg)
> {
>
> Â struct timeval tpstart,tpend;
> Â atomic_t *v=(atomic_t *)arg;
> Â Rational *array[1000];
> Â // Start timing here
> Â gettimeofday(&tpstart,NULL);
>
>
> Â Â for (int j = 0; j < 50000; j++)
> Â Â {
> Â Â Â Â for (int i = 0; i < 1000; i++)
> Â Â Â Â {
> Â Â Â Â Â Â array[i] = pool.construct(i);
>
> Â Â Â Â }
> Â Â Â Â for (int i = 0; i < 1000; i++)
> Â Â Â Â {
> Â Â Â Â Â Â pool.destroy(array[i]);
> Â Â Â Â }
> Â Â }
> Â Â gettimeofday(&tpend,NULL);
> Â Â int timeuse = (tpend.tv_sec-tpstart.tv_sec)*1000000 + (tpend.tv_usec-tpstart.tv_usec);
> Â Â printf("time= %d usec\n",timeuse);
> Â Â atomic_add(timeuse, v);
> }
>
>
> int main(int argc,char **argv)
> {
> Â Â if (argc != 2)
> Â Â {
> Â Â Â Â perror("usuage:Mempool numberOfThreads\n");
> Â Â Â Â exit(-1);
> Â Â }
> Â Â /*
> Â Â * initialize atomic_t
> Â Â */
> Â Â atomic_set(&gv, 0);
> Â Â char *end;
> Â Â int numThreads = strtol(argv[1],&end,10);
> Â Â pthread_t a_thread[numThreads];
>
>
> Â Â void *thread_result;
> Â Â int res;
> Â Â for (int i=0; i < numThreads;i++)
> Â Â {
> Â Â Â Â res = pthread_create(&a_thread[i], NULL, thread_function, (void *)&gv);
> Â Â }
>
> Â Â for (int i=0; i < numThreads;i++)
> Â Â {
> Â Â Â Â res = pthread_join(a_thread[i], &thread_result);
> Â Â }
>
> Â Â printf("\nTotal time= %d usec\n",gv);
> }
>
> Regards,
> zhou rui
>
>
> _______________________________________________
> Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
>
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk