Hi Alexei,
I believe that the mutex is the better approach, after seeing some of its
uses for similar needs in other projects. Also, I think that the static
counter scenario should work, but hasn't that got to be within a thread
lock? If not it wont be thread safe like most other defines. Also, you may
go ahead with "#define INC". '(' and ')' are not needed as the #defines are
subjected to a literal replacement.
Regards,
Senaka
On Thu, Apr 10, 2008 at 4:50 PM, Xiao-Feng Li <[EMAIL PROTECTED]> wrote:
> On Thu, Apr 10, 2008 at 6:08 PM, Alexei Fedotov
> <[EMAIL PROTECTED]> wrote:
> > Thanks, Alexei, Xiao Feng,
> > This was helpful.
> >
> > BTW, as far as I understand using mutexes, I need to protect reads as
> > well. If I use CAS for adding a new element, the list is always
> > consistent for reading from other threads without any protection.
> >
>
> Well, it depends on if you are using a read-write lock. Read-write
> lock permits concurrent readings.
>
> Thanks,
> xiaofeng
>
> >
> > On Wed, Apr 9, 2008 at 6:00 PM, Xiao-Feng Li <[EMAIL PROTECTED]>
> wrote:
> > > Right.
> > >
> > > To avoid the CAS loop, you can use Mutex.
> > >
> > > mutex_lock();
> > > //critical section;
> > > mutex_unlock();
> > >
> > > Thanks,
> > > xiaofeng
> > >
> > >
> > >
> > > On Wed, Apr 9, 2008 at 8:35 PM, Aleksey Shipilev
> > > <[EMAIL PROTECTED]> wrote:
> > > > IMHO here:
> > > >
> > > > 1. Using CAS infers using busy-loop, e.g. "we are trying until
> > > > operation succeed". You might tolerate spinning there by using
> PAUSE
> > > > instruction or some other way.
> > > > 2. How about "static int counter = 0; #define INC() counter++;"?
> It
> > > > will be done in runtime though.
> > > >
> > > > Thanks,
> > > > Aleksey.
> > > >
> > > >
> > > >
> > > > On Wed, Apr 9, 2008 at 3:57 PM, Alexei Fedotov <
> [EMAIL PROTECTED]> wrote:
> > > > > Hello folks,
> > > > > Could you please help?
> > > > >
> > > > > 1. Is it possible to add to a thread safe linked list using
> CAS
> > > > > without a busy loop?
> > > > >
> > > > > void add_site(LogSite* log_site) {
> > > > > LogSite** p_site_head = (LogSite**)
> &get_logger()->log_site;
> > > > > LogSite* old_value = *p_site_head;
> > > > > do {
> > > > > log_site->next = (LogSite*) old_value;
> > > > > (LogSite*) old_value = (LogSite*)
> apr_atomic_casptr((volatile
> > > > > void **) p_site_head,
> > > > > (void*) log_site, log_site->next);
> > > > > } while (old_value != log_site->next); // busy loop
> > > > > }
> > > > >
> > > > > 2. Is it possible to create an auto-increment macro within a
> > > > > compilation unit using C preprocessor?
> > > > >
> > > > > #define INC() ...
> > > > >
> > > > > INC() // == 1
> > > > > INC() // == 2
> > > > > ...
> > > > >
> > > > > Thank you for sharing your expertise.
> > > > > --
> > > > > With best regards,
> > > > > Alexei
> > > > >
> > > >
> > >
> > >
> > >
> > > --
> > > http://xiao-feng.blogspot.com
> > >
> >
> >
> >
> > --
> > With best regards,
> > Alexei
> >
>
>
>
> --
> http://xiao-feng.blogspot.com
>