|
Oh yeah, I totally forgot that C++/CLI uses different syntax for managed types
|
# ? Jan 4, 2012 00:49 |
|
|
# ? Jun 7, 2024 15:18 |
|
I have a thread which checks if a bool is set, and if it is, calls a function through a pointer. Once set, neither the bool nor the pointer will change for the lifetime of the thread. Another thread will set the pointer and then set the bool. I don't care about the case where the pointer has been set but the bool is not. Are there any race conditions I haven't thought of, and is it a horror to just mark both the bool and the pointer as volatile, ignoring any other threading structures?
|
# ? Jan 4, 2012 07:17 |
|
tractor fanatic posted:I have a thread which checks if a bool is set, and if it is, calls a function through a pointer. Once set, neither the bool nor the pointer will change for the lifetime of the thread. Another thread will set the pointer and then set the bool. I don't care about the case where the pointer has been set but the bool is not. Are there any race conditions I haven't thought of, and is it a horror to just mark both the bool and the pointer as volatile, ignoring any other threading structures? Yes, it is a horror. volatile is not good enough, even for this. Never simply use volatile for anything multi-threaded related in standard C++, period.
|
# ? Jan 4, 2012 07:31 |
|
tractor fanatic posted:I have a thread which checks if a bool is set, and if it is, calls a function through a pointer. Once set, neither the bool nor the pointer will change for the lifetime of the thread. Another thread will set the pointer and then set the bool. I don't care about the case where the pointer has been set but the bool is not. Are there any race conditions I haven't thought of, and is it a horror to just mark both the bool and the pointer as volatile, ignoring any other threading structures? Aside from volatile not being the correct thing, wouldn't it be simpler to just check the pointer, and call through it if it isn't NULL? The bool does not seem required in this situation, and with only a single pointer to base your condition on you can make use of atomic operations fairly easily.
|
# ? Jan 4, 2012 07:38 |
|
ShoulderDaemon posted:Aside from volatile not being the correct thing, wouldn't it be simpler to just check the pointer, and call through it if it isn't NULL? The bool does not seem required in this situation, and with only a single pointer to base your condition on you can make use of atomic operations fairly easily. Yeah I should probably just do that.
|
# ? Jan 4, 2012 07:49 |
|
tractor fanatic posted:I have a thread which checks if a bool is set, and if it is, calls a function through a pointer. Once set, neither the bool nor the pointer will change for the lifetime of the thread. Another thread will set the pointer and then set the bool. I don't care about the case where the pointer has been set but the bool is not. Are there any race conditions I haven't thought of, and is it a horror to just mark both the bool and the pointer as volatile, ignoring any other threading structures? The answer is "it's platform-dependent". volatile guarantees that the compiler won't reorder any of the volatile accesses, but doesn't put any such constraints on the hardware. If you want your code to work fine on x86 but have a hard-to-catch race condition on any other architecture, then just using volatile is fine.
|
# ? Jan 4, 2012 08:08 |
|
The latest article on http://blog.regehr.org/ has a nice writeup on the subject with some interesting examples of code that may introduce problems.
|
# ? Jan 4, 2012 08:32 |
|
tractor fanatic posted:I have a thread which checks if a bool is set, and if it is, calls a function through a pointer. Once set, neither the bool nor the pointer will change for the lifetime of the thread. Another thread will set the pointer and then set the bool. I don't care about the case where the pointer has been set but the bool is not. Are there any race conditions I haven't thought of, and is it a horror to just mark both the bool and the pointer as volatile, ignoring any other threading structures? You need memory barriers --- otherwise the write to the pointer may become visible -after- the write to the bool in the reading thread.
|
# ? Jan 4, 2012 16:21 |
|
I'm writing C#, but this probably applies to C++ as well. Recently I got it into my head that my next 2D game project would profit in many ways by using signed distance fields to describe the levels. This means that any point in 2D space is accompanied by a positive or negative distance to the nearest surface. I've started out by writing a class with various primitive shapes, each with a function called "DistanceFrom" that returns a float representing the signed distance, like so:code:
What I would like to do is create another class with useful operations that can be performed on the signed distance fields to get useful things like gradient vectors, kind of like this: code:
|
# ? Jan 4, 2012 17:14 |
|
snorch posted:What I would like to do is create another class with useful operations that can be performed on the signed distance fields to get useful things like gradient vectors, kind of like this: It looks like this would be a good use case for extension methods. Just define your common operations (like DistanceFrom) in interfaces (grouped as appropriate; I'm assuming not all operations will make sense for all shapes), have your concrete classes implement them and write extension methods taking an instance of the appropriate interface type as the "this" parameter.
|
# ? Jan 4, 2012 17:28 |
snorch posted:... where the variable SDF can be any class, so long as it has a DistanceFrom function. Is there any way I can easily do this? I'm pretty sure the .NET megathread would be a better place for your C# questions, but anyway... What I would do in this case was define an interface (call it BoundedObject or something) that has the DistanceFrom method, then have all your various 2D objects implement that interface. Your getGradient method would then take an object of the BoundedObject type.
|
|
# ? Jan 4, 2012 17:28 |
|
Are there any coding tutorial/puzzle sites similar to project euler or in the same vein that would be good for C++? I like doing self teaching/messing around but I am terrible at coming up with crap for me to do so exercises like "create a solver for x problem" are great.
|
# ? Jan 5, 2012 20:09 |
|
edit: welp, it turns out the compiler was inlining everything, I just did something dumb with the testing. I'm doing a bicubic interpolation inside a loop, and I'm trying to cut some of the time off. The interpolation function itself is a templated function that calls a get function parameter, to get a bunch of points, and then returns the interpolated value. It looks like this: code:
The inner loop calls the get_interpolated function, which essentially just calls the bicubic function, like this: code:
code:
tractor fanatic fucked around with this message at 22:16 on Jan 5, 2012 |
# ? Jan 5, 2012 20:51 |
|
I'm trying to give myself another go at C++ after failing miserably in college over a decade ago. The book I'm using is Ivor Horton's Beginning Visual C++ 2010. http://www.wrox.com/WileyCDA/WroxTitle/productCd-0470500883.html Since I only have the express edition, I won't be able to get everything this book goes through but for right now I'm struggling with pointers and arrays. For the most part the book is good. The first half of the book is learning C++ with the first part of the chapter dealing with native C++ and the second part dealing with the differences and additional features in C++/CLI. Aside from the confusion that can cause, the only big problem I noticed was an end of chapter exercise used a function that wasn't introduced to solve a problem that couldn't be solved with previously explained material. Right now I am on Chapter 4: Arrays, Strings, and Pointers. When I tried this book last summer this is where I gave up. I have had the most difficult time understanding pointers and this was no different. For this chapter I decided to do the native C++ exercises before reading about C++/CLI. The first two exercises are: 1. Write a native C++ program that allows an unlimited number of values to be entered and stored in an array allocated in the free store. The program should then output the values, five to a line,followed by the average of the values entered. The initial array size should be five elements. The program should create a new array with five additional elements, when necessary, and copy values from the old array to the new. 2. Repeat the previous exercise, but use pointer notation throughout instead of arrays. For the first problem I can't figure out how to use new/delete for an array instead of a pointer to an array, or how to copy information to a bigger array, to the point where I don't understand what they were asking. After failing to get that for a day I tried doing it in pointer notation since using pointers was how new/delete was demonstrated. After fixing up all the errors I got this: code:
|
# ? Jan 7, 2012 03:10 |
|
I think they just wanted you to use array notation for the first one, so pvalues[i] instead of *(pvalues+i) Your code is inefficient in that it makes a temporary array and then discards it. You don't need to make a temporary array and fill it will the old data, then make a new array and copy the data from the temporary. You can just use the temporary directly as the new array, saving you a bunch of copies.
|
# ? Jan 7, 2012 05:22 |
|
I'm working on a C++/CLI project that incorporates some older MFC dialogs for legacy purposes. I also have a new dialog that inherits from the System::Windows::Forms Form class. Is there a way to get the new dialog to be a child of one of the older MFC dialogs, so that modal properties of blocking access to the parent window works properly? So far, I've tried using NativeWindow / Control's FromHandle function together with the CWnd HWND handle and passing that into the Form's ShowDialog function. This doesn't seem to work though.
|
# ? Jan 7, 2012 10:42 |
|
tractor fanatic posted:I think they just wanted you to use array notation for the first one, so pvalues[i] instead of *(pvalues+i) Indeed, I tried to envision the array like a bucket and the loop like a kid filling the bucket up with water. When the bucket gets full he needs a new bucket. So how do we tell the kid to fill up the temp bucket instead of getting him a new bucket with his name on it. Why not peel his nametape of the old bucket and put on the temp bucket instead of trading the old bucket for a new bucket. So.. code:
code:
Where do I put the delete [] ptemp; line? If I put it after pvalues = ptemp or after the end; it spits out garbage data or crashes the program. If I comment it out, the program works fine. I gave up and dowloaded the code. The author does it like this code:
I have a feeling from here on out this is going to be a long difficult book.
|
# ? Jan 7, 2012 17:12 |
|
Crazy Mike posted:and he doesn't have a delete [] temp; line. After wondering why we don't need this line and writing this all out it seems that by deleting the old values array and storing the address of the new array, when you delete that array when it is called values it has the same effect of deleting the temporary array that is now being used.
|
# ? Jan 7, 2012 17:46 |
|
Remember that "temp" and "values" are just names, nothing more. You cannot "delete" a pointer, only the memory that it points to. So when you say delete[] values; it's freeing the memory which the name "values" currently points to. I always hated all the analogies people liked to throw at me when learning about pointers. I finally understood it once somebody explained it to me in literal terms. So here's my explanation if it helps: code:
"values" points towards some newly allocated memory. Call it memory A. The contents of memory A is uninitialized. Enough space exists for 5 doubles. Line 2: double* temp = new double[10]; "temp" points towards some newly allocated memory. Call it memory B. The contents of memory B is uninitialized. Enough space exists for 10 doubles. code:
The contents of memory A is assigned to 0,1,2,3,4. code:
The contents of memory A is copied into memory B. The same data now exists in both memory locations. code:
Memory A is freed. That memory space is now no longer owned by the program. Memory B remains untouched. code:
Line 8: values = temp; The name "values" now points to Memory B. We don't have to worry about memory A anymore. code:
"temp" stops pointing to memory B. The final state is this: code:
Dicky B fucked around with this message at 03:53 on Jan 8, 2012 |
# ? Jan 7, 2012 18:37 |
Good explanation, except for one thing:Dicky B posted:Line 7: delete[] values; I think you aren't explicit enough about values being invalid after this. Sure, the variable has a value (points somewhere), but it points to invalid memory, it has an invalid value. I would change your illustration to: code:
|
|
# ? Jan 7, 2012 20:17 |
|
Also, don't use l as an array indexer. It gets confusing fast.
|
# ? Jan 7, 2012 20:39 |
|
Only if you use a terrible font.
|
# ? Jan 7, 2012 22:13 |
|
e:wait this was stupid anyways
tractor fanatic fucked around with this message at 22:51 on Jan 7, 2012 |
# ? Jan 7, 2012 22:49 |
|
Thanks for your help explaining those things. The next exercise was a bit simpler but still exposes some problems with this book. Declare a character array, and initialize it to a suitable string. Use a loop to change every other character to uppercase. Hint: In the ASCII character set, values for uppercase characters are 32 less than their lowercase counterparts. Now I remember earlier the book mentioning string functions to see if the character is a letter or change its case, however looking back those Char::IsLetter and Char::ToUpper()are for C++/CLI programs. The book mentions using the cstring header for certain string functions but in his solution he uses the string header. I just checked out his answer code:
code:
|
# ? Jan 8, 2012 07:55 |
|
Crazy Mike posted:why does my *(saying+i) -= 32; line not work like the str[i] -= 32; line?
|
# ? Jan 8, 2012 08:15 |
|
Profiling my first stab at a voxel engine. It's fun but it feels like I have no clue at all what are the actual performance impacts on a C++ program. I model the world with 16x16x64 chunks of voxels defined like:code:
code:
Would it be better to do something like this instead: code:
Bisse fucked around with this message at 01:37 on Jan 10, 2012 |
# ? Jan 10, 2012 01:29 |
|
Are you in release mode when you're testing the speed? I'm pretty sure a compiler knows all the tricks of multi-dimension array lookups that you would try. Also, when I've run very sleepy it doesn't even measure the get functions because they're usually inlined by the compiler already. Maybe four dimensional array lookup puts it past the threshold of simple functions though. Other than that, maybe your get function is called by too many things and you should look into reducing the number of calls.
|
# ? Jan 10, 2012 01:48 |
Make sure that function gets inlined. Compared to the actual body of it, all the prologue and calling stuff is going to be really heavy. All the actual body of the function does is calculate a pointer value, it doesn't even fetch (dereference) anything -- well apart from this->voxels. Move the function definition into the class declaration, like this: code:
On a side note, #include <stdint.h> and use standard types such as uint8_t defined by your compiler vendor. (Except if you use MSVC 2008 or an earlier MSVC, they don't ship stdint.h but there exists a third-party implementation of it for those compilers.)
|
|
# ? Jan 10, 2012 02:26 |
|
edit: misinterpreted what you were saying. But I'll leave this bit: He's obviously profiling a debug build.
Paniolo fucked around with this message at 05:15 on Jan 10, 2012 |
# ? Jan 10, 2012 04:20 |
|
HOLY MOTHER OF gently caress WE HAVE LIFTOFF Without inlined get/set function: 10 FPS With inlined get/set: 700 FPS Only a 7000% performance boost, tyvm EDIT: Seems I'll need to turn this guy into a #define to be able to do any debugging at all. (Since Visual C++ 2010 inline optimization is incompatible with its debug mode) Bisse fucked around with this message at 23:35 on Jan 10, 2012 |
# ? Jan 10, 2012 22:51 |
Bisse posted:EDIT: Seems I'll need to turn this guy into a #define to be able to do any debugging at all. (Since Visual C++ 2010 inline optimization is incompatible with its debug mode) Don't profile your debug builds. Don't debug your profiling builds. Debugging is identifying incorrect behaviour, profiling is identifying performance bottlenecks. You generally shouldn't need performance to be able to identify your bugs, and you should only start profiling when you know your code is otherwise correct.
|
|
# ? Jan 10, 2012 23:06 |
|
nielsm posted:Debugging is identifying incorrect behaviour, profiling is identifying performance bottlenecks. You generally shouldn't need performance to be able to identify your bugs, and you should only start profiling when you know your code is otherwise correct. Agreeing that obviously you don't want to profile a debug build, but when not inlining a function takes you from 700fps to 10fps, it's a bitch that VC++ won't inline it in debug mode.
|
# ? Jan 10, 2012 23:18 |
|
You can enable inlining in debug builds, it just defaults to disabled. It's under Project Properties -> Configuration Properties -> Optimization -> Inline Function Expansion. Not sure if it negatively impacts the debugging of those functions or not, though. Edit: If it still doesn't get inlined with that option, you can try also using __forceinline instead of inline. chglcu fucked around with this message at 00:38 on Jan 11, 2012 |
# ? Jan 11, 2012 00:35 |
|
Any ideas on how I could access information from a structure contained in a structure that inherits from an outside structure?code:
code:
|
# ? Jan 11, 2012 00:40 |
|
The nested structures aren't your problem. As don't have a myInt field. To do exact what you're asking you'll have to downcast (or more generally, access that field through a type that actually has it - you could just use a B::C * to begin with). This seems like one of those "what are you actually trying to do?" questions, though.
|
# ? Jan 11, 2012 00:51 |
|
Princess Kakorin posted:Any ideas on how I could access information from a structure contained in a structure that inherits from an outside structure? As GrumpyDoctor said, this doesn't have to do with nested types, you are misunderstanding inheritance. In the code you posted you are accessing an instance of C via a pointer to A. There is no "myInt" in A so you get a compile error. If you need to access myInt, then the object through which you're accessing the instance of C needs to know it's a C (either use a pointer or reference to a C, or a C object itself).
|
# ? Jan 11, 2012 01:42 |
|
WELP. I actually have no idea what I was thinking when I made the post, or why I thought inheritance worked that way. But thanks guys, I have it figured out now!
|
# ? Jan 11, 2012 03:34 |
|
prolecat posted:You can enable inlining in debug builds, it just defaults to disabled. It's under Project Properties -> Configuration Properties -> Optimization -> Inline Function Expansion. Not sure if it negatively impacts the debugging of those functions or not, though.
|
# ? Jan 11, 2012 09:04 |
|
That's odd. It didn't complain for me when I tried it out before posting. I was using Professional though. Edit: Just tried it again, and I did get an error, so don't know WTF was up yesterday. Changing the debug format to /Zi instead of /ZI seems to get rid of the error, though it will disable edit and continue. Edit #2: Just to make sure, checked the assembly and it did inline correctly. chglcu fucked around with this message at 21:00 on Jan 11, 2012 |
# ? Jan 11, 2012 20:46 |
|
|
# ? Jun 7, 2024 15:18 |
|
For background: I'm programming a robot, I want the robot to execute tasks on its own but allow the user to type some command into the console and be able to take control from it at any time. The first way I've though of this is have a loop going around constantly (Check commands, execute robot actions, print data) but is it possible to do that in a way such that it ignored the "check commands" step if nothing has been entered? Maybe I'm being dumb, but any cin >> operation is just gonna pause it all, right? The other option, of course, is threading. If that's the way I should go instead, could anyone suggest a good library that's nice and simple for this, any background documents I should maybe read or just any tips in general? Many thanks in advance everyone! e: C++ y'all!
|
# ? Jan 12, 2012 02:36 |