Re: GThreads and GThreadPool help for a dummy

On Thu, 22 Jul 2010 13:27:29 +0200
Øystein Schønning-Johansen <oystein gnubg org> wrote:
> I'm not really experienced when it comes to threading, and GThreads is
> the thing I want to use, since (I assume) it's quite portable.
> I have a function that returns a gfloat and I call the function
> sequentially and accumulate the total.
> Like this:
> /* My sequential pseudo code */
> data_t data[N_DATAPOINTS];
> gfloat tot_err = 0.0f;
> guint i;
> fill_the_data_array( data );
> for ( i = 0; i < N_DATAPOINTS; i++ )
>     tot_err += calc_error( &data[i] );
> /* End of my sequential pseudo code */
> I have the impression that this should be rather simple to thread
> for-loop. Since addition is commutative, I may not even need a mutex
> for the tot_err variable.

Commutativity (indifference to the order of evaluation) is irrelevant
to whether you would need a mutex for access to a shared variable (you
would if access is shared to a variable which is written to as well as
read) but there are ways of arriving at a sum which would not require
shared access: in particular g_thread_join() is a synchronisation point
which will cause memory/caches between processors to become visible and
also return a value for summing.

The glib documentation on threading is not all that good and does not go
into questions of memory synchronisation.  If you are beginning with
threads you might do better to read up on pthreads, which GThread
mimics and of which it implements a subset.  On unix-like systems
GThreads is mostly just a wrapper for pthreads.


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