|
I need to know how safe it is to write to one file from multiple processes. I can find a lot of information about multiple processes appending to the same file, but that isn't what I am doing. I am creating a cache file. I am opening this file as a new file, spew some crap to it, and then closing it. I am not appending. A programmer is trying to convince me that two processes running at the same time can both write to this one file, creating a bad file. I suppose that is true, because I cannot think of how the operating system would actually prevent that from happening. The thing is, in the case of a race condition, I don't actually care who wins. It doesn't matter. Both processes are going to wind up spewing out the same exact cache file. I just don't want them both to write to the same file at once and create a monstrosity. Edit: this is c++ on windows 7.
|
# ? Oct 13, 2015 20:13 |
|
|
# ? May 18, 2024 12:14 |
|
If every single process is cleanly writing a brand new file with the same name, and as you say you don't care who is last, the only problem is making sure you aren't trying to write when there are actual file system level locks on the file. So you may have some resource contention issues if you have 100s of processes spinning all waiting to write the same garbage to the same file. It's probably a better solution to use something like SQLite here instead of a flat file. You'll get less deadlocking issues.
|
# ? Oct 13, 2015 20:22 |
|
I suppose I need to understand just how much file system level locking is going on. If a process sees the cache file is there already, then it will just read in the cache file instead of generating it. I don't want the read to happen before the file is done being written by another process, and then only get half of the file. Is that a possibility? This has to be a common thing that I am trying to do. I'm just not sure what it is called. I don't really want to switch to something other than just reading/writing files.
|
# ? Oct 13, 2015 20:29 |
Open the file for exclusive write access. The first process to do so will succeed, the rest will fail with a sharing violation. Those that fail then know that they will have to wait for someone else to build the file. (Or maybe they just don't need to bother writing the file.)
|
|
# ? Oct 13, 2015 20:29 |
|
baby puzzle posted:I don't really want to switch to something other than just reading/writing files. If you just use files, you'll have to do all the checks for sharing violations and so forth. Letting SQLite do it is a lot simpler, you just execute your add/update or read queries. SQLite is really simple to use and doesn't require a DB engine to be installed beforehand, and is completely file based, so instead of having your custom mycache.dat file, you have an SQLite mydb.db file.
|
# ? Oct 13, 2015 20:42 |
|
baby puzzle posted:Edit: this is c++ on windows 7. Create a temporary file (in a nearby directory or somewhere on the same file system) with a unique name, write to it, close it(?), then rename it. If you're on NTFS the renaming will be an atomic operation. If it's something other than NTFS then do some reading.
|
# ? Oct 13, 2015 22:05 |
|
sarehu posted:Create a temporary file (in a nearby directory or somewhere on the same file system) with a unique name, write to it, close it(?), then rename it. If you're on NTFS the renaming will be an atomic operation. If it's something other than NTFS then do some reading. This is actually what I was trying (writing to a randomized filename, then renaming it). If it is a real thing that works then maybe I'll just keep doing it that way. Edit: I get random failure to read the files when I do this. It doesn't seem to be safe. I think I just need to figure out how to do a cross-process mutex. baby puzzle fucked around with this message at 23:34 on Oct 13, 2015 |
# ? Oct 13, 2015 22:36 |
|
Well, you'll have to research this more than me, then, but I'm guessing if you've got the file open in one process, and another renames another file on top of it, that could be the source of your failure to read. Or not.
|
# ? Oct 13, 2015 23:33 |
|
Open the file without FILE_SHARE_WRITE as suggested or yeah, use a named mutex for interprocess synchronization.
|
# ? Oct 13, 2015 23:44 |
|
This is a mathy question. I'm trying to get a formula for detecting if a line segment intersects with a circle. I find this http://stackoverflow.com/questions/1073336/circle-line-segment-collision-detection-algorithm This answer is pretty good http://stackoverflow.com/a/1084899 quote:Taking The issue is, wtf is t? Someone asks but no one really answers. Please, someone, explain to me the formula for finding t and what t represents. Please KoRMaK fucked around with this message at 13:42 on Oct 14, 2015 |
# ? Oct 14, 2015 04:20 |
|
I am not a great math person, but given it's a circle/sphere I'm going to take a random guess and say it's tau.
|
# ? Oct 14, 2015 05:29 |
|
The answer has a lot of algebra - I'm not sure that necessarily makes it a good answer, but whatever. What you're looking at is called a parametric equation. You're probably familiar with defining curves by expressing the relationship between x and y - "y = 4x + 2" would be an example. If you want to figure out the points on the curve, you look at all the x values in the domain and work out the corresponding y values. But that's not the only way to define a curve. Parametric equations define a curve by relating both x and y to some other parameter - usually called t. You can still find points on the curve in a similar way, though - just pick a value of t in the domain, and then evaluate the equations to find the corresponding x and y values. In your example, the answer uses parametric equations for the line segment (it should be clear that choosing values of t gives you x,y coordinates on the line through the start point in the direction of the end point). The whole point of the algebra is to find an equation that you can solve for t, which you can then plug back in to the equation of the line to get the x,y coordinates you're after. As for why it does that? It makes it a lot easier to generalise to higher dimensions, different shapes, etc. - you just follow exactly the same process, maybe with bigger vectors or a different equation for what you're matching against. If you're just looking to match a circle with a line segment in two dimensions, there are plenty of more straightforward solutions.
|
# ? Oct 14, 2015 05:42 |
|
t is "time". That answer is taken from raytracing, where you want to know not only whether the ray will hit the sphere, but which side it will hit, and whether it might have hit something else in the scene first. For example, suppose you solve the intersection equations for three different spheres and a plane. The equations for a sphere/circle intersection are quadratic, so you'll get two (usually distinct) answers. Suppose the solutions are: for sphere A, t is -1 or 6; for sphere B, t is 2 or 4; for sphere C, t is 2+3i or 2-3i; for the plane, t is 7. So the ray won't hit sphere C at all, and it will hit sphere B at time 2, sphere A at time 6, and the plane at time 7 — which is to say, it will hit sphere B and not reach the others (unless the sphere is transparent and non-refractive). The t=-1 result says that the ray would hit sphere A if it were moving in the opposite direction (and together with the t=6 result, that implies that the ray source must be within the sphere).
|
# ? Oct 14, 2015 06:40 |
|
KoRMaK posted:This is a mathy question. I'm trying to get a formula for detecting if a line segment intersects with a circle. I find this http://stackoverflow.com/questions/1073336/circle-line-segment-collision-detection-algorithm "d" is a normalized (length = 1) vector, so "t" is the distance from E to wherever you are hitting. BTW, are you checking agains a CIRCLE or against a SPHERE? The solution you linked works with a sphere, not a circle (as in 2-d circle).
|
# ? Oct 14, 2015 12:34 |
|
I'm working with circles right now. Some day I'd like to move into sphere, but I would think I just have to add the z co-ord. Either way, let just focus on 2d circles for now.Jabor posted:(it should be clear that choosing values of t gives you x,y coordinates on the line through the start point in the direction of the end point). It's not . But if I understand what you are saying, it means I can pick a value for t and it gives me an x,y on the line. is t a vector or a scalar? It's hard to determine because they switch between uppercase meaning vector and lowercase meaning scalar and vector (d is a vector). The answers you guys all gave seem to be different, so I am confused. The closest one I can find accessible is Jabor posted:The answer has a lot of algebra - I'm not sure that necessarily makes it a good answer, but whatever. Because P is later defined as Px = Ex + tdx tx = (Px - Ex)/dx (right?) Is this tx or just t? If I do the same for the y equation, would t be the same value? Py = Ey + tdy tx = (Py - Ey)/dy tx == ty ????? Or, if someone could just hook me up with a c# function that takes a circle and a line segment and does the math for me we can just forget any of this ever happened. Maybe this will work http://csharphelper.com/blog/2014/09/determine-where-a-line-intersects-a-circle-in-c/
|
# ? Oct 14, 2015 14:38 |
|
baby puzzle posted:I need to know how safe it is to write to one file from multiple processes. https://msdn.microsoft.com/en-us/library/windows/desktop/aa363858%28v=vs.85%29.aspx with dwShareMode of 0? Then one of those processes will get an error if it tries to open the file.
|
# ? Oct 14, 2015 17:18 |
|
KoRMaK posted:I'm working with circles right now. Some day I'd like to move into sphere, but I would think I just have to add the z co-ord. Either way, let just focus on 2d circles for now. Ok, that makes the thing easier. Let's define the circle: Center at C Radius r And the straight line: Origin at E direction as D C and E are coordinates (x,y). D is a normalized vector (Dx,Dy). Since it is normalized, mod(D) = 1 Hence, the parametric equations for the circle and the line are: Circle: (x-Cx)^2 + (y-Cy)^2 = r (1) Line: (x,y) = E + t·D (in vectorial form), or: x = Ex + t·Dx (2a) y = Ey + t·Dy (2b) Now, to find if the line intersects the circle, substitute 2a and 2b into 1 and *solve the resulting equation for 't'*. The resulting equation is quadratic, so you can have the following situations: - There are no real roots for the equation: there is no intersection between the line and the circle. - You have two equal real roots: the line is *tangent* to the circle. - You have two different real roots: the line *crosses* the circle. The sign of the real roots show if the intersection is in front of E (according to the direction of D) in case the root is positive, or behind E if the root is negative. If you get one root positive and the other negative, then the origin of the line is *inside* the circle. Does this make sense? English is not my native language, and I'm not confortable with math terminology, so perhaps I've made some gross mistake. E: Oh, and if you want the cartesian coordinates of the intersection points, just apply the equation for the line. For a 't' root: intersection = E + t · D
|
# ? Oct 14, 2015 18:49 |
|
Is t a scalar or vector?
|
# ? Oct 14, 2015 19:15 |
|
KoRMaK posted:Is t a scalar or vector? It is a scalar. A real number. I see the 'dot' may have confused you.
|
# ? Oct 14, 2015 19:31 |
|
Ok cool thank you. I think you explained it well, your English came across just fine! Now its up to me to digest this stuff - I think I have all the info I need to derive the facts from it. Thanks everyone! I found this site which I'll use to help me visualize this stuff https://www.desmos.com/calculator/zs5wfg9uem
|
# ? Oct 14, 2015 19:41 |
Who at a software company is typically responsible for backporting code changes to older (but still maintained) code branches? Is it the developer making the changes themselves? Some other release engineer type role? I'm thinking maybe it has to be the developer, in case the changes don't merge cleanly with the older branches. Not really sure though.
|
|
# ? Oct 14, 2015 20:14 |
|
fletcher posted:Who at a software company is typically responsible for backporting code changes to older (but still maintained) code branches? Is it the developer making the changes themselves? Some other release engineer type role? I'm thinking maybe it has to be the developer, in case the changes don't merge cleanly with the older branches. Not really sure though. It totally depends on the company. For us it's typically whoever made the change unless the changes are easy enough that a newbie could update the older branches using current as a reference.
|
# ? Oct 14, 2015 20:23 |
|
This is a dumb question I'm sure.. I'm going through my old c# book I never finished and one exercise is: create a program that prompts a user for input, accepts an int, then evaluates if the input is odd, even, multiple of 10, or > 100 using if statements. I did this part fine, but the next exercise says to do it with a switch statement. Not sure how to do that, as switch is to compare strings and boolean ? (Sorry phone posting)
|
# ? Oct 14, 2015 23:27 |
|
Danith posted:This is a dumb question I'm sure.. I'm going through my old c# book I never finished and one exercise is: create a program that prompts a user for input, accepts an int, then evaluates if the input is odd, even, multiple of 10, or > 100 using if statements. I did this part fine, but the next exercise says to do it with a switch statement. Not sure how to do that, as switch is to compare strings and boolean ? (Sorry phone posting) obviously convert the int to a string. Haha. I think I'm serious, it is c#.
|
# ? Oct 14, 2015 23:35 |
|
Danith posted:This is a dumb question I'm sure.. I'm going through my old c# book I never finished and one exercise is: create a program that prompts a user for input, accepts an int, then evaluates if the input is odd, even, multiple of 10, or > 100 using if statements. I did this part fine, but the next exercise says to do it with a switch statement. Not sure how to do that, as switch is to compare strings and boolean ? (Sorry phone posting) C# code:
|
# ? Oct 15, 2015 01:39 |
|
Danith posted:This is a dumb question I'm sure.. I'm going through my old c# book I never finished and one exercise is: create a program that prompts a user for input, accepts an int, then evaluates if the input is odd, even, multiple of 10, or > 100 using if statements. I did this part fine, but the next exercise says to do it with a switch statement. Not sure how to do that, as switch is to compare strings and boolean ? (Sorry phone posting) I don't know c#, but if you want an 'absolutely do NOT do this under any conditions on grounds of being screamed at' solution , I bet you can do code:
|
# ? Oct 15, 2015 01:58 |
|
fritz posted:I don't know c#, but if you want an 'absolutely do NOT do this under any conditions on grounds of being screamed at' solution , I bet you can do If you're going to do this, you may as well just go all-in on the bit operations and use bit-shifts and bit-wise | operators instead of the multiply-then-add. In fact, I'd wager this is probably the intended solution (though you probably shouldn't do it). KoRMaK posted:obviously convert the int to a string. Haha. You could actually do this, it is extremely evil: code:
|
# ? Oct 15, 2015 02:09 |
|
Or just rewrite if (cond) { x(); } else { y(); } as switch(cond) { case true: x(); break; case false: y(); }
|
# ? Oct 15, 2015 02:15 |
|
b0lt posted:Or just rewrite if (cond) { x(); } else { y(); } as switch(cond) { case true: x(); break; case false: y(); } The question said a switch statement, so I think they wanted a single one that did the job.
|
# ? Oct 15, 2015 02:18 |
|
Jsor posted:If you're going to do this, you may as well just go all-in on the bit operations and use bit-shifts and bit-wise | operators instead of the multiply-then-add. In fact, I'd wager this is probably the intended solution (though you probably shouldn't do it). This is the best answer because the prof will have to spend at least 7 minutes evaluating its correctness. They will be irritated and then intrigued and satisfied.
|
# ? Oct 15, 2015 04:51 |
|
The best part is I actually handle the "10 and not 2" case, and then declared it unreachable later (granted the unreachable block is necessary for it to compile, just not for the commented reason).
|
# ? Oct 15, 2015 04:56 |
|
This is an awful assignment and you should never write code like any of this and the professor's absolute best quality is that they haven't driven you to throw your computer out a window yet.
|
# ? Oct 15, 2015 07:23 |
|
Here is a fun question. I'm doing research where performance really matters. That being said, I really like throwing together a prototype of everything I write, to discover issues with my high-level design. For now I'm using the Julia language for prototypes, and it is pretty good. I am wondering however, is there a better language I should learn for prototyping, considering that Julia has a few gnarly things that don't quite map to C cleanly. I would like my prototypes to be primarily quick and fun to write and not awful in performance. Any suggestions? I'm ready to learn any language, this is a fun thing. Comedy options may be entertained.
|
# ? Oct 15, 2015 08:04 |
|
Python with NumPy is the obvious suggestion.
|
# ? Oct 15, 2015 15:09 |
|
What exactly is the performance critical part of your prototype? Obviously C/C++ are pretty easy to optimize on most platforms due to their ability to map closely to assembly.
|
# ? Oct 15, 2015 16:43 |
|
Sex Bumbo posted:What exactly is the performance critical part of your prototype? Obviously C/C++ are pretty easy to optimize on most platforms due to their ability to map closely to assembly. Yeah, and a C program will be the final deliverable. Problem being that C isn't exactly fun to prototype in, when things are very much in flux. It is the prototyping language I'm looking for. The critical part is kinda goofy. There are components which are engaged in a back-and-forth calling. A calls B returns to A calls C returns to A calls D returns to A loops and calls B ... B, C and D are teensy, but need to be separate from A. The idea is a proof-of-concept for a program where A is held constant and people can just write the code for B, C and D separately. In the final product the files with the B, C and D functions would be #included into A. Then A can be written by an expert and any noob can just plug in their B, C and D and still have good stuff happen and fast. Inlining is critical as B, C and D are just usually going to be one or two lines long, a singly-nested loop at worst.
|
# ? Oct 15, 2015 20:09 |
|
GrumpyDoctor posted:Python with NumPy is the obvious suggestion. This, along with iPython notebooks, is my go-to for prototyping now. But it's a massively different problem than the one you're solving. What risk areas is your prototype going to mitigate? Strictly the interface from A to the subsystems or the difficult internals of A?
|
# ? Oct 15, 2015 22:41 |
|
I need to write a program for a device that I've created that needs to do a few things: First of all I need to communicate over USB and send/receive data (doesn't have to be blisteringly fast, maybe a packet every 2-3 ms). I tried using C++ but the examples for the WinUSB driver are a bit over my head. I can find the device but not do much else, and there doesn't seem to be a lot of help around for it. Needs to have a UI. I need to be able to plot the data in real time and be able to zoom in/out with it as well as having a few other buttons to change setting on the device. Need to be able to create something like a .exe file that is easily used by other people (this device will be for sale and this software will be included). I am more experience with embedded programming so I'm familiar with C/C++ although like I said all the windows stuff was way over my head with the WinUSB examples. I've been looking at Python and it seems that it may suit my needs. I've seen that there's a library for easy USB integration (PyUSB) and matplotlib for doing graph plotting. Can these be used together with something else to make a workable UI? I am open to learning whatever I need to as long as I can make sure that the end product will do what I need it to do.
|
# ? Oct 16, 2015 00:01 |
|
JawnV6 posted:This, along with iPython notebooks, is my go-to for prototyping now. But it's a massively different problem than the one you're solving. The interface, yeah. Usually A and the others are in one piece, but this sucks because the user always has to write their own A. The A's internals are nailed down and solid, just a matter of finding a clean line separating A from the rest of the system so that the user can be an idiot and just write the domain-specific parts.
|
# ? Oct 16, 2015 00:43 |
|
|
# ? May 18, 2024 12:14 |
|
Has anyone written anything of great interest using Rust yet? Is Atom/Sublime the new vi/emacs war?
|
# ? Oct 16, 2015 03:30 |