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
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
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 !
I am stumped
OK. Here is what I am trying to do. I have the conditions set, and if the conditions are met, I want to randomly find a user id, and username from a table, and then store each of these names in the
Losing 'page' data
I have this code that allows me to update my database. But after updating, I lose the $_GET['page'] value in the browser. I need this value because it is passed when I go to other pages on the
help removing unwanted graphic
How do I remove the small elongated rectangular shape between the "Latest post" and "Archives" section at http://www.simonlindgren.com ?The code surrounding this place
Do not allow posting of whitespace
Currently the script below works if the user does not type a name/message, but if i create a whitespace with the spacebar, it will submit. How would I fix this?message page:<?php// Get the
function to return url
Hi,Can you guys help me giving a function that takes all the contents in a particular field and returns text and url seperately...For instance,Go:here is the linkwww.google.comit takes all the
Running a cron job through PHP/Apache?!
Hello all,I have a PHP script which makes a cron file from user input. When i try to run the cron job via system() the job doesn't run, I suspect it has something to do with permissions. Any ideas on
Query issues
I probably have this setup wrong well I know I do because nothing is showing up now. What I want is for it to show all the character names that are assigned to each user so yes than can be more than
Must be a string? Huh, what? HELP?
with the following script, I get Fatal error: Property name must be a string in /home/content/e/s/o/esone/html/test/123.php on line 24Anyone maybe know what is causing it?Code: <?phpfunction
Does design fit in FPGA ?
Hi all,I've made a large HCC-Design. Because of the program-size the compile process with the handel-c compiler takesvery long. Is there any possibility to find out whether the design fits into my
Renaming a file that a user uploads to site?
My site allows for registered users to upload images to the site under their own gallery. Currently it just leaves the file named as however the person has it named. But what I would like to do is