InterlockedIncrement?

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
priyank_bolia
wxWorld Domination!
wxWorld Domination!
Posts: 1339
Joined: Wed Aug 03, 2005 8:10 am
Location: BANGALORE, INDIA
Contact:

InterlockedIncrement?

Post by priyank_bolia » Tue Sep 06, 2005 10:28 am

Any such thing in wxWidgets?

SnakeChomp
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 235
Joined: Sun Oct 10, 2004 2:53 am
Contact:

Post by SnakeChomp » Tue Sep 06, 2005 3:27 pm

What are you expecting wxWidgets to be? A port of the win32 api that works on all platforms?

Even though thats basically what it is, there is absolutely no way you can expect an arbitrary win32 function to appear in a cross platform toolkit. If you want to use it on windonws go right ahead. If not, wrap up your counters and provide mutators which increment them using mutex locks.

priyank_bolia
wxWorld Domination!
wxWorld Domination!
Posts: 1339
Joined: Wed Aug 03, 2005 8:10 am
Location: BANGALORE, INDIA
Contact:

Post by priyank_bolia » Tue Sep 06, 2005 3:46 pm

Hi SnakeChomp, why are you so angry, I always worked on windows and will continue to be. Also my applications are being developed for the windows only. But I just wanted to know that, is there something that do that, or have to write my own function everytime.

ddaeschl
Knows some wx things
Knows some wx things
Posts: 41
Joined: Wed Oct 27, 2004 6:06 pm
Location: Western NY
Contact:

Post by ddaeschl » Wed Sep 07, 2005 12:30 pm

Hello,

SnakeChomp, all that he probably wanted to know was is there a wxWidgets function for atomic increment and decrement, which is not windows specific by any means, he just wasn't asking the question in a non windows specific manner.

Atomic operations are defined at the processor level, so the functions would have to be written once for each processor type that your application will be running on (x86, POWER, SPARC, etc...)

If your project is open source you should take a look at mono's atomic.h

http://svn.myrealbox.com/viewcvs/trunk/ ... iew=markup

They have source there for multiple interlocked increment operations on many different platforms.

priyank_bolia
wxWorld Domination!
wxWorld Domination!
Posts: 1339
Joined: Wed Aug 03, 2005 8:10 am
Location: BANGALORE, INDIA
Contact:

Post by priyank_bolia » Wed Sep 07, 2005 2:27 pm

SnakeChomp, all that he probably wanted to know was is there a wxWidgets function for atomic increment and decrement, which is not windows specific by any means, he just wasn't asking the question in a non windows specific manner.

Atomic operations are defined at the processor level, so the functions would have to be written once for each processor type that your application will be running on (x86, POWER, SPARC, etc...)
I am looking for single statement atomic increment and decrement.
If your project is open source you should take a look at mono's atomic.h

http://svn.myrealbox.com/viewcvs/trunk/ ... iew=markup

They have source there for multiple interlocked increment operations on many different platforms.
But how to use them with wxWidgets.

ssigala
Earned some good credits
Earned some good credits
Posts: 109
Joined: Fri Sep 03, 2004 9:30 am
Location: Brescia, Italy

Post by ssigala » Wed Sep 07, 2005 4:08 pm

priyank_bolia wrote: I am looking for single statement atomic increment and decrement.
I assume you are using threads.

The current solution I use is the class wxCriticalSectionLocker:

Code: Select all

static wxCriticalSection gs_critical;
int my_shared_obj;
// ...
void MyClass::Mumble()
{
    if (i_have_to_increment_object) {
       wxCriticalSectionLocker locker(gs_critical);
       ++my_shared_obj;
    }

    if (i_have_to_decrement_object) {
       wxCriticalSectionLocker locker(gs_critical);
       --my_shared_obj;
    }
}
Or do you really need to call the processor intrinsic instruction to increment/decrement?
Sandro Sigala - Kynosoft, Brescia

ddaeschl
Knows some wx things
Knows some wx things
Posts: 41
Joined: Wed Oct 27, 2004 6:06 pm
Location: Western NY
Contact:

Post by ddaeschl » Wed Sep 07, 2005 7:39 pm

The only thing I would have against the critical section solution is that it could be a lot more expensive than an atomic increment.

This may not seem like a big deal, but if he is using the operation for something like reference counting of many objects, the extra time can add up very quickly.

priyank_bolia
wxWorld Domination!
wxWorld Domination!
Posts: 1339
Joined: Wed Aug 03, 2005 8:10 am
Location: BANGALORE, INDIA
Contact:

Post by priyank_bolia » Thu Sep 08, 2005 6:43 am

I go with ddaeschl, also for such a simple thing, just to increment a variable like:

Code: Select all

a++;
that much code is ..............

User avatar
Ryan Norton
Moderator
Moderator
Posts: 1319
Joined: Mon Aug 30, 2004 6:01 pm

Post by Ryan Norton » Mon Sep 12, 2005 2:03 am

ddaeschl wrote:The only thing I would have against the critical section solution is that it could be a lot more expensive than an atomic increment.

This may not seem like a big deal, but if he is using the operation for something like reference counting of many objects, the extra time can add up very quickly.
Yes - what he wants is an atomic operation, or spinlock. On MSW it's the InterlockedXXX methods and on linux/unix it's the kernel spin_lock (including OSX, as that's what CF uses for reference counting). Mono's atomic.h is an OK solution but it's processor-specific (mostly assembly) and missing some things.

Other than that you'll have to use a critical section.

There are disadvantages to both - atomic operations steal cpu time while critical sections are slower.
[Mostly retired moderator, still check in to clean up some stuff]

priyank_bolia
wxWorld Domination!
wxWorld Domination!
Posts: 1339
Joined: Wed Aug 03, 2005 8:10 am
Location: BANGALORE, INDIA
Contact:

Post by priyank_bolia » Thu May 21, 2009 11:00 am

Old Post, but today I found some interesting blog, which had the answer: __sync_fetch_and_add

http://www.alexonlinux.com/multithreade ... -variables

You should also read the post:
http://www.alexonlinux.com/how-inherita ... rk-in-cpp/

Kraymer
Earned a small fee
Earned a small fee
Posts: 23
Joined: Wed Feb 04, 2009 1:32 pm
Location: Germany
Contact:

Post by Kraymer » Thu May 21, 2009 4:55 pm

I think there's an AtomicIncement method in the development branch and trunk of wxwidgets, but I'm not sure. Also, I don't know it's exact name but it shouldn't be too hard to find it out. AFAIK there's no such thing in the stable versions (<2.9)

Post Reply