InterlockedIncrement?
-
- wxWorld Domination!
- Posts: 1339
- Joined: Wed Aug 03, 2005 8:10 am
- Location: BANGALORE, INDIA
- Contact:
-
- Filthy Rich wx Solver
- Posts: 235
- Joined: Sun Oct 10, 2004 2:53 am
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.
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.
-
- wxWorld Domination!
- Posts: 1339
- Joined: Wed Aug 03, 2005 8:10 am
- Location: BANGALORE, INDIA
- Contact:
-
- Knows some wx things
- Posts: 41
- Joined: Wed Oct 27, 2004 6:06 pm
- Location: Western NY
- Contact:
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.
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.
-
- wxWorld Domination!
- Posts: 1339
- Joined: Wed Aug 03, 2005 8:10 am
- Location: BANGALORE, INDIA
- Contact:
I am looking for single statement atomic increment and decrement.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...)
But how to use them with wxWidgets.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.
-
- Earned some good credits
- Posts: 109
- Joined: Fri Sep 03, 2004 9:30 am
- Location: Brescia, Italy
I assume you are using threads.priyank_bolia wrote: I am looking for single statement atomic increment and decrement.
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;
}
}
Sandro Sigala - Kynosoft, Brescia
-
- wxWorld Domination!
- Posts: 1339
- Joined: Wed Aug 03, 2005 8:10 am
- Location: BANGALORE, INDIA
- Contact:
I go with ddaeschl, also for such a simple thing, just to increment a variable like:
that much code is ..............
Code: Select all
a++;
- Ryan Norton
- wxWorld Domination!
- Posts: 1319
- Joined: Mon Aug 30, 2004 6:01 pm
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.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.
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]
-
- wxWorld Domination!
- Posts: 1339
- Joined: Wed Aug 03, 2005 8:10 am
- Location: BANGALORE, INDIA
- Contact:
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/
http://www.alexonlinux.com/multithreade ... -variables
You should also read the post:
http://www.alexonlinux.com/how-inherita ... rk-in-cpp/