#Atomic operations on none atomic types.

35 messages · Page 1 of 1 (latest)

pure pawn
#

Hi, I need a hand operating atomically on a none atomic int.
I cant use C++20 and our boost version is too old, so std::atomic_ref and boost::atomic_ref can't be used.
I can't really rely on a mutex as a mutex is too slow. I'm not very comfortable using inline assembly. So any suggestions would be great

lusty geyserBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.

opal spoke
#

okay but why dont you make the int atomic?

#

std::atomic<int>

#

doesnt need c++20

#

otherwise maybe you could have an atomic bool and only modify the ints value if that atomic bool is true? but then why not use std::atomic<int>

kindred galleon
#

you can also use intrinsics and assume that pure write or read are atomic on most platforms (the later is risky ofc)

hollow junco
#

I can't really rely on a mutex as a mutex is too slow.

#

what

bleak knoll
hollow junco
#

Are you stupid or something
quit with the aggression

#

I'm just curious why OP says cannot use atomic int and in the same time says that the reason he/she won't use mutex is that it is slow

#

so...

#

no atomic int, no mutex?

outer inlet
#

buy a faster computer xD

hollow junco
#

When you don't want to have the overhead of context switching you typically use atomic variables

#

whats the problem with atomic int

mossy wasp
#

C11 has stdatomic.h

#

never used it but worth looking into

outer inlet
ruby mica
#

Use a custom binary semaphore with spinlock

pure pawn
#

The issue is is that there are two contending treads, a slow task and a fast task, the fast take does 1 "iteration" in 200us and MUST not go over, so is fairly speedy, Unfortunately i don't have control of the hardware or of the types used,
The function currently doing the job is a built-in OS function but i'm looking to make an abstraction layer for it.

#

I could look at changing them all to be atomic<int> but i would have to change a fair few files and theres all the validation that goes with that

pure pawn
ruby mica
#

If this is for work I'd recommend the team to invest in a spike/investigation/experiment before committing to any particular approach.

ruby mica
#

Have the rubric for comparisons and metrics prepared before starting the experiment.

pure pawn
#

I'll have a look at the atomics and maybe a mutex approach too, i need to benchmark the times really.
We're doing a simulation build and a "normal" build, for the sim build timing requirements we don't have to care about, in the "normal" build we do, but the RTOS's mutex might be faster than the windows version.

opal spoke
#

other question is an atomic really required ? for what do you need it ? maybe the simplest solution is to decide afterwards what values too take

#

or let one thread wait till the other one is finished

ruby mica
opal spoke
#

i meant with .join()

kindred galleon
#

yes thats mutex based

opal spoke
lusty geyserBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.