Re: multi thread app and often variable accessing



On 6/8/07, Tomasz Jankowski <tomcioj gmail com > wrote:
>
> Hi!
>
> I'm working on small multi thread application based on Gobject. In one of
> my
> objects I have integer variable, which determine current object's status.
> The problem is, that I need to read it's status really often, so it will
> be
> to expensive to lock and unlock mutex all the time. Can I use there atomic
> operations provided by GLib instead of mutex locking and unlocking system?
> I
> mean, if g_atomic_get and g_atomic_set would not occur any problems, when
> I
> will try to access the same variable from two thread at the same time?
>
> --
> Cya!
> Tom

On 6/8/07, Jonathan Winterflood <jonathan winterflood gmail com> wrote:
Hi,

As far as I understand, g_atomic_int_get/set will do the job
just fine, as long as you're sure the only part that needs to be
atomic is the getting and setting.
However, if the hardware you are running on does not provide an atomic way
of getting/setting an int, (eg: int is an int32 and the hardware is 8-bit),
you will end up locking and unlocking mutexes anyways (but most systems
nowadays are 32bit+ anyways)

Jonathan


Also, the hardware must have something like the Pentium CMPXCHG8B or
it's newer cousins to do this "efficiently". I'm not sure exactly how
it's done in glib, but normally, if such instructions dont exist, you
need to issue standard MOV instructions with a bus-lock prefix, which
is slow.

-Jim

PS, please don't top-post.



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]