Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
baby puzzle
Jun 3, 2011

I'll Sequence your Storm.
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.

Adbot
ADBOT LOVES YOU

Skandranon
Sep 6, 2008
fucking stupid, dont listen to me
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.

baby puzzle
Jun 3, 2011

I'll Sequence your Storm.
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.

nielsm
Jun 1, 2009



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.)

Skandranon
Sep 6, 2008
fucking stupid, dont listen to me

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.

sarehu
Apr 20, 2007

(call/cc call/cc)

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.

baby puzzle
Jun 3, 2011

I'll Sequence your Storm.

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

sarehu
Apr 20, 2007

(call/cc call/cc)
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.

omeg
Sep 3, 2012

Open the file without FILE_SHARE_WRITE as suggested or yeah, use a named mutex for interprocess synchronization.

KoRMaK
Jul 31, 2012



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

E is the starting point of the ray,
L is the end point of the ray,
C is the center of sphere you're testing against
r is the radius of that sphere
Compute:
d = L - E ( Direction vector of ray, from start to end )
f = E - C ( Vector from center sphere to ray start )

Then the intersection is found by..
Plugging:
P = E + t * d
This is a parametric equation:
Px = Ex + tdx
Py = Ey + tdy
into
(x - h)2 + (y - k)2 = r2
(h,k) = center of circle.

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

The MUMPSorceress
Jan 6, 2012


^SHTPSTS

Gary’s Answer
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.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
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.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
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).

Amberskin
Dec 22, 2013

We come in peace! Legit!

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


This answer is pretty good http://stackoverflow.com/a/1084899


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

"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).

KoRMaK
Jul 31, 2012



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 :smith:. 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.

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.

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/

feedmegin
Jul 30, 2008

baby puzzle posted:

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.

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.

Amberskin
Dec 22, 2013

We come in peace! Legit!

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

KoRMaK
Jul 31, 2012



Is t a scalar or vector?

Amberskin
Dec 22, 2013

We come in peace! Legit!

KoRMaK posted:

Is t a scalar or vector?

It is a scalar. A real number. I see the 'dot' may have confused you.

KoRMaK
Jul 31, 2012



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

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
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.

The MUMPSorceress
Jan 6, 2012


^SHTPSTS

Gary’s Answer

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.

Danith
May 20, 2006
I've lurked here for years
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)

KoRMaK
Jul 31, 2012



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#.

Sedro
Dec 31, 2008

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:
switch (x) {
  ...
  case -1: // odd
  case 0: // even, multiple of 10
  case 1: // odd
  case 2: // even
  case 3: // odd
  ... and so on
}

fritz
Jul 26, 2003

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:
int parity(int x){ return x&1;}
int multiple10(int x) { return !(x%10);}
int bigger100(int x) { return (x > 100);}

// etc
int y = (bigger100(x)*4 + multiple10(x)*2 + parity(x);
switch (y) {
case 0: // odd and under 100 
case 1: // even, under 100, not multiple of 10
case 3: // multiple of 10 under 100
case 4: // odd, over 100
case 5: // even, not multiple of 10, over 100
case 7: // multiple of 10, over 100
}

Linear Zoetrope
Nov 28, 2011

A hero must cook

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

code:
int parity(int x){ return x&1;}
int multiple10(int x) { return !(x%10);}
int bigger100(int x) { return (x > 100);}

// etc
int y = (bigger100(x)*4 + multiple10(x)*2 + parity(x);
switch (y) {
case 0: // odd and under 100 
case 1: // even, under 100, not multiple of 10
case 3: // multiple of 10 under 100
case 4: // odd, over 100
case 5: // even, not multiple of 10, over 100
case 7: // multiple of 10, over 100
}

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.

I think I'm serious, it is c#.

You could actually do this, it is extremely evil:

code:
String lowRentTuple = (x%2 == 0).ToString() + "," + (x%10 == 0).ToString() + "," + (x > 100).ToString();

// Badly emulate a pattern match expression in a language you have no business doing this in
switch(lowRentTuple) {
    case "False,True,True":
    case "True,True,True":  
         return "x is a multiple of 10 greater than 100";
    
    case "True,True,False":
    case "False,True,False":
        return "x is a multiple of 10 less than or equal to 100";

    case "True,False,True":
        return "x is a multiple of 2 (but not 10) greater than 100";

    case "True,False,False":
        return "x is a multiple of 2 (but not 10) less than or equal to 100";

    case "False,False,True":
        return "x is odd and greater than 100";

    case "False,False,False":
        return "x is odd and less than or equal to 100";

    default: // unreachable, a number can't be a multiple of 10 and not 2
        return "unreachable!";
}
It's basically just a worse version of the bit-field solution.

b0lt
Apr 29, 2005
Or just rewrite if (cond) { x(); } else { y(); } as switch(cond) { case true: x(); break; case false: y(); }

Linear Zoetrope
Nov 28, 2011

A hero must cook

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.

KoRMaK
Jul 31, 2012



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).


You could actually do this, it is extremely evil:

code:
String lowRentTuple = (x%2 == 0).ToString() + "," + (x%10 == 0).ToString() + "," + (x > 100).ToString();

// Badly emulate a pattern match expression in a language you have no business doing this in
switch(lowRentTuple) {
    case "False,True,True":
    case "True,True,True":  
         return "x is a multiple of 10 greater than 100";
    
    case "True,True,False":
    case "False,True,False":
        return "x is a multiple of 10 less than or equal to 100";

    case "True,False,True":
        return "x is a multiple of 2 (but not 10) greater than 100";

    case "True,False,False":
        return "x is a multiple of 2 (but not 10) less than or equal to 100";

    case "False,False,True":
        return "x is odd and greater than 100";

    case "False,False,False":
        return "x is odd and less than or equal to 100";

    default: // unreachable, a number can't be a multiple of 10 and not 2
        return "unreachable!";
}
It's basically just a worse version of the bit-field solution.

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.

Linear Zoetrope
Nov 28, 2011

A hero must cook
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). :downs:

Sex Bumbo
Aug 14, 2004
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.

No Gravitas
Jun 12, 2013

by FactsAreUseless
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.

raminasi
Jan 25, 2005

a last drink with no ice
Python with NumPy is the obvious suggestion.

Sex Bumbo
Aug 14, 2004
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.

No Gravitas
Jun 12, 2013

by FactsAreUseless

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.

JawnV6
Jul 4, 2004

So hot ...

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?

Woolwich Bagnet
Apr 27, 2003



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.

No Gravitas
Jun 12, 2013

by FactsAreUseless

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.

What risk areas is your prototype going to mitigate? Strictly the interface from A to the subsystems or the difficult internals of A?

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.

Adbot
ADBOT LOVES YOU

Hadlock
Nov 9, 2004

Has anyone written anything of great interest using Rust yet?

Is Atom/Sublime the new vi/emacs war?

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply