« first day (181 days earlier)      last day (4769 days later) » 

3:00 PM
Can't remember the details, but a lot of articles seemed to pop up a while ago, first about how Java was almost at the point of having some kind of consensus on getting closures, and then all of a sudden "oh, well, not this time"
If C++ proves that design by committee can work, then Java proves that it doesn't always work.
 
Als
@ChrisBecke: Do you think its the next popular in thing?
 
@jalf Seems familiar... C++ concepts anyone?
 
@DavidRodríguezdribeas true
 
@jalf I seem to remember something like that as well. My impression was that Oracle wanted to just cut the debate and get some improvements out now. Roughly the same idea as removing Concepts from C++11 -- nobody wanted to, but realistically it just about had to be done.
@DavidRodríguezdribeas Great (or some sort of) minds think alike, I guess.
 
@Als I do hope not.
 
3:02 PM
I'm probably biased, but I think the concepts thing had good reasons behind it though. They were pushing into unknown territory, and there were a lot of legitimate concerns about what effects it'd have. Whereas lambdas are... well, they could steal the spec from C#, and they'd more or less be home free
 
@jalf Personally, I kind of doubt it. The two languages haven't diverged enough for one to have a real advantage over the other, but just enough that they can't quite feed off each other that easily either.
 
@DavidRodríguezdribeas still a valid point though
 
@ChrisBecke I don't think efficiency is Java's greatest problem.
 
@jalf It is certainly true that concepts were pushing into much closer to virgin territory though...
 
@JerryCoffin yeah, but would there be any unique problems with lambdas in Java over lambsdas in .NET? I can't think of any unique obstacles that might require Java's implementation to be fundamentally different
 
3:04 PM
@FredOverflow Actually, I'd go so far as to say that efficiency is almost the least of its problems.
 
Seems like it was much more of a conservative "OOP has always been good enough for US" attitude that prevented any of this fancy FP stuff
2
 
the next language I would choose to learn would look like C, with classes, using objective-c dynamic binding with c++ syntax, using overrideable allocators so that the language syntax is agnostic to wether the implementation uses a heap, reference counted, garbage collected, or smart objects for memory management.
 
@ChrisBecke mine would be Haskell :)
3
 
Mine will be C++ :)
2
 
Well, or Objective C++, if I end up in possession of a Mac
 
3:06 PM
it would still be a prodecural language at its heart, as I belive that functional languages don't work because they dont map to how people naturally think
but it definately would have lambdas and co-routines.
 
anyway, I've kind of lost interest in imperative languages for educational purposes. Whether you call it C#, C++, Ruby or anything else, I don't really see much point in learning them, other than for practical reasons, if I end up needing it for a project
 
@ChrisBecke Doesn't work. GC removes deterministic destruction, which makes RAII essentially unworkable. That's exactly why the solutions trying to graft RAII onto Java and C# are so horrible and kludgy.
 
@JerryCoffin there's no fundamental reason why you couldn't have both in a language though.
 
@JerryCoffin Work with objective-c for a while before making that claim
 
@ChrisBecke I have -- it's a lot of the basis for the claim.
 
3:08 PM
objective-c was the source of my dissatisfaction with c++
 
@JerryCoffin I don't see what's so horrible about using and Java's special try (other than naming).
 
It could work in several ways. Either split the language so that some types are GC'ed and others have deterministic destruction (C# does that, although they could take it a lot further), or just separate destruction from memory release. Let the GC take care of reclaiming memory nondeterministically, but destroy the objects deterministically before then with RAII
@MartinhoFernandes the fact that it's necessary
 
@MartinhoFernandes What's horrible is that they don't centralize the RAII. Instead of the class designer being able to write it so it manages its own resources, they require the everybody who uses the class still has to deal with the resource management.
 
obj-c has an ugly syntax, and a high runtime overhead that is perhaps unavoidable, but it does have an elegant way of dealing with allocating and initializing objects and their descendants.
 
In C++, you can just create a variable, and when it goes out of scope, it will clean itself up. I don't have to think about, or know about, how to clean up after it
But in C#/Java, you have to specifically say "and now clean up this variable I've been using"
 
3:10 PM
which removes the need for having to have special keywords like new and delete.
 
@jalf What about allocating stuff with new in C++?
 
the thing I really liked about C++ when I first saw it
was coming from an assembler, and basic, and pascal background
 
@jalf This gives another scenario that's about equally nasty: it creates zombie objects that have been destroyed, but still exist.
 
where magic key words were the order of the day
was the idea that the language had a runtime, that was written exactly the same way as user written functions were, for performing actions as basic as malloc().
 
