Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ MT-multithreading and servers » Intentionally inconstistent use of "Atomic" in Mt.h?
Intentionally inconstistent use of "Atomic" in Mt.h? [message #14165] Sat, 16 February 2008 13:19 Go to next message
Werner is currently offline  Werner
Messages: 234
Registered: May 2006
Location: Cologne / Germany
Experienced Member
All the Atomic... functions in Mt.h (be it WIN32 or POSIX) return int - but shouldn't it be Atomic?

The type of Atomic depends:

MT & POSIX: _Atomic_word -> int
MT & WIN32: LONG -> long
ST & POSIX: int
ST & WIN32: int

Indeed the called WIN32 functions Interlocked... return long. So, to avoid implicit casts it might be preferable to adapt the return type of the Atomic... functions. If I then assign an Atomic to an int, I am seeing what I am doing.

Werner
Re: Intentionally inconstistent use of "Atomic" in Mt.h? [message #14166 is a reply to message #14165] Sat, 16 February 2008 17:03 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Werner wrote on Sat, 16 February 2008 07:19

All the Atomic... functions in Mt.h (be it WIN32 or POSIX) return int - but shouldn't it be Atomic?

The type of Atomic depends:

MT & POSIX: _Atomic_word -> int
MT & WIN32: LONG -> long
ST & POSIX: int
ST & WIN32: int

Indeed the called WIN32 functions Interlocked... return long. So, to avoid implicit casts it might be preferable to adapt the return type of the Atomic... functions. If I then assign an Atomic to an int, I am seeing what I am doing.

Werner



Well, but you should not directly assign Atomic to int, you should rather use AtomicRead...

Mirek
Re: Intentionally inconstistent use of "Atomic" in Mt.h? [message #14167 is a reply to message #14166] Sat, 16 February 2008 17:36 Go to previous messageGo to next message
Werner is currently offline  Werner
Messages: 234
Registered: May 2006
Location: Cologne / Germany
Experienced Member
luzr wrote on Sat, 16 February 2008 17:03

Well, but you should not directly assign Atomic to int, you should rather use AtomicRead...


A misunderstanding? If so, sorry for expressing the issue insufficiently!

I'm talking about the all the "Atomic..." functions in Mt.h, i. e.

AtomicDec,
AtomicInc,
AtomicRead,
AtomicWrite,
AtomicXAdd.

They all have an "Atomic&" as parameter but treat it with an "int" and/or return an "int".

If - and this is the case when using the MSC - "Atomic" is a "long" in disguise, namely typedef'd, implicit casts are inevitable.

I would prefer explicit casts or at least casts which are obvious to me. That is why I suggest to replace the "int" arguments and/or return types with "Atomics".

Werner

P. S.:
"AtomicDec", "AtomicInc", and "AtomicXAdd" are "global" functions. If you discourage their usage, you should document that.

W.
Re: Intentionally inconstistent use of "Atomic" in Mt.h? [message #14239 is a reply to message #14167] Wed, 20 February 2008 19:39 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Werner wrote on Sat, 16 February 2008 11:36

luzr wrote on Sat, 16 February 2008 17:03

Well, but you should not directly assign Atomic to int, you should rather use AtomicRead...


A misunderstanding? If so, sorry for expressing the issue insufficiently!

I'm talking about the all the "Atomic..." functions in Mt.h, i. e.

AtomicDec,
AtomicInc,
AtomicRead,
AtomicWrite,
AtomicXAdd.

They all have an "Atomic&" as parameter but treat it with an "int" and/or return an "int".



First of all I think this is miniscule problem... not worthy of such detailed examination.

Anyway, notice this: The set of functions is complete. You never need to assign int directly to Atomic or back.

Imagine some platform that e.g. lacks interlocked operations. In that case you would need to simulate Atomic using mutex:

struct Atomic { Mutex lock; int value; }

There are no direct conversions to/from Atomic now... But using AtomicRead/Write and others, returning int, everything works.

Sure, things are not ideal, perhaps Atomic should rather be class from the beginning and all operations its methods....

Mirek
Re: Intentionally inconstistent use of "Atomic" in Mt.h? [message #14243 is a reply to message #14239] Wed, 20 February 2008 21:47 Go to previous message
Werner is currently offline  Werner
Messages: 234
Registered: May 2006
Location: Cologne / Germany
Experienced Member
luzr wrote on Wed, 20 February 2008 19:39

First of all I think this is miniscule problem... not worthy of such detailed examination.


... but then again I have seen - in other cases and under worse conditions - that minuscule inconstistencies led to major problems ...

But never mind, in this specific case you are most likely right, so thank you very much for still answering anyway.

I mainly posted this question out of curiosity, just to learn why you coded it that way.

Actually I bumped into this issue when analyzing Mt.h, Mt.cpp, CoWork.h, and CoWork.cpp. Unfortunately multithreading with Ultimate++ is poorly documented so far, and I am the guy who wants to know exactly what's going on when using someone else's code.

Werner
Previous Topic: Distant file info
Next Topic: Checking socket connection after send.
Goto Forum:
  


Current Time: Fri Mar 29 01:49:15 CET 2024

Total time taken to generate the page: 0.01546 seconds