cstdatomic (c++0x std::atomic) / g++ 4.4


Posted on 16th Feb 2014 07:03 pm by admin

Hello,

I'm trying to use cstdatomic (std::atomic in the upcoming c++0x standard) in g++ 4.4, but it doesn't seems to work as expected and I would like to known if I missed something...

My goal in the code below is just to test differents ways of concurrency (mutex vs atomic) : 4 threads increments counters in 3 ways.

1 - with std::mutex : works perfectly but slightly slow (lock / unlock)
2 - with g++ atomics "builtins" : works perfectly
3 - with std::atomic : doesn't works.


here is the code :

#include
#include
#include

int cnt_mutex = 0;
int cnt_builtins = 0;
std::atomic cnt_stdatomic = {0};
std::mutex smutex;

/* thread code */
void thrcode(int thrid)
{
/* counter local to thread */
int cnt_local;
for (cnt_local = 0; cnt_local < 10000000; cnt_local++)
{
/* c++0x fetchadd -> FAIL */
cnt_stdatomic.fetch_add(1);

/* g++ builtin fetchadd -> OK */
__sync_fetch_and_add(&cnt_builtins,1);

/* scoped std::mutex : OK */
{
std::unique_lock lockit(smutex);
cnt_mutex++;
};
};
printf("thread(%d) : cnt_local %dn",thrid,cnt_local);
};


int main() {
/* thread init */
std::thread m1(thrcode,1),m2(thrcode,2),m3(thrcode,3),m4(thrcode,4);

/* wait all thread */
m1.join(); m2.join(); m3.join(); m4.join();

/* display counters */
printf("end : cnt_mutex %d, cnt_builtins %d, cnt_stdatomic %dn",
cnt_mutex,cnt_builtins,(int)cnt_stdatomic);

};


compile with : g++ -std=c++0x mt.cpp -O3 -pthread && date && ./a.out && date

it give :
dimanche 4 octobre 2009, 19:10:28 (UTC+0200) (french time...)
thread(1) : cnt_local 10000000
thread(3) : cnt_local 10000000
thread(4) : cnt_local 10000000
thread(2) : cnt_local 10000000
end : cnt_mutex 40000000, cnt_builtins 40000000, cnt_stdatomic 39990400
dimanche 4 octobre 2009, 19:10:46 (UTC+0200)

cnt_stdatomic counter is incorrect... Someone could tell me where i'm wrong, did i miss something in code ?

(I'm using g++ version 4.4.1 Ubuntu 4.4.1-4ubuntu6)

Thanks !

No comments posted yet

Your Answer:

Login to answer
268 Like 14 Dislike
Previous forums Next forums
Other forums

please, need help coding this voting for your favorite car polling system.
hi, my code needs a lot of work, but i want to be able to let users vote on their favorite car, upda

How to secure my login data to my database?
Hi there,

It may be a stupid question but I don't know how to secure my login data to my data

a multi dimensional array with for each
hi, I have been asked to write an array, I have Zone 2,3,4,5,6,7,8 each zone has 3 sections of weigh

Mail sending is slow on server
Why mail is taking time to be send on apache-linux server

reading xml faster than DB call?
I am trying to optimize a high traffic website, and I enabled a feature where there are three photos

How to assign JS screen.width to ASP.Net variable
I've got this ASP.net routine that gets the cookie value of the screen width, but now I need to chan

fire away
i launched about 4 months ago as a lagit co. i have 2 paying clients, 1probono, 1 side progect, and

$variable = $variable
Hello

Sorry if this is a really simple question but I have run out of ideas. Is there a reall

please hep to get values from a table row
can anyone tel me how I can select and get data from one row by pressing an Edit button in that row

Mail Script Problems
I have created a PHP mail script. I am having a problem though. In $body i am trying to put two va

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS



Play Free Quiz and Win Cash