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.
 
  • Locked thread
Shaggar
Apr 26, 2006

GameCube posted:

hey you terrible freaking programmers. if i've got a semaphore and one thread has been waiting on it for five minutes and then another thread comes along and waits on it the new thread might get the semaphore before the old one does. that sucks. how do i make it not do that. thanks in advance for answering my question about programming

use a queue

Adbot
ADBOT LOVES YOU

Jabor
Jul 16, 2010

#1 Loser at SpaceChem

GameCube posted:

hey you terrible freaking programmers. if i've got a semaphore and one thread has been waiting on it for five minutes and then another thread comes along and waits on it the new thread might get the semaphore before the old one does. that sucks. how do i make it not do that. thanks in advance for answering my question about programming

the reason it does that is because making it strictly ordered makes it way slower (and is in most cases unnecessary).

if you really do want strict queueing behaviour, use a higher-level synchronization mechanism that offers that.

hobbesmaster
Jan 28, 2008

GameCube posted:

hey you terrible freaking programmers. if i've got a semaphore and one thread has been waiting on it for five minutes and then another thread comes along and waits on it the new thread might get the semaphore before the old one does. that sucks. how do i make it not do that. thanks in advance for answering my question about programming

you need to implement a queue for the semaphore. there may be a function for this already or you may have to implement it yourself, you didn't say what environment you're in. java and c# call it a blockingqueue. if you have an event system you can also just put something into its message queue

GameCube
Nov 21, 2006

all i wanna do is make sure our requests to some freakin "rest" (NOT) api are sent in order. but i think they're all being sent from the same thread anyway. i don't get this c# async crap AT ALL. gotta read some more cleary

jony neuemonic
Nov 13, 2009

Shaggar posted:

functional languages are bad

i also prefer non-functional languages.

easier to hide the terrible programmerness.

GameCube
Nov 21, 2006

i'm just gonna try some poo poo in debug and see what happens.

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder

GameCube posted:

all i wanna do is make sure our requests to some freakin "rest" (NOT) api are sent in order. but i think they're all being sent from the same thread anyway. i don't get this c# async crap AT ALL. gotta read some more cleary

you want worker threads reading from a queue

but if you actually want to guarantee that the requests are made in order, all of the requests need to happen in a single thread, because you can't guarantee that one worker won't pop a request off the queue and dispatch it before a previous request is dispatched.

if you just want them to be mostly in order though, do a queue with worker threads

the queue probably needs to be threadsafe or something idk

DONT THREAD ON ME fucked around with this message at 17:34 on Jul 6, 2016

prefect
Sep 11, 2001

No one, Woodhouse.
No one.




Dead Man’s Band

comedyblissoption posted:

this is the proper formatting for 4-space indentation
code:
int    x = 0;
double y = 0;

foo.callSomeFunc(
    x,
    y,
    z,
);

LordSaturn
Aug 12, 2007

sadly unfunny

GameCube posted:

hey you terrible freaking programmers. if i've got a semaphore and one thread has been waiting on it for five minutes and then another thread comes along and waits on it the new thread might get the semaphore before the old one does. that sucks. how do i make it not do that. thanks in advance for answering my question about programming

I actually thought posix semaphores already did this. rip me I guess

GameCube
Nov 21, 2006

MALE SHOEGAZE posted:

you want worker threads reading from a queue

but if you actually want to guarantee that the requests are made in order, all of the requests need to happen in a single thread, because you can't guarantee that one worker won't pop a request off the queue and dispatch it before a previous request is dispatched.

if you just want them to be mostly in order though, do a queue with worker threads

the queue probably needs to be threadsafe or something idk

i'm tryin to avoid adding threads entirely here. c# motherfucker. async all the way. my first implementation had a single task that read the stuff from the queue and sent it but gently caress that. async.

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder
o

i don't really understand then

CPColin
Sep 9, 2003

Big ol' smile.
Java's Semaphore class supports FIFO behavior:

http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Semaphore.html#Semaphore-int-boolean-

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder
i rly like makefiles

HoboMan
Nov 4, 2010

GameCube posted:

i'm tryin to avoid adding threads entirely here. c# motherfucker. async all the way. my first implementation had a single task that read the stuff from the queue and sent it but gently caress that. async.

but like, do you need the async?

also what is your problem exactly?
are you doing this kind of thing?
C# code:
async void func1() {
	string poo = await func3();
	// do things with poo
}

async void func2() {
	string fart = await func3();
	// do things with fart
}

string func3() {
	return "poot"
}

void main() {
	func1();
	func2();
}
but sometimes func2 finishes first?

GameCube
Nov 21, 2006

HoboMan posted:

but like, do you need the async?

also what is your problem exactly?
are you doing this kind of thing?
C# code:
async void func1() {
	string poo = await func3();
	// do things with poo
}

async void func2() {
	string fart = await func3();
	// do things with fart
}

string func3() {
	return "poot"
}

void main() {
	func1();
	func2();
}
but sometimes func2 finishes first?

yeah but i just assumed that maybe sometimes func2 would finish first if i didn't put something in to prevent it. now i'm realizing that might not actually happen, so that's what i'm gonna test out

NihilCredo
Jun 6, 2011

iram omni possibili modo preme:
plus una illa te diffamabit, quam multæ virtutes commendabunt

btw the framework has a thread safe queue class you can use, system.collections.concurrent.concurrentqueue

VikingofRock
Aug 24, 2008




MALE SHOEGAZE posted:

i rly like makefiles

:eyepop:

VikingofRock
Aug 24, 2008




gonadic io posted:

here's how I'd implement a Fischer-Yates shuffle.

This is really good; thanks. Personally I'm cool with using flip here, since evalRandT only takes two arguments (making flip easy to reason about) and I think the increased brevity makes this a little more readable then the alternative ways of writing it. What don't you like about flip in situations like this, and what would you prefer?

oh no blimp issue
Feb 23, 2011

i saw some contractor code that did function signatures like this today:
code:
butts
(
    poop a,
    poop b
){
    toilet()
}

Valeyard
Mar 30, 2012


Grimey Drawer

MALE SHOEGAZE posted:

i rly like makefiles

Maven is actually an acronym for Makefile Ant Versioning Eclipse Null

gonadic io
Feb 16, 2011

>>=

VikingofRock posted:

This is really good; thanks. Personally I'm cool with using flip here, since evalRandT only takes two arguments (making flip easy to reason about) and I think the increased brevity makes this a little more readable then the alternative ways of writing it. What don't you like about flip in situations like this, and what would you prefer?

Using flip and an inline do-block here makes it harder to refactor, doesn't break it up in to manageable named chunks, encourages you to be "clever" with lambdas, exactly like I did for the loop etc. It's just the difference between hobby vs industry programming - in a personal project I wouldn't think twice about it.

The downside of splitting it up (even just into the 3 parts like you had) is that the type inference fails so you have to write an explicit signature on the outer do block (which you called shuffled).

This signature is "forall s. RandT StdGen (ST s) (Vector a)". (honestly this was a major reason why I did inline it).

Soricidus
Oct 21, 2010
freedom-hating statist shill
I like the idea of makefiles, but the reality is just horrid

Shaggar
Apr 26, 2006

GameCube posted:

i'm tryin to avoid adding threads entirely here. c# motherfucker. async all the way. my first implementation had a single task that read the stuff from the queue and sent it but gently caress that. async.

if your code is like

C# code:
var result1 = await client.DoRequestAsync();
var result2 = await client.DoRequestAsync();
iirc the execution order of the awaited calls is not guaranteed, it just guarantees that both requests will be done before their results are used. async/await is fancy syntax for futures/callbacks.

if you need these to be in order then its not a candidate for async/await. You have a few options

Option 1: Run the requests synchronously.
C# code:
var result1 = client.DoRequestAsync().Wait();
var result2 = client.DoRequestAsync().Wait();
If you have work you want to do between request1 and request2 while waiting for request1 to finish but before request2 runs, then do one of these:

Option 2: explicit Synch after do stuff

C# code:
var task1 = client.DoRequestAsync();
//do stuff
var result1 = task1.Wait();
var result2 = client.DoRequestAsync().Wait();
Option 3: async w/ implicit sync call when result1 is accessed

C# code:
var result1 = await client.DoRequestAsync();
//do stuff
var butt = result1.Butt; 
var result2 = client.DoRequestAsync().Wait();
personally I find that option 2 there is more readable and is the way to go unless the stuff in //do stuff is heavy work that can be done in parallel.

Shaggar fucked around with this message at 19:40 on Jul 6, 2016

Shaggar
Apr 26, 2006

Soricidus posted:

I like the idea of makefiles, but the reality is just horrid

the concept behind makefiles is totally flawed and build scripts are retarded crap for idiots. you want a build definition that is handled by the build tool and its plugins.

Shaggar
Apr 26, 2006
basically you want maven. its the only good build tool and everything else is garbage.

GameCube
Nov 21, 2006

Shaggar posted:

if your code is like

C# code:
var result1 = await client.DoRequestAsync();
var result2 = await client.DoRequestAsync();
iirc the execution order of the awaited calls is not guaranteed, it just guarantees that both requests will be done before their results are used. async/await is fancy syntax for futures/callbacks.

if you need these to be in order then its not a candidate for async/await. You have a few options

Option 1: Run the requests synchronously.
C# code:
var result1 = client.DoRequestAsync().Wait();
var result2 = client.DoRequestAsync().Wait();
If you have work you want to do between request1 and request2 while waiting for request1 to finish but before request2 runs, then do one of these:

Option 2: explicit Synch after do stuff

C# code:
var task1 = client.DoRequestAsync();
//do stuff
var result1 = task1.Wait();
var result2 = client.DoRequestAsync().Wait();
Option 3: async w/ implicit sync call when result1 is accessed

C# code:
var result1 = await client.DoRequestAsync();
//do stuff
var butt = result1.Butt; 
var result2 = client.DoRequestAsync().Wait();
personally I find that option 2 there is more readable and is the way to go unless the stuff in //do stuff is heavy work that can be done in parallel.

.Wait() is verboten so option 2 it is

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder

Soricidus posted:

I like the idea of makefiles, but the reality is just horrid

i don't actually use them to build things, I just like putting all of my dev tasks in there. starting containers, kicking off the actual build script, building docs, git push -f, etc

i also put whatever scripts jenkins needs to run in my make file so all my jenkins jobs need to do is make foo instead of a bunch of garbage bash typed into a browser form

DONT THREAD ON ME fucked around with this message at 20:00 on Jul 6, 2016

Shaggar
Apr 26, 2006
I just wanna note that

C# code:
var result1 = await client.DoRequestAsync();
var result2 = await client.DoRequestAsync();
and

C# code:
var task1 = client.DoRequestAsync();
//do stuff

var result1 = await task1;
var result2 = await client.DoRequestAsync();
are equivalent in terms of async request ordering.

If you want to force a specific order you have to wait for the result1 at some point

GameCube
Nov 21, 2006

Shaggar posted:

I just wanna note that

C# code:
var result1 = await client.DoRequestAsync();
var result2 = await client.DoRequestAsync();
and

C# code:
var task1 = client.DoRequestAsync();
//do stuff

var result1 = await task1;
var result2 = await client.DoRequestAsync();
are equivalent in terms of async request ordering.

If you want to force a specific order you have to wait for the result1 at some point

well in three days the autistic guy is moving to a different team so maybe i'll just Wait() til then. little programmer humor there

oh no blimp issue
Feb 23, 2011

what does he have against wait()?

HoboMan
Nov 4, 2010

"People fear what they don't understand and hate what they can't conquer."

Shaggar
Apr 26, 2006
actually you know what I think I'm wrong here.

C# code:
var result1 = await client.DoRequestAsync();
var result2 = await client.DoRequestAsync();
should execute in order. its

C# code:
var task1 = client.DoRequestAsync();
var task2 = client.DoRequestAsync();
that might not.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

gonadic io posted:

have the threads 1) mine bitcoins while idling and 2) use those bitcoins to incentivise the semaphore to serve them first

i did that and now all my other threads that were not using semaphores before are grabbing them and holding on to them until i pay them

GameCube
Nov 21, 2006

Shaggar posted:

actually you know what I think I'm wrong here.

C# code:

var result1 = await client.DoRequestAsync();
var result2 = await client.DoRequestAsync();
should execute in order. its

C# code:

var task1 = client.DoRequestAsync();
var task2 = client.DoRequestAsync();

that might not.

oh. that would make things a fuckload simpler

gonadic io
Feb 16, 2011

>>=

Wheany posted:

i did that and now all my other threads that were not using semaphores before are grabbing them and holding on to them until i pay them

free market, bitch

HoboMan
Nov 4, 2010

this is probably pretty simple but speaking of asynchronous operations i got a problem for it
i want to make a collector type thing where multiple threads are by adding items to a queue for another thread to consume them as they come in
as a java dev I've never been expected to make anything fast so this is new to me (also i'm doing this in c#)

HoboMan fucked around with this message at 21:29 on Jul 6, 2016

Lutha Mahtin
Oct 10, 2010

Your brokebrain sin is absolved...go and shitpost no more!

gonadic io posted:

free market, bitch

lockchain technology :v:

also hoboman there is probably an exact class or 2 that you can use to make that. don't ask me what they are. (because i do not know).

HoboMan
Nov 4, 2010

Lutha Mahtin posted:

lockchain technology :v:

HoboMan
Nov 4, 2010

well I think i accidentally started making a test harness for our webforms app

this is hard

Adbot
ADBOT LOVES YOU

kitten emergency
Jan 13, 2008

get meow this wack-ass crystal prison

Shaggar posted:

actually you know what I think I'm wrong here.

C# code:

var result1 = await client.DoRequestAsync();
var result2 = await client.DoRequestAsync();
should execute in order. its

C# code:

var task1 = client.DoRequestAsync();
var task2 = client.DoRequestAsync();

that might not.

yes, await blocks on the caller

this tripped me up a while ago

  • Locked thread