Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #6632 > unrolled thread
| Started by | Moshbear dot Net <moshbear.net@gmail.com> |
|---|---|
| First post | 2011-06-11 15:08 -0700 |
| Last post | 2011-06-18 06:41 -0500 |
| Articles | 7 — 4 participants |
Back to article view | Back to comp.lang.c++
instance counter Moshbear dot Net <moshbear.net@gmail.com> - 2011-06-11 15:08 -0700
Re: instance counter "Chris M. Thomasson" <cristom@charter.net> - 2011-06-11 15:43 -0700
Re: instance counter Moshbear dot Net <moshbear.net@gmail.com> - 2011-06-11 15:58 -0700
Re: instance counter Pete Becker <pete@versatilecoding.com> - 2011-06-11 14:01 -1000
Re: instance counter "Chris M. Thomasson" <cristom@charter.net> - 2011-06-11 19:52 -0700
Re: instance counter Moshbear dot Net <moshbear.net@gmail.com> - 2011-06-12 03:17 -0700
Re: instance counter Paavo Helde <myfirstname@osa.pri.ee> - 2011-06-18 06:41 -0500
| From | Moshbear dot Net <moshbear.net@gmail.com> |
|---|---|
| Date | 2011-06-11 15:08 -0700 |
| Subject | instance counter |
| Message-ID | <ccf39133-e576-456d-9ee2-66f9bebdf8aa@d14g2000yqb.googlegroups.com> |
Given
class Fred {
public:
Fred() {
ctr = ++xctr;
}
...
private:
unsigned ctr;
static unsigned xctr; // initialized via boost::thread::run_once
...
};
,
what would be the best way to make the increment-and-read of xctr
atomic so that it is thread-safe?
I'm using xctr to track instance number of class so that I can use
operator<(const Fred&) to compare by instance-creation time.
[toc] | [next] | [standalone]
| From | "Chris M. Thomasson" <cristom@charter.net> |
|---|---|
| Date | 2011-06-11 15:43 -0700 |
| Message-ID | <ecSIp.1414$z%.403@newsfe12.iad> |
| In reply to | #6632 |
"Moshbear dot Net" <moshbear.net@gmail.com> wrote in message
news:ccf39133-e576-456d-9ee2-66f9bebdf8aa@d14g2000yqb.googlegroups.com...
> Given
> class Fred {
> public:
> Fred() {
> ctr = ++xctr;
> }
> ...
> private:
> unsigned ctr;
> static unsigned xctr; // initialized via boost::thread::run_once
> ...
> };
> ,
> what would be the best way to make the increment-and-read of xctr
> atomic so that it is thread-safe?
Perhaps an atomic fetch-and-add operation, or a statically initialized
PThread mutex.
> I'm using xctr to track instance number of class so that I can use
> operator<(const Fred&) to compare by instance-creation time.
What happens when that "version counter" wraps around?
;^)
[toc] | [prev] | [next] | [standalone]
| From | Moshbear dot Net <moshbear.net@gmail.com> |
|---|---|
| Date | 2011-06-11 15:58 -0700 |
| Message-ID | <80324efc-d3cb-44c9-be7e-837ead801ec6@m4g2000yqk.googlegroups.com> |
| In reply to | #6634 |
On Jun 11, 6:43 pm, "Chris M. Thomasson" <cris...@charter.net> wrote:
> "Moshbear dot Net" <moshbear....@gmail.com> wrote in messagenews:ccf39133-e576-456d-9ee2-66f9bebdf8aa@d14g2000yqb.googlegroups.com...
>
> > Given
> > class Fred {
> > public:
> > Fred() {
> > ctr = ++xctr;
> > }
> > ...
> > private:
> > unsigned ctr;
> > static unsigned xctr; // initialized via boost::thread::run_once
> > ...
> > };
> > ,
> > what would be the best way to make the increment-and-read of xctr
> > atomic so that it is thread-safe?
>
> Perhaps an atomic fetch-and-add operation, or a statically initialized
> PThread mutex.
Is there a portable way of doing
atomic {
++xctr;
ctr = xctr;
}
Also, it looks like I'm going to have to search the boost docs for
mutex-locked statically-scoped counters.
However, I believe that somebody has already done this and can offer
guidance as to which boost class is
preferred (or c++0x/tr1 class, given that this is c.l.c++ :)
>
> > I'm using xctr to track instance number of class so that I can use
> > operator<(const Fred&) to compare by instance-creation time.
>
> What happens when that "version counter" wraps around?
I'd be using ::uint64_t in actual code. This was just to show the
concept.
[toc] | [prev] | [next] | [standalone]
| From | Pete Becker <pete@versatilecoding.com> |
|---|---|
| Date | 2011-06-11 14:01 -1000 |
| Message-ID | <2011061114015396183-pete@versatilecodingcom> |
| In reply to | #6637 |
On 2011-06-11 12:58:12 -1000, Moshbear dot Net said:
>
> Is there a portable way of doing
> atomic {
> ++xctr;
> ctr = xctr;
> }
>
std::atomic<int> xctr = 0;
and
ctr = ++xctr;
That's with C++11; your library might already have it.
--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)
[toc] | [prev] | [next] | [standalone]
| From | "Chris M. Thomasson" <cristom@charter.net> |
|---|---|
| Date | 2011-06-11 19:52 -0700 |
| Message-ID | <YRVIp.1372$UT4.998@newsfe05.iad> |
| In reply to | #6639 |
"Pete Becker" <pete@versatilecoding.com> wrote in message
news:2011061114015396183-pete@versatilecodingcom...
> On 2011-06-11 12:58:12 -1000, Moshbear dot Net said:
>
>>
>> Is there a portable way of doing
>> atomic {
>> ++xctr;
>> ctr = xctr;
>> }
>>
>
> std::atomic<int> xctr = 0;
[...]
Want it NOW!
http://www.stdthread.co.uk
:^)
[toc] | [prev] | [next] | [standalone]
| From | Moshbear dot Net <moshbear.net@gmail.com> |
|---|---|
| Date | 2011-06-12 03:17 -0700 |
| Message-ID | <e05a0698-2305-479b-bf2f-3659ddbd86a4@b1g2000yql.googlegroups.com> |
| In reply to | #6641 |
On Jun 11, 10:52 pm, "Chris M. Thomasson" <cris...@charter.net> wrote:
> "Pete Becker" <p...@versatilecoding.com> wrote in message
>
> news:2011061114015396183-pete@versatilecodingcom...
>
> > On 2011-06-11 12:58:12 -1000, Moshbear dot Net said:
>
> >> Is there a portable way of doing
> >> atomic {
> >> ++xctr;
> >> ctr = xctr;
> >> }
>
> > std::atomic<int> xctr = 0;
>
> [...]
>
> Want it NOW!
>
> http://www.stdthread.co.uk
>
Any F/OSS versions? boost::interprocess::detail has atomic operations,
but it looks a bit ugly to use in end-user code.
And <atomic> is c++0x (existence in: g++ 4.5 and higher, likely in VC
1x (not in VC10)).
[toc] | [prev] | [next] | [standalone]
| From | Paavo Helde <myfirstname@osa.pri.ee> |
|---|---|
| Date | 2011-06-18 06:41 -0500 |
| Message-ID | <Xns9F08958908623myfirstnameosapriee@216.196.109.131> |
| In reply to | #6651 |
Moshbear dot Net <moshbear.net@gmail.com> wrote in news:e05a0698-2305-479b-
bf2f-3659ddbd86a4@b1g2000yql.googlegroups.com:
>> >> Is there a portable way of doing
>> >> atomic {
>> >> ++xctr;
>> >> ctr = xctr;
>> >> }
[...]
> Any F/OSS versions? boost::interprocess::detail has atomic operations,
> but it looks a bit ugly to use in end-user code.
In C++ one encapsulates such things in a little class which can then be
(re)used easily in the "end-user code".
hth
Paavo
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c++
csiph-web