@JerryCoffin but no doubt you could come up with ways to solve that. Ensure that no references to the object exist, for example
I'm just saying that if you designed your language for it from scratch, it could support both RAII and GC smoothly
 
3:12 PM
@JerryCoffin Worse even: I have not really seen at the implementations of the classes that are manageable, but I assume that it will be similar to C# IDisposable, where the class requires a good amount of code to handle the resource (when it is dispose, and when it is not disposed and the finalizer is called, in case that the user forgot to dispose it), and then after making the class more complex it still requires user code to be aware and handle that in a using/try block
 
but yeah, there are a lot of issues you'd have to consider, and you definitely wouldn't be able to bolt it on like C# tries to do
 
so, thinking that c++ was little more than c with classes (I was uneducated remember) I fell in love with the idea that languages should have as few keywords as possible, and that outtside of those, all language features should be user implementable.
which is why I find new, and delete, frustrating.
 
@ChrisBecke You're gonna love LISP.
 
In a program that uses malloc(), I could undefine free(), and replace it with release() and copyref() or something.
and get an effective reference counted heap, that very little c code would need to be changed for.
 
@ChrisBecke yeah, whatever our disagreements about C++, its memory management functions (new/delete) definitely have a few issues
 
3:14 PM
In objective C its that simple to remove the dealloc method from a classes meta methods
or implement retain and release as new methods
 
On the lambda's issue, I don't think that they could have just grabbed C# lambdas and added that to Java as it is. The problem being that their different approaches to "closures" as of now differ greatly (with Java's being more cumbersome, a little more sane)
 
@jalf Maybe -- Modula III (for one example) certainly handled it a lot more cleanly than most since, but even there it got kludgy in places. Go seems (to me) to have a bit of the same problem. Even Smalltalk has some fairly nasty areas to deal with when you try to put it to serious use.
 
I find new, and delete, so frustrating as a result
I can't delete delete
I can't change their parameters
 
@DavidRodríguezdribeas I thought they didn't have closures yet?
 
and I can't add "retain" and "release" as new class metemethods.
 
3:16 PM
Well, not really closures, but an anonymous class is not that different from a cumbersome lambda
 
ah right
 
@ChrisBecke Of course you can:
class HasNoDelete
{
    void operator delete(void*) = delete;
};
 
if c++ fitted my mental definition of "Elegant", then new, and delte, would have been methods added to classes using some generic syntax, that allowed different forms to be defined, and allowed them to not be defined at all.
 
@jalf They don't, but they do have a number of proposals, and seem to have decided on a rough idea of what they're going to do, and it's built on what's already there (anonymous, local classes).
 
c++0x sure
 
3:17 PM
@DavidRodríguezdribeas But that's not really a closure: it doesn't capture any variables.
 
@MartinhoFernandes Right, it copies constants instead :)
 
You can capture, but they are required to be final
 
@FredOverflow it's a starting point though
 
@DavidRodríguezdribeas That doesn't count!
 
oh, that was meant to be @MartinhoFernandes
 
3:18 PM
Do they? If you capture a final MutableObject x = new MutableObject();, are you not allowed to change the object?
 
but I still cant:
class HasRetainSemantics {
void operator delete(void*) = delete;
thisclass* operator retain(thisclass*);
void operator release();
};
 
The reference being final, but the object possibly changing?
 
@DavidRodríguezdribeas You don't capture objects with closures, you capture variables.
 
@FredOverflow Uhm... what is the huge difference there, from say, capturing by value a pointer?
 
I vaguely could, using templates, but then the result would be so clouded in template specific syntax that the result wouldn't be usable, in the same way that being able to
MyClass* scndRef = retain frstRef;
looks, to me, quite elegant
 
3:20 PM
I guess this drills down to the actual definition of variable, is the reference or the referred object the variable? Intuitively the object, in reality the reference, I guess
 
@DavidRodríguezdribeas Can't do it with primitive types. Can't do x = new SpecialMutableObject().
 
@DavidRodríguezdribeas You can't reseat the pointer variable.
 
@FredOverflow again, what would be the difference from [=](){ std::cout << x; } ?
 
Sorry, but as I understand the term, C++0x does not have closures ;) All it offers is some syntactic sugar for defining functors in-place.
 
(given an appropriate x in scope...) Or we can get into a deeper discussion and say that C++ lambdas are closures sometimes but aren't some other times... (I don't really want to go that way...)
@FredOverflow Good, that interests me (sincerely, this is not ironic): What would be a definition of a captured variable, and a closure?
 
3:24 PM
int x = 1;
Action inc = () => { x++; } // I'm not at home with C++ syntax for closures :(
inc(); inc(); inc();
print(x); // prints 4;
That closure captures the x variable and changes it.
The outer scope can see the changes, because it's the same variable.
 
The captured variable must be guaranteed to live long for the closure. C++0x does not guarantee this. You can either copy (in which case you get a different variable), or you can capture by reference and hope that the lambda does not execute after the surrounding function has returned.
 
@FredOverflow Why is the first part a requirement? C++0x doesn't guarantee it because it's not guaranteed in C++ outside closures either.
 
In C#, captured variables always live on the heap, if I understand correctly. Of course this is relatively easy to achieve in a language with GC.
 
that seems more like "something that happens to be true in most languages that have closures"
 
@jalf It is a requirement for my personal tastes only ;)
I don't like the idea of having named access to something that does not exist anymore.
 
3:27 PM
I'd argue that the main thing is that inside the closure, operating on the variable should have the same semantics as operating on it outside the closure. (Reseating the reference/pointer should be reflected outside the closure, for example)'
 
@FredOverflow Yes, the C# compiler cheats and creates a class for the captured variables. Sintactically they looks like variables, but they're really fields.
 
C++0x can sorta pull that off, as long as you capture by reference
 
@FredOverflow It's not really just your personal taste -- it is pretty much part of the (usual) definition of "closure". At the same time, a true closure basically requires saving the entire current state of the program until some arbitrary later time, with essentially no ability to specify that time, even if you want to. As such, it really does pretty nearly require GC to work at all.
 
@MartinhoFernandes How is a field not a variable? And since when is implementing something in terms of older language features cheating? Is implementing virtual with vtables also cheating, because they are just arrays of function pointers? ;)
 
@jalf If you capture by reference, you don't extend the live time. Which is one characteristic of some closures.
 
3:28 PM
@FredOverflow oh, right, I misunderstood what you meant. Yeah, I agree about lifetime then
 
About concepts, my perception is that there were two conflicting vision of concepts. And the disagreement between holders of them frightened the one which had no opinion.
 
Are there any papers by Bjarne or other famous C++ people about RAII vs. GC?
 
@AProgrammer That's sort of true -- specifically, there was a difference of opinion about what should happen implicitly.
 
@FredOverflow I meant local variables. And I admit "cheats" was probably not a good choice of words.
 
That is equivalent to:
int x;
std::function<void ()> f = [&](){ ++x; };
f(); f(); f();
std::cout << x;
 
3:31 PM
@MartinhoFernandes From the scope point of view, it still is a local variable, isn't it? :) It just does not live on the stack, but that should not matter.
 
@AProgrammer it's also my impression that one camp more or less went along, until they sprung the bombshell at the end that "what we're doing just isn't good enough. We need a rethink".
 
That is why I brought the comment on whether C++ lambdas are closures or not depending on the case...
 
@FredOverflow Again I ask the same question as before, what is the variable in Java/C#, the reference or the referred object?
 
@FredOverflow Yes, the compiler does a good job at making it feel the same.
 
3:34 PM
@DavidRodríguezdribeas Neither :) It is the thing that stores the reference.
 
@FredOverflow What's that to do with anything?
 
@jalf There is quite a bit of political play. Some seems to coming back with rejected propositions until they are accepted...
 
@MartinhoFernandes It discusses the "local = stack?" issue.
 
@AProgrammer regarding concepts?
 
(But sadly I can follow things nearly enough to have a true opinion instead of just an impression)
 
3:35 PM
@FredOverflow There was a thread in comp.lang.c++.moderated a few years ago. It was started by Herb Sutter, and Andrei Alexandrescu chipped in a bit as well. groups.google.com/group/comp.lang.c++.moderated/browse_frm/…
 
@FredOverflow Not in the same context. In C# (the language, not the implementation) there is no stack (well, outside unsafe code), but there are local variables.
 
@DavidRodríguezdribeas in C#, it's neither. It is the object itself (kind of like a C++ reference9
 
@JerryCoffin Link doesn't work :(
@MartinhoFernandes Interesting.
 
@jalf In general. Concepts was one thing I knew I couldn't follow near enough. And then I had to drop following things I though I had enough time to follow.
 
@FredOverflow I think you have to log into Google groups for it to work -- it does for me anyway. Maybe this is better: groups.google.com/group/comp.lang.c++.moderated/browse_frm/… ?
 
3:37 PM
@FredOverflow Actually, the point of that article is exactly that: there is no stack.
 
@JerryCoffin A then Walter pops in and mentions how D does it better. Typicial :)
 
@JerryCoffin Can you copy/paste the first sentence? Then I will simply google it.
 
