|
Boost : |
Subject: Re: [boost] [Boost.utility]
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2010-01-27 17:03:27
AMDG
Andrew Chinkoff wrote:
> Ok, I see. I would try to explain.
>
> Let us look at pseudo code:
>
> *** BEGIN PSEUDO CODE ***
> static int* my_int = 0;
> static Mutex mutex;
>
> void CreateInt()
> {
> if(!my_int)
> {
> Locker locker(mutex);
> if(!my_int)
> my_int = new int(100);
> }
> }
>
> void thread_func()
> {
> boost::call_once(CreateInt, flag);
> printf("[%d]", *my_int);
> }
>
> void main()
> {
> th1 = create_thread(thread_func);
> th2 = create_thread(thread_func);
> while(1)
> {
> // Prints "[0][100]", or "[100][0]", or "[100][100]" - hm... undefined!
> }
> }
> *** END OF PSEUDO CODE ***
>
There is no race in this code.
It will always print [100][100].
In fact, the code would work equally well if you said:
void CreateInt() {
my_int = new int(100);
}
In Christ,
Steven Watanabe
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk