g_atomic_pointer*

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

g_atomic_pointer*

james morris-8
Hi,

I need to use an atomic pointer for my app. The documentation for the glib
atomic operations is not exactly helpful.

Anyway, I wrote a test program:

----8<----
/* atomic.c */
#include <glib.h>
int main()
{
    char* myptr = 0;
    char* str = "Hello";
    g_atomic_pointer_set(&myptr, str);
    char* p = g_atomic_pointer_get(&myptr);
    return 0;
}
----8<----

I had to use the output of

gcc `pkg-config --cflags glib-2.0` atomic.c -E

to understand what was meant to be used as the parameters. Anyway, the
output of this does not look at all atomic: (not that i'm any expert)

----8<----
...
# 4 "atomic.c" 2

int main()
{
    char* myptr = 0;
    char* str = "Hello";
    ((void) (*(&myptr) = (str)));
    char* p = ((gpointer)*(&myptr));
    return 0;
}
----8<----

This is on a x86_64, running Gentoo, glib-2.20.5.

I don't know if I've got the idea right for how these functions are
supposed to be used. Any ideas?

Cheers,
james.


_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Reply | Threaded
Open this post in threaded view
|

Re: g_atomic_pointer*

Brian Lavender-3
On Mon, Mar 01, 2010 at 10:35:22AM -0000, James Morris wrote:

> Hi,
>
> I need to use an atomic pointer for my app. The documentation for the glib
> atomic operations is not exactly helpful.
>
> Anyway, I wrote a test program:
>
> ----8<----
> /* atomic.c */
> #include <glib.h>
> int main()
> {
>     char* myptr = 0;
>     char* str = "Hello";
>     g_atomic_pointer_set(&myptr, str);
>     char* p = g_atomic_pointer_get(&myptr);
>     return 0;
> }
> ----8<----

You should write two or three threads competing for the same resource.
It is your typical dining philosopher problem.  An atomic operation
is used in place of a mutex (may actually use a mutex or a semaphore
underneath), so that access to a resource is synchronized.

I haven't quite figured out the resource that is being guarded here, but
it looks like access to a certain section of memory.

--
Brian Lavender
http://www.brie.com/brian/

"About 3 million computers get sold every year in China, but people don't
pay for the software. Someday they will, though. As long as they are going
to steal it, we want them to steal ours. They'll get sort of addicted, and
then we'll somehow figure out how to collect sometime in the next decade."

-- Bill Gates (Microsoft) 1998
_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Reply | Threaded
Open this post in threaded view
|

Re: g_atomic_pointer*

Tim-Philipp Müller
In reply to this post by james morris-8
On Mon, 2010-03-01 at 10:35 +0000, James Morris wrote:

Hi,

> I need to use an atomic pointer for my app. The documentation for the glib
> atomic operations is not exactly helpful.
>
> Anyway, I wrote a test program:
>     g_atomic_pointer_set(&myptr, str);
>     char* p = g_atomic_pointer_get(&myptr);
>
> (...) to understand what was meant to be used as the parameters. Anyway,
> the output of this does not look at all atomic: (not that i'm any expert)
>
>     ((void) (*(&myptr) = (str)));
>     char* p = ((gpointer)*(&myptr));
>
> This is on a x86_64, running Gentoo, glib-2.20.5.
>
> I don't know if I've got the idea right for how these functions are
> supposed to be used. Any ideas?

This should be ok in your case, it may expand to something else on other
platforms though.

Cheers
 -Tim


_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list