@jalf I don't quite understand this, a reference is basically a pointer, and there is the lvalue (pointer itself) and the converted rvalue (value of the pointer), and we also have the pointed object (lvalue again), so which of the three is the variable?
According to @FredOverflow, it would be the pointer lvalue
(ouch, I hate using those terms, but I couldn't think of an explicit way of differentiating...)
 
@DavidRodríguezdribeas Yeah, but in C#, virtually everything is a reference, and references are copied by value, which would imply that if, inside a lambda, you reseated a captured reference, the change wouldn't be reflected outside the closure
 
@DavidRodríguezdribeas Inside a closure, I want to be able to say some_local_variable = new Foo(). Does that clear things up?
 
3:40 PM
my point is that it doesn't work like that, it works as if the closure saw a C++ reference (or an alias of the variable itself), not just a copy of a reference
 
@jalf Really? I thought it would?
 
@FredOverflow that's what I mean! ;) A naive implementation in C# would have that problem, but the actual implementation doesn't
 
@jalf and yet C# has reference semantics in functions, isn't it?
 
@DavidRodríguezdribeas C# has pass by value, just like Java
 
@jalf Unless you explicitly pass by reference with the ref keyword ;)
 
3:41 PM
value types are copied by value, reference types are passed by copying the reference
 
wasn't there something like: void foo( ref X a )?
 
The first sentence is a quote from elsewhere. See if this works: "Can you think of a legitimate reason why one would want to access an
object after destruction? This is not a loaded question; it's just that
I cannot think of a convincing example."
 
@DavidRodríguezdribeas yep, and that works around it, so you get proper reference semantics
 
@DavidRodríguezdribeas yes, that's kind of "passing a captured variable".
 
but ref is only allowed in specific limited cases (function parameters are the only ones I can think of), not local variables or class members
 
3:43 PM
@JerryCoffin I found this link, is that it?
 
And, if I understand it correctly that is what actually happens (through some compiler trickery that involves stealing variables from the local context and moving them as attribute of the lambda) with C# lambdas, or not?
 
@DavidRodríguezdribeas depends on what you mean by "that", but probably
 
@FredOverflow It looks like that's part of the same thread, yes.
 
@DavidRodríguezdribeas When a lambda captures variables, the compiler creates a class with a field for each one and a method for the actual lambda. Then it rewrites the method and changes every usage of the variable to a field access.
 
> As others have pointed out, even though only objects of a relative few types need to be explicitly "destructed" (to close file handles, etc.), the need for deterministic destruction bubbles into any object of any type that contains one of those "relative few" objects. This is a much more serious problem than you appear to allow for.
Good point!
 
3:45 PM
aye
@FredOverflow who said that? Anyone we may have heard of?
 
@MartinhoFernandes Yes, that is what I understood some time ago...
 
@jalf Paul Mensonides said it here.
 
@DavidRodríguezdribeas But by-ref parameters don't use that trickery. The runtime has special support for those.
From a usage point of view, the differences are irrelevant, though.
 
@MartinhoFernandes Yes, yes... but the semantics are the same... however it is implemented the semantic are that changes inside and outside the lambda/function will be visible on the other end
So by @Fred's definition C# does have closures, or am I missing something?
 
@DavidRodríguezdribeas it does, afaik
 
3:51 PM
@DavidRodríguezdribeas Yes, C# has true closures, not just anonymous functions.
 
@DavidRodríguezdribeas Yes, I think C# has closures.
 
@FredOverflow Yes -- Paul may not be particularly famous, but I generally thought he was an intelligent and (perhaps more importantly) sensible person.
 
I like what he says a bit further down in the same post too
 
@JerryCoffin Weren't there rumors that Paul was dead? ;)
 
> in my subjective opinion, we don't need it because it really doesn't give us much (if anything) over smart pointers with deterministic destruction in all cases. The areas that are interesting--that GC is typically used to enable--are things like backwards closures. Regular dynamic memory management is almost always trivial nowadays
thar we go
 
3:54 PM
I assume by "backwards closure" he means a regular closure?
 
I think so
 
hello there! How can I access a value in 2d array if its initialized like:
int **A = 0;
A = new int *[rowsA];

then: void print_matrix(int **A, int x, int y)
cout A[i+j*x] return a pointer...
 
or specifically the lifetime issue we talked about earlier, that the variable must still be alive when seen by the closure, even if it is long after it was in scope in the surrounding code
is my guess. That kiiinda fits with the "backwards" part
 
Have you tried cout << A[y][x] ?
 
also man, that thread is like a who's who of C++ :)
 
3:56 PM
@c4rrt3r You should probably start here
 
@FredOverflow return 0..
 
So if you held an object (call it variable, knowingly misleading, I know) through a shared_ptr, and you captured that shared ptr by value, then you would have a closure-of-sorts... on variable (not on the shared_ptr that holds it)
 
heh yeah, seems right
 
@FredOverflow Hm...I never thought nearly as highly of that Paul! (IMO, the Beatles are nearly tied with Led Zeppelin as "most overrated band of all time.")
 
@DavidRodríguezdribeas If your point was to show that a GC isn't necessary for closures, then I think you won ;)
and since these shared pointers would likely be an implementation detail invisible to user code, you wouldn't have to worry about circular references either
 
4:04 PM
@jalf No, I am not trying to prove anything, I am trying to understand
 
@jalf The problem is that some of the original variables (at least in C++) might be on the stack. Maybe GC isn't necessary, but you essentially have to enforce (like Java, et. do) that all variables are on the heap, where you can extend their lifetime as needed.
 
@DavidRodríguezdribeas well, I can't find a flaw in it, nevertheless
 
@DavidRodríguezdribeas That's very similar to what the C# compiler does.
 
@JerryCoffin no, you just have to know in advance which variables are going to be captured, so the compiler can put them in a shared_ptr instead of on the stack
that's trivial, and just like what the C# compiler does
of course, performance would likely be better with a proper GC, but I can't see why the shared_ptr version wouldn't work correctly
It would also cause other problems, in that destructors would be called later than expected, as seen by the code "outside" the closure
but you'd get proper closure semantics
 
@jalf You'd have to do a bit more than that, making it so the "normal" code dealing with the captured variables did so via a reference (e.g., roughly like Java and C# normally do). Ultimately, yes, if you change enough to make it almost like C# otherwise, you could end up with it working about like it does in C#...
 
4:09 PM
:)
well, it's not just C#. Pretty much any language which implements closures does it like that
 
I think that could actually be implemented outside of the core language, by means of libraries with type erasure, in the same way that std::function/boost::any work... so if you want to *actually* capture an integer variable you could write:

capturable<int> x;
std::function< void () > f = [=]() { /* operate on x */ }
 
@jalf Oh, I didn't mean to imply it was unique to C#; was just using that as an example.
 
with the usual caveats of object wrappers that seem to never work perfectly
 
@jalf Some lisp are using GCed stacks
 
ok i'm closer now.. changed: void print_matrix(int *A, int x, int y) and passed like print_matrix(&A[0][0],rowsA,columnsA);

Now for input (1,2,3,4) it returns {{1,6-digitnumber},{2,6-digitnumber}}
any idea why?
i have done this before and it worked.. but now matrix is dynamically created with pointers of pointers
 
4:14 PM
:602113 Have you changed the array definition from:
   int **A = new int *[rowsA];
into something like
   int *A = new int[ rowsA*colsA ];
??
If you haven't, which looks like you haven't, then the problem is on what you are declaring and how you intend on using it
The variable definition above declares an array of *pointers* to ints, to properly allocate the memory you would have to do something like:
int **A = new int*[rowsA];
for ( int i = 0; i < rowsA; ++i )
   A[i] = new int[colsA];
But that is generally not a great idea
But that is not generally a great idea. Search in SO proper for bidimensional arrays
 
@DavidRodríguezdribeas thanks a lot! will do.
 
@c4rrt3r or, go back to the link I provided above and read the entry on multidimensional arrays (by @FredOverflow, present here)
 
am I the only one who's having a bit of trouble focusing on down-to-earth C++ questions like that after our discussion? :)
 
@jalf Nope... I basically skipped over the original question by providing a link... I would probably have produced the code above any other day...
 
4:51 PM
In below sample, how would you make the "cancel"-condition thread-safe?

`class Uploader
{
public:
void startUpload(const std::string & inFile)
{
CreateThread(boost::bind(&Uploader::startUploadImpl, this, inFile));
}

private:
void startUploadImpl(const std::string & inFile)
{
while (!isCancelled())
{
uploadSomeMore();
}
}

bool isCancelled() const
{
return mIsCancelled;
}

void setCancelled()
{
mCancel = true;
}

bool mCancel;
};`
 
My rule of thumb: if you have so much code it requires scrolling, it'd be a better fit on SO proper ;)
3
 
Perhaps my sample code is written in a style that consumes too much space. It's really small actually.
@jalf But perhaps, you're right..
 
yeah, I'm not saying it's complex, just that it takes up too much space to easily fit here ;)
anyway, thread safe with respect to what? You could just make sure mCancel is only ever accessed by atomic instructions
or guarantee that it's only ever accessed by a single thread
 
@jalf For example, while the upload is progressing in the worker thread, the user may press a "Cancel" button in the main UI thread. So the boolean would be accessed from two different threads.
My usual implementation is to use a mutex. However, I tend to end up with lots of mutexes after a while..
Probably volatile will be sufficient in this situation.
 
you could do the same just with a few memory barriers and by ensuring that every access is atomic. But I'm not sure that's preferable in the general case
 
5:02 PM
@StackedCrooked: An atomic access will be perfectly good in that situation
 
@StackedCrooked why does the boolean need to be accessed from two threads, you can just change it in the main thread and then stop the thread
 
infact, there are condition variables for this exact purpose provided by the Windows API, I believe
 
@Tony The thread must stop itself.
 
@Tony stopping threads from the outside is a pretty nasty thing to do
of course, if given the option, I'd use transactional memory to make it thread-safe ;)
 
Well my options seem to be: nothing, mutex, volatile, lock-free functions?
 
5:04 PM
Are you 100% sure volatile would work here?
 
The question I actually wanting to ask is: Is it worth it to write a template wrapper class that bundles a variable with a mutex?
 
Whoo, 6 accepted answers today and I've reached the rep cap without them... that must be a personal record.
 
no
 
@StackedCrooked you have many options, but those seem to be the obvious ones, yes
 
@jalf The only problem that I am aware of with volatile is reordering. However since I'm using method to encapsulate each kind of access I don't think it will be a problem.
 
5:05 PM
@StackedCrooked: Use an atomic operation, "nothing" and "volatile" both won't cover this situation and "mutex" is overkill
 
@StackedCrooked "I don't think it will be a problem": famous last words when dealing with multithreading ;)
 
@jalf Thx for reminding me of what I seem to have forgotten :)
 
@DeadMG I does have to be cross-platform (Visual Studio and GCC at least)
 
@StackedCrooked: x86 provides these instructions as part of it's set; all compilers targetting it should provide appropriate extensions
 
@jalf Yes, the fact is that I don't know.
 
5:06 PM
tbh, unless you're super concerned with performance, you might as well just wrap it in a mutex and move on to more interesting problems ;)
2
 
I know that GCC and VS both provide
 
but yeah, the atomic ops can definitely be implemented in a cross-platform manner
or wrapped, anyway
 
boost::thread probably provides them
 
I created this utility:

template<class T>
class AtomicPrimitive : boost::noncopyable // let's ignore copy semantics for now
{
public:
AtomicPrimitive(T inValue = T()) : mValue(inValue) { }

void set(T inValue)
{
ScopedLock lock(mMutex);
mValue = inValue;
}

T get() const
{
ScopedLock lock(mMutex);
return mValue;
}

private:
mutable Mutex mMutex;
T mValue;
};
 
that's not atomic
that's serialized
the two are not the same thing
 
5:08 PM
@DeadMG It's thread safe. That's the main issue.
 
yeah
I can't tell you that it isn't thread safe :P
 
@DeadMG How does atomic prevent deadlocks?
 
because the operation is guaranteed to be completed before the next instruction is executed
if you atomically read some memory, then the lock is locked and unlocked in the same hardware instruction
making it pretty impossible for you to lock another lock in that time
 
@DeadMG Ok, but then it won't support coordinated changes?
 
what do you mean?
 
5:10 PM
@StackedCrooked not directly, but you can use it to compose larger thread-safe operations
 
@DeadMG For example when I want a block of code to be executed as one operation.
 
they can do, but not always
 
@StackedCrooked then you use transactional memory!
 
any block of code that could be expressed in maybe a few or one hardware operations, you probably could
and many higher-level operations can be decomposed to atomic operations
 
@jalf Ok, any good C++ libraries out there??
 
5:11 PM
but not all of them
 
I know it's not a useful answer, and that I'm starting to sound like a broken record by now ;)
 
@jalf I have some Clojure experience. It's really easy there ;)
 
@StackedCrooked: I personally would pick Intel's Thread Building Blocks
 
@StackedCrooked kiiind of. There are plenty of C-style ones you could use if you really wanted to. I'm developing my own which basically works, but a few bits of it aren't as efficient as they should be ;)
also it comes with absolutely no warranties or support ;)
 
it's not free, but cross-platform and offers very good interface and handles a lot of implementation details for you
Microsoft has a proprietary library for Visual Studio which is very similar, and I like it a lot
 
5:13 PM
@jalf One complaint that I once read about transactional memory is that it requires the library to do a lot of book-keeping at runtime. So the speed advantage is lost.
@jalf Link? I'd like to have a look at least..
 
yeah, as far as I'm concerned, it's not about gaining a speed advantage, but about providing safer and more intuitive semantics with a tolerable speed disadvantage
I think the discussions about it actually improving speed are mostly academic in nature
It could happen, if a lot of conditions were met
 
@DeadMG I think that is the only safe use of volatile for multithreaded. There is not much that can be reordered in a loop with respect to the loop condition. I.e. the compiler cannot reorder effects of the loop body to be executed after the condition is tested. And even if they could be reordered (i.e. the variable preloaded into a register) that would only imply that another iteration is executed. Am I missing something there?
 
but I'm happy if it's not noticeably slower than locks
 
@jalf I do find it conceptually similar to read/write locks. (Apart the from "optimistic" vs "pessimistic" difference.)
 
it was my understanding that the main advantage of STM is that it just works, and if you're writing something with very high performance requirements, then you might want to go closer to the hardware
 
5:16 PM
@DavidRodríguezdribeas Are you referring to the sample I posted?
 
@StackedCrooked sec, just verifying that I've got the most recent version online. Like I said, it's still under development, so there's no documentation or anything :)
 
@jalf Cool
 
@David: Honestly, it doesn't really matter if volatile is safe for like, one rare operation
 
@StackedCrooked well, with the exception that with locks the responsibility is on the programmer to ensure that all code paths that access a variable are protected by locks
 
easier and less error-prone just to ditch it
 
5:17 PM
@StackedCrooked Yes, the use of a volatile variable there ensures that the compiler generates code to write to memory, the hardware then will invalidate the other processes caches and the change will be immediately visible by other threads
 
with STM, you can enforce that the variable can only be accessed within a transaction
ideally, anyway. Depends on the STM implementation (and on what's possible in the language, of course)
 
@jalf Yeah, I don't think you can prevent such a thing in C++.
 
And atomic... well, setting a boolean variable is atomic, like reading an aligned int variable on intel platforms, which is different from incrementing which takes more operations read, increment, write.
 
you sort of can
of course it can always be circumvented if the user is sufficiently stubborn
 
@jalf :)
 
5:20 PM
And yet I have used mutexes for that particular use case in the past, so I guess I agree with jalf on that: use a mutex and forget the issue
 
but I'm using a wrapper template kind of like a smart pointer, which only allows access to its inner variable if you pass it a transaction reference
so unless you intentionally try to break it, variables meant to be shared using STM can only be accessed inside a transaction
 
@DavidRodríguezdribeas I find that I frequently need to lock single variables: "cancel flags", "quit flags", counters, etc.. And it results in many mutexes. I'm I the only one that has this?
 
that's what atomic operations are for
 
@DeadMG I agree with you on this one.
 
@StackedCrooked I do it often, and note that there are actually very few cases where you can get along without doing it. For example, you cannot increment a counter without a lock (or using atomic operations)
 
5:24 PM
curious
MSDN seems to think that you can only perform atomic addition on Itanium
 
ok, @StackedCrooked, jalf.dk/stm.zip
and pastebin.com/GKnuXyZH for a very tiny example of the most basic usage
 
and x86 only has inc/dec
 
I just typed it out without testing it though, so it's possible there's a typo or two ;)
 
@jalf Cool thanks
Gonna have a look.
 
I'm working on polishing off the last major feature of it (retry/orelse functions, as Haskell STM has), but after that, I'll put it "properly" online and write up some documentation and stuff
it should work in VC2010 and GCC 4.5 at least
atm it uses a few C++0x features, just out of convenience (a few autos and lambdas, nothing more), so it'd be fairly straightforward to get C++03 support as well
by the way, how does it work in clojure? I don't think I came across that when researching for my lib
@DeadMG isn't that correct? I don't think x86 has atomic add instructions
 
5:31 PM
@jalf so a "SECTION" starts a transaction?
 
@StackedCrooked oh nah, that's from the unit test lib I'm using
 
Ok, :)
 
stm::atomic runs a transaction. YOu pass it a functor, which becomes the body of the tx
in the pastebin example I linked to above, I used a lambda, but a regular functor would work too
the functor just has to take a stm::transaction& as its parameter, then atomic will call it and ensure it runs atomically
 
@jalf So STM means that two threads can start a transaction and access overlapping data. Only in case of a conflict one of the two transactions is cancelled. Right?
 
in case of a conflict, one of them is rolled back and implicitly retries
so what the user sees is jsut that both complete normally
 
5:35 PM
Rolling back means undoing changes?
 
yep
but it automatically retries, so you don't have to write any code to handle conflicts
 
is there some guarantee of termination?
 
@jalf But if one of the actions was a delete, how do you undo that?
 
@DavidRodríguezdribeas in most cases. At the moment, it is possible to create a situation where it'll probably never terminate, which is one of the things I need to fix
@StackedCrooked at the moment, I don't. That's a side effect, which shouldn't take place inside a transaction
same with file I/O
 
@jalf Doesn't that apply to most side-effects?
 
5:37 PM
but if the delete is part of a larger data structure, then it can done since the transaction will operate on a private copy of the data structure, and when committing, write that back to the "outside" copy. So if, say, you deleted a node during the transaction, that change will be reflected properly
@StackedCrooked yup
@DavidRodríguezdribeas you have to try pretty hard to prevent it from terminating though. It won't happen just by accident
 
@jalf So that means that transactions should mostly operate on local data, or even simply avoid code that has side effects?
 
@StackedCrooked doesn't have to be local data, but they should avoid code that has side effects, yeah. Unless it is hidden away inside a RAII object or something, which will automatically do the right thing when it is copied into the transaction and back out at commit
 
@jalf what kind of side-effects is the code capable of rolling back?
 
I've got some ideas for allowing you allowing some side effects in transactions (by letting the user specify hooks to either commit or rewind the side effect, which are then called by the library), but that's not implemented yet
@StackedCrooked none, at the moment. What it does is just copying the shared object into a private buffer during the transaction, and then copying (or moving) it back when committing. So you can do what you like on that object, just remember that it will get copied once you're done, and it may be executed more than once (if the transaction rolls back and retries because of a conflict)
so, for example, if you deleted a node from a std::list, you'd be safe, because you're only deleting the node from the private copy, which no other thread will touch. And when committing, the original list will be overwritten by your modified one
but if you explicitly call delete on a variable, then you may be in trouble
 
@jalf Looks interesting though. Are you planning on releasing it sometime?
 
5:44 PM
@StackedCrooked yep, as soon as I've got the last wrinkles worked out, and have got some documentation down on paper ;)
 
@jalf Some documentation would be nice. Even just a broad overview would be interesting.
 
@StackedCrooked I know. It was originally part of my masters thesis at university, so I've got all the stuff I wrote back then.
but I'd like to update it a bit, and cut out all the irrelevant parts ;)
maybe that should be my project for this weekend :)
 
@jalf Definitely looks like a fine way to spend time on.
 
it really struck me when I did the original research, how all the C/C++ implementations had such crippled interfaces
no genericity, macros everywhere, and requiring lots of boilerplate code
 
@jalf yeah, I know..
 
5:48 PM
or they required intrusive changes to every object you wanted to access, like deriving from some STM base class
so I figured it'd be fun to try to make a modern C++ equivalent
now I just need to polish it a bit so I can release it on the interwebs
 
@jalf Are you familiar with the Poco C++ libraries. It was founded for the same reasons: to have a clean and modern alternative.
 
yeah, never used them much though, but sure
 
In the past people must have thought that macros make your code look more professional..
 
although boost was more of an inspiration for me
I guess in the past people just didn't see any alternatives :)
 
@jalf It would be for me were I able to understand the source code ;)
@jalf I like Poco because it looks like code that I could write. Boost seems to be written by inhuman beings..
 
5:52 PM
@StackedCrooked lol, I don't bother trying to understand it, as long as I can understand the basic idea of what they're doing
 
Well, boost definitely impacted C++ programming like no other library.
 
yeah (apart from the STL)
 
@StackedCrooked STL
 
sheesh, so pedantic ;)
 
5:56 PM
Actually, without boost, and the modern C++ programming trend, I probably would have agreed with the idea that C++ is a relic of the past
 
@jalf Cheater!
 
@JamesMcNellis you can't prove anything!
@StackedCrooked same
 
@jalf: edit icon notwithstanding
 
@StephenCanon ah, but it doesn't prove anything about what I edited
at least afaik
 
Lounge<C++>: Where statements are undefined until proven defined.
 
5:58 PM
@StackedCrooked yeah, it really is incredible how much of a difference some changes to programming style can make
for the language as a whole
 
@jalf: click on message -> history
 
@StephenCanon damn ;)
 
@JamesMcNellis My first working day as a professional programmer was in 2005. The codebase I had to work with was started in 1999. It didn't use STL, instead they had written their own "Basic Framework Library". Reason was unreliable compatiblity and performance of STL across different platforms. Can you believe that?
 
@StephenCanon Ooooh, I didn't know that feature was there.
@StackedCrooked In 1999 I could certainly believe that.
 
@StackedCrooked sure. It was more or less true back then. But still sad when you see it years later
 
5:59 PM
1999 isn't so long ago!
 
@StackedCrooked: yeah. In 1999, there were a lot of ... fragile things about STL
 
in C++ terms, it is
 
@jalf You can use lock xadd.
 
depending on what platform you were on.
 

« first day (181 days earlier)      last day (4769 days later) »