Re: g_atomic_pointer*



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



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