Boost logo

Boost Users :

From: Thomas Costa (oohrah_at_[hidden])
Date: 2006-04-27 21:15:36


You usually don't hold the mutex guarding the state indicating
variables for a long time.
You usually hold it just enough to update the variables to indicate
the new state you have transitioned to.
That is you hold it while you pick up the request from the queue and
transfer it to local variables in the thread (or somehow mark it in
the queue) as being processed, then you release the lock and go off
and handle request. After you are done processing you grab the lock
again and update state or lock for new requests and then repeat. If
there are no requests then you block on condition while holding the
mutex and the blocking wait releases and reacquires the lock for you
when the condition is signaled. You will be holding the mutex lock
for very little time unless popping/marking queue elements is quite
expensive.
Also, it often a little more efficient to signal the condition
variable while not holding the mutex. It can avoid needless context
ping ponging.

On Apr 27, 2006, at 3:49 PM, Peter Mackay wrote:

> Hello,
>
> I'm trying to use the Boost Threads library to help me implement a
> background loading system for a game I'm writing.
>
> I want to enqueue load requests while the main game thread is
> running, and
> have a separate thread load the data and pass back the appropriate
> objects.
>
> Currently when I enqueue a new request, I notify a 'something to do'
> condition, which my loading thread waits on. The trouble is, when I
> notify
> it, the loader could be loading in the background, and has a lock
> on the
> mutex connected with the condition.
> Would anyone be able to offer me any advice on how to solve this?
>
> Many thanks,
> Peter
>
> P.S. Please find attached my current implementation.
>
>
> begin 666 loader.cpp
> M(VEN8VQU9&4@(FQO861E<BYH<' B#0H-"B-I;F-L=61E(#QB;V]S="]B:6YD
> M+FAP<#X-"@T*;&]A9&5R.CIL;V%D97(H*0T*.B!T:')E861?<VAO=6QD7V5X
> M:70H9F%L<V4I#0I[#0H)=&AR96%D+G)E<V5T*&YE=R!B;V]S=#HZ=&AR96%D
> M*&)O;W-T.CIB:6YD*"9L;V%D97(Z.G1H<F5A9%]P<F]C+"!T:&ES*2DI.PT*
> M?0T*#0IL;V%D97(Z.GYL;V%D97(H*0T*>PT*"2\O(%-I9VYA;"!T:&4@=&AR
> M96%D('1O(&5X:70N#0H)>PT*"0EB;V]S=#HZ;75T97_at_Z.G-C;W!E9%]L;V-K
> M(&QO8VLH;75T97_at_[hidden]*"0ET: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-"F-L87-S(&QO861E<B Z('!R:79A
> M=&4_at_8F]O<W0Z.FYO;F-O<'EA8FQE#0I[#0IP=6)L:6,Z#0H-"@EL;V%D97(H
> M*3L-"@EV:7)T=6%L('YL;V%D97(H*3L-"@T*<')I=F%T93H-"@T*"79O:60)
> M=&AR96%D7W!R;V,H*3L-"@T*"2\O($%C8V5S<VEB;&4_at_8GD@86YY('1H<F5A
> M9"X-"@EB;V]S=#HZ;75T97@)"6UU=&5X.PT*"6)O;W-T.CIC;VYD:71I;VX)
> M<V]M971H:6YG7W1O7V1O.PT*"6)O;VP)"0D)=&AR96%D7W-H;W5L9%]E>&ET
> M.PT*"7-T9#HZ<75E=64\:6YT/@D)<&5N9&EN9U]L;V%D<SL-"@T*"2\O($%C
> M8V5S<VEB;&4_at_8GD@=&AE(&UA:6X@=&AE860@;VYL>2X-"@EB;V]S=#HZ<VAA
> M<F5D7W!T<CQB;V]S=#HZ=&AR96%D/@ET:')E860[#0I].PT*#0HC96YD:68-
> !"@``
> `
> end
>
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net