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...
Did you know?Explore Trending and Topic pages for more stories like this.
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
14
Other forums
File upload issues
Hi Guys,
Can anyone see any issues with this code:
Code: $setImage= 'productimages/' . dat
Sharing PHP Sessions Across Domains
I am in the process of writing a script to share a php session across various domains I have.
The
turning an array into a string then poping it into a db
Hi im just wondering how you get a requested array into a single string then pop it into the db so f
DB2 for z/OS, LUW, iSeries
Hello,
In this forum a lot of threads related to other members of the DB2 family than DB2 for z/OS
Multi-user card game
I'm writing a batch of games and such. Lottery is finished, Poker,blackjack,etc are next. They will
Help with PHP Email Script
Hi guys,
I'm a newbie to php and need help with the script below:
At the moment when I
Turning Data into URLs?
This may seem very basic to you, but I'm having a hard time figuring out how to do this since I don'
Why does this file not return file names that start with numbers?
This file returns a list of filenames to help populate a drop down in my form. For some reason it ig
search query in mysql in php problem
my search query wont work, i know, that my codes are correct.please help
Code: [Select]&l
Form errors in an array
I'm processing a form and putting the errors in an array. empty($errors) doesn't seem to do the tric