Boost logo

Boost :

From: helmut.zeisel_at_[hidden]
Date: 2001-10-11 10:36:18


--- In boost_at_y..., Darin Adler <darin_at_b...> wrote:

>
> The conventional solution to this is to create a object containing
the
> pointer to the function, and using a pointer to that as the
thread-specific
> data. The only issue then making sure that the object is deleted.
>

I am not the author of the threads library,
but using your hint a bug fix like the following should work:

@ -28,7 +28,10 @@

 typedef void (*once_callback)();
 }
-
+struct once_callback_wrapper
+{
+ once_callback cb;
+};
 extern "C" {

 static void key_init()
@@ -38,7 +41,7 @@

 static void do_once()
 {
- once_callback cb =
reinterpret_cast<once_callback>(pthread_getspecific(key)
);
+ once_callback cb =
reinterpret_cast<once_callback_wrapper*>(pthread_getspec
ific(key))->cb;
     (*cb)();
 }

@@ -85,7 +88,9 @@
        }
 #elif defined(BOOST_HAS_PTHREADS)
     pthread_once(&once, &key_init);
- pthread_setspecific(key, func);
+ once_callback_wrapper w;
+ w.cb=func;
+ pthread_setspecific(key, &w);
        pthread_once(&flag, do_once);
 #endif
 }

It compiles and the example

http://www.boost.org/libs/thread/doc/call_once.html

works.

Helmut


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk