Coding: Fleeting Thoughts

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

Re: Coding: Fleeting Thoughts

Postby Xeio » Tue Nov 29, 2011 10:12 pm UTC

Lies! MM is months, mm is minutes. :P
User avatar
Xeio
Friends, Faidites, Countrymen
 
Posts: 4411
Joined: Wed Jul 25, 2007 11:12 am UTC
Location: C:\Users\Xeio\

Re: Coding: Fleeting Thoughts

Postby phlip » Tue Nov 29, 2011 11:39 pm UTC

+%Y%m%d-%H%M%S
While no one overhear you quickly tell me not cow cow.
but how about watch phone?
User avatar
phlip
Restorer of Worlds
 
Posts: 6739
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia

Re: Coding: Fleeting Thoughts

Postby Rysto » Thu Dec 01, 2011 2:05 am UTC

EvanED wrote:The other is to use macro hackery. If you define your implementation in a header file, you can arrange things so that something like
Code: Select all
#define HEAP_DATA_TYPE double
#include "my_heap.h"

(I suggest actually undefining HEAP_DATA_TYPE in my_heap.h, but that's personal preference. There is a reason though -- it seems like it would reduce the risk of including my_heap.h with the wrong type.)

I think the latter option sucks less, but that's me. (It means that merely the implementation of your heap is terrible, whereas with a void* interface, the use of it is as well.)

Lame. This is how you implement a type-safe, generic data structure in C.
Rysto
 
Posts: 1420
Joined: Wed Mar 21, 2007 4:07 am UTC

Re: Coding: Fleeting Thoughts

Postby EvanED » Thu Dec 01, 2011 5:27 am UTC

Rysto wrote:
EvanED wrote:The other is to use macro hackery. If you define your implementation in a header file, you can arrange things so that something like
Code: Select all
#define HEAP_DATA_TYPE double
#include "my_heap.h"

(I suggest actually undefining HEAP_DATA_TYPE in my_heap.h, but that's personal preference. There is a reason though -- it seems like it would reduce the risk of including my_heap.h with the wrong type.)

I think the latter option sucks less, but that's me. (It means that merely the implementation of your heap is terrible, whereas with a void* interface, the use of it is as well.)

Lame. This is how you implement a type-safe, generic data structure in C.

Ugh, having worked on code that used macros like that (and having worked on the macros themselves), my way is way better.

Using macros like that is horrendous on several different levels.
EvanED
 
Posts: 3767
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI

Re: Coding: Fleeting Thoughts

Postby phlip » Fri Dec 02, 2011 6:10 am UTC

OK, that's possibly one of the worse bugs I've had.

A while ago, when I set up Cygwin on my Windows boot, I found that it didn't come with a "killall" utility... just the usual "kill" that works by PID. So I built my own bare-bones killall using kill and pidof. Except that Cygwin didn't have pidof, either, so I built my own bare-bones one using which, ps and grep.

The things I learned, using this script today:
  • "set -e" is my friend, and I should use it more often in these small scripts (where I don't intend to do proper error handling).
  • If I pass a non-existant program to my hacked pidof, a bug caused by cascading errors (that set -e would catch) results in it returning the PID of every active process.
While no one overhear you quickly tell me not cow cow.
but how about watch phone?
User avatar
phlip
Restorer of Worlds
 
Posts: 6739
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia

Re: Coding: Fleeting Thoughts

Postby headprogrammingczar » Fri Dec 02, 2011 1:33 pm UTC

I think the moral of that story is "you should be running a virtual linux OS instead"...
<quintopia> You're not crazy. you're the goddamn headprogrammingspock!
<Weeks> You're the goddamn headprogrammingspock!
<Cheese> I love you
User avatar
headprogrammingczar
 
Posts: 2953
Joined: Mon Oct 22, 2007 5:28 pm UTC
Location: Beaming you up

Re: Coding: Fleeting Thoughts

Postby EvanED » Fri Dec 02, 2011 1:48 pm UTC

Also: never run killall on a SunOS box. :-)
EvanED
 
Posts: 3767
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI

Re: Coding: Fleeting Thoughts

Postby maafy6 » Fri Dec 02, 2011 9:22 pm UTC

EvanED wrote:Also: never run a SunOS box. :-)


FTFY

Granted, I probably have an irrational hatred of SunOS thanks to being forced to work on a system on Solaris 8 at work. So happy with that.

So very happy.
maafy6
 
Posts: 102
Joined: Wed Aug 22, 2007 10:43 pm UTC

Re: Coding: Fleeting Thoughts

Postby rath358 » Mon Dec 12, 2011 3:54 pm UTC

Thought of a maybe slightly better than brute force way to do that huge find the highest possible sum path through the triangle problem from project Euler. A shame I have real-world things like finals to worry about...
TEAM SHIVAHN
Pretty much the best team ever

Red Hal wrote:If you can't tick all the boxes then you don't have privilege! Privilege; it's a multiple-input AND gate!
User avatar
rath358
Marzipan's Understudy
 
Posts: 793
Joined: Wed Jan 14, 2009 6:02 am UTC
Location: RPI

Re: Coding: Fleeting Thoughts

Postby RoadieRich » Mon Dec 12, 2011 6:32 pm UTC

rath358 wrote:Thought of a maybe slightly better than brute force way to do that huge find the highest possible sum path through the triangle problem from project Euler. A shame I have real-world things like finals to worry about...

There is a very neat method to solve that one. Gives you a runtime of O(n^2), where n is the number of rows. Brute force is closer to O(n!), I think.
roband wrote:Mav is a cow.

UniJam 2012: Inter-university Games Jam hosted by Nottingham Trent University DevSoc.
nlug: Nottingham Linux User Group
DevSoc: The Nottingham Trent University Software Development Society
User avatar
RoadieRich
The Black Hand
 
Posts: 1030
Joined: Tue Feb 12, 2008 11:40 am UTC
Location: Somewhere only we know

Re: Coding: Fleeting Thoughts

Postby phlip » Tue Dec 13, 2011 12:11 am UTC

RoadieRich wrote:Brute force is closer to O(n!), I think.

Not quite that bad... O(n2n) I think: there's 2n paths to check, and each one involves adding up n numbers.
While no one overhear you quickly tell me not cow cow.
but how about watch phone?
User avatar
phlip
Restorer of Worlds
 
Posts: 6739
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia

Re: Coding: Fleeting Thoughts

Postby Thesh » Tue Dec 13, 2011 5:02 am UTC

It should be O(n2n-1). There's two choices each step down, but no choice for the first row.
Eppur si mouve.
User avatar
Thesh
Has the Brain Worms, In Case You Forgot.
 
Posts: 2440
Joined: Tue Jan 12, 2010 1:55 am UTC
Location: Southern California, USA

Re: Coding: Fleeting Thoughts

Postby phlip » Tue Dec 13, 2011 5:20 am UTC

That's only different by a factor of 2, though, and big-O notation ignores those...
While no one overhear you quickly tell me not cow cow.
but how about watch phone?
User avatar
phlip
Restorer of Worlds
 
Posts: 6739
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia

Re: Coding: Fleeting Thoughts

Postby Shivahn » Tue Dec 13, 2011 7:02 am UTC

RoadieRich wrote:
rath358 wrote:Thought of a maybe slightly better than brute force way to do that huge find the highest possible sum path through the triangle problem from project Euler. A shame I have real-world things like finals to worry about...

There is a very neat method to solve that one. Gives you a runtime of O(n^2), where n is the number of rows. Brute force is closer to O(n!), I think.

I felt so satisfied when I figured that one out.
User avatar
Shivahn
 
Posts: 2144
Joined: Tue Jan 06, 2009 6:17 am UTC

Re: Coding: Fleeting Thoughts

Postby Dason » Tue Dec 13, 2011 8:11 am UTC

RoadieRich wrote:
rath358 wrote:Thought of a maybe slightly better than brute force way to do that huge find the highest possible sum path through the triangle problem from project Euler. A shame I have real-world things like finals to worry about...

There is a very neat method to solve that one. Gives you a runtime of O(n^2), where n is the number of rows. Brute force is closer to O(n!), I think.

I hadn't put too much thought into this particular problem for quite some time but your comment here helped me figure out a solution. Thanks!
double epsilon = -.0000001;
User avatar
Dason
 
Posts: 1264
Joined: Wed Dec 02, 2009 7:06 am UTC
Location: ~/

Re: Coding: Fleeting Thoughts

Postby troyp » Tue Dec 13, 2011 8:30 am UTC

Thesh wrote:It should be O(n2n-1). There's two choices each step down, but no choice for the first row.

That's actually the same thing ;-)
troyp
 
Posts: 398
Joined: Thu May 22, 2008 9:20 pm UTC
Location: Lismore, NSW

Re: Coding: Fleeting Thoughts

Postby tastelikecoke » Tue Dec 13, 2011 1:22 pm UTC

Freshie CS student here. What do you think would be a good example of a real life CS problem? I just want an idea on what you would call a practical problem, and probably try solving it :?
User avatar
tastelikecoke
 
Posts: 1117
Joined: Mon Feb 01, 2010 7:58 am UTC
Location: Antipode of Brazil

Re: Coding: Fleeting Thoughts

Postby joshz » Tue Dec 13, 2011 7:23 pm UTC

Traveling salesman has enormous practical applications and implications

So does prime factorization---it's essential for cryptography.
You, sir, name? wrote:If you have over 26 levels of nesting, you've got bigger problems ... than variable naming.
suffer-cait wrote:it might also be interesting to note here that i don't like 5 fingers. they feel too bulky.
User avatar
joshz
 
Posts: 1466
Joined: Tue Nov 11, 2008 2:51 am UTC
Location: Pittsburgh, PA

Re: Coding: Fleeting Thoughts

Postby RoadieRich » Tue Dec 13, 2011 11:40 pm UTC

tastelikecoke wrote:Freshie CS student here. What do you think would be a good example of a real life CS problem? I just want an idea on what you would call a practical problem, and probably try solving it :?

Anything that uses a datastore of some sort is applicable: do you want fast random access or sequential access, fast inserts, ultra efficient use of memory? That's tree vs list vs array.

For instance, how do you store an index of a library's books, if they're usually looked up by author? (Yes, letting a database handle it for you is the proper answer, but that's soft eng, not comp sci.)

It depends on how deep you want to go. I'm guessing "Freshie" means you've just started?
roband wrote:Mav is a cow.

UniJam 2012: Inter-university Games Jam hosted by Nottingham Trent University DevSoc.
nlug: Nottingham Linux User Group
DevSoc: The Nottingham Trent University Software Development Society
User avatar
RoadieRich
The Black Hand
 
Posts: 1030
Joined: Tue Feb 12, 2008 11:40 am UTC
Location: Somewhere only we know

Re: Coding: Fleeting Thoughts

Postby tastelikecoke » Wed Dec 14, 2011 8:31 am UTC

RoadieRich wrote:It depends on how deep you want to go. I'm guessing "Freshie" means you've just started?

Yep, not completely zero though. I guess I should read more about data structures then.
But I'm more of wondering what do programmers actually do while working in some IT company, so actually my question's closer to soft eng.
User avatar
tastelikecoke
 
Posts: 1117
Joined: Mon Feb 01, 2010 7:58 am UTC
Location: Antipode of Brazil

Re: Coding: Fleeting Thoughts

Postby Steax » Wed Dec 14, 2011 10:56 am UTC

Very practical challenge: Have a long list of items, in which you have to display with pagination and sorting options (bonus: fuzzy searching and filtering). And by long, I mean catching issues with your method of slicing arrays, and finding corner cases. And make it a real solution: collapse the pagination items if they go over a threshold, allow sorting across 2 properties (i.e. use objects instead of just strings). I find that this challenge trips up a lot of people.

I can give you a bunch of practical things a guy would do in the web development industry, if you want those.
In Minecraft, I use the username Rirez.
User avatar
Steax
SecondTalon's Goon Squad
 
Posts: 2711
Joined: Sat Jan 12, 2008 12:18 pm UTC

Re: Coding: Fleeting Thoughts

Postby b.i.o » Thu Dec 15, 2011 6:59 pm UTC

One project I've worked on recently that's probably simple enough for you to actually implement (and possibly useful enough for you to actually use for yourself at some point) is a file archiving program. It takes files from a whole bunch of locations (my version had some remote ones, and some on the local file system), puts them into archive files (.tar.gz's/zips) and then copies those archives into some remote locations.
User avatar
b.i.o
Green is the loneliest number
 
Posts: 2512
Joined: Fri Jul 27, 2007 4:38 pm UTC
Location: Hong Kong

Re: Coding: Fleeting Thoughts

Postby Yakk » Thu Dec 15, 2011 9:59 pm UTC

I can give you a hard one.

A data structure for a table with the following properties:
1: You can get element n through element n+m fast (where m is small, and n can be large).
2: You can delete element n fast.
3: You can change how it is sorted in a stable manner. This needs to be reasonably fast, and more importantly responsive (ie, if it starts sorting what you are looking at first, that is good). You can sort on any of the columns. (note that stable sorting is important)
4: You can insert an element at location n fast.
5: You can insert an element to be placed in sorted order fast.
6: You need to be able to deal with millions or more elements at once in the structure, while only using a few megs of actual memory (at most).
7: You need to be able to deal with filtering (ie, only show elements that pass this test, or remove elements that fail this test), including asynchronous filtering.
8: You need to be able to change properties of the items in the columns fast (causing a resort of that item).

I think that is about it. It is a rough description of what you need for an inbox data structure that doesn't suck. Note that most inbox data structures suck.

The interesting part, why something like a standard C++ container doesn't work, is the ability to both get the nth element fast and delete it/add one fast. No standard library C++ container can do this, but it isn't hard to write.

My best stab at what I'd use for the above problem (I never got around to writing it) is a something like a B-tree with each tree knowing how many leaves it has under it.
One of the painful things about our time is that those who feel certainty are stupid, and those with any imagination and understanding are filled with doubt and indecision - BR

Last edited by JHVH on Fri Oct 23, 4004 BCE 6:17 pm, edited 6 times in total.
User avatar
Yakk
 
Posts: 10039
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Re: Coding: Fleeting Thoughts

Postby Xeio » Fri Dec 16, 2011 3:29 pm UTC

Next time, remember that your gut instincts are right, when you navigate to a function and the first line is the comment
Code: Select all
//Whamjacked!
Something terrible is afoot.

Ok, so "something terrible" is a null reference exception, and is a minor fix, but still...
User avatar
Xeio
Friends, Faidites, Countrymen
 
Posts: 4411
Joined: Wed Jul 25, 2007 11:12 am UTC
Location: C:\Users\Xeio\

Re: Coding: Fleeting Thoughts

Postby tes » Fri Dec 16, 2011 3:32 pm UTC

I'm learning Java, and it seems a bit wrong to me that you can't access private member variables of the parent class in a subclass. When I want to override a function using those member variables, how should I do it? From what I understand of encapsulation, these variables shouldn't be accessible outside the parent class and subclasses, so public get and set methods are not an option. When I use protected, all classes in the same package can access the variables.
User avatar
tes
 
Posts: 56
Joined: Thu Nov 12, 2009 5:35 pm UTC

Re: Coding: Fleeting Thoughts

Postby RoadieRich » Fri Dec 16, 2011 5:34 pm UTC

tes wrote:I'm learning Java, and it seems a bit wrong to me that you can't access private member variables of the parent class in a subclass. When I want to override a function using those member variables, how should I do it? From what I understand of encapsulation, these variables shouldn't be accessible outside the parent class and subclasses, so public get and set methods are not an option. When I use protected, all classes in the same package can access the variables.

Not allowing access to private members of parent classes seems pretty normal to me - it's the way C++ does it as well.

Use protected, and assume your programmers are consenting adults, a la python?

Alternatively, use more packages.
roband wrote:Mav is a cow.

UniJam 2012: Inter-university Games Jam hosted by Nottingham Trent University DevSoc.
nlug: Nottingham Linux User Group
DevSoc: The Nottingham Trent University Software Development Society
User avatar
RoadieRich
The Black Hand
 
Posts: 1030
Joined: Tue Feb 12, 2008 11:40 am UTC
Location: Somewhere only we know

Re: Coding: Fleeting Thoughts

Postby EvanED » Fri Dec 16, 2011 5:58 pm UTC

Java protected doesn't act like C++ protected. (Regardless of inheritance relationships, Java protected only allows access to other classes in the same package.)

I think the way to look at this is that Java is protecting against actual deliberate, malicious attempts to access those semi-private variables. (This is important because a lot of important things like their security manager depend on the integrity of those variables!) And from that point of view, C++ protected variables might as well be public, because to some extent if you wanted to access one you could just make a subclass and do so. Whereas on the C++ side, they can't really protect against access at all, because you can do horrible-but-working stuff like #define private public or synthesizing pointers to the private members.

Edit: as pointed out below, my statements about the semantics of Java protected are wrong.
Last edited by EvanED on Fri Dec 16, 2011 7:36 pm UTC, edited 1 time in total.
EvanED
 
Posts: 3767
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI

Re: Coding: Fleeting Thoughts

Postby tes » Fri Dec 16, 2011 7:02 pm UTC

Thanks, I'll use protected, then (and packages - until now, I had practically all my files in one package). I'm a beginner, so I didn't mean to say that Java was doing something wrong, just that it didn't seem very intuitive to me.
User avatar
tes
 
Posts: 56
Joined: Thu Nov 12, 2009 5:35 pm UTC

Re: Coding: Fleeting Thoughts

Postby You, sir, name? » Fri Dec 16, 2011 7:16 pm UTC

EvanED wrote:Java protected doesn't act like C++ protected. (Regardless of inheritance relationships, Java protected only allows access to other classes in the same package.)

I think the way to look at this is that Java is protecting against actual deliberate, malicious attempts to access those semi-private variables. (This is important because a lot of important things like their security manager depend on the integrity of those variables!) And from that point of view, C++ protected variables might as well be public, because to some extent if you wanted to access one you could just make a subclass and do so. Whereas on the C++ side, they can't really protect against access at all, because you can do horrible-but-working stuff like #define private public or synthesizing pointers to the private members.


In C++, you'd use pimpl'ing if you wanted to truly hide implementation details from a public interface.
Blag.
Ternary computer emulator. Latest version is 0.5 [Nov 29 2008].

Good morning, that's a nice tnetennba.
User avatar
You, sir, name?
 
Posts: 6128
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City

Re: Coding: Fleeting Thoughts

Postby RoadieRich » Fri Dec 16, 2011 7:18 pm UTC

EvanED wrote:Java protected doesn't act like C++ protected. (Regardless of inheritance relationships, Java protected only allows access to other classes in the same package.)

I think the way to look at this is that Java is protecting against actual deliberate, malicious attempts to access those semi-private variables. (This is important because a lot of important things like their security manager depend on the integrity of those variables!) And from that point of view, C++ protected variables might as well be public, because to some extent if you wanted to access one you could just make a subclass and do so. Whereas on the C++ side, they can't really protect against access at all, because you can do horrible-but-working stuff like #define private public or synthesizing pointers to the private members.

That's not what http://docs.oracle.com/javase/tutorial/ ... ntrol.html says:
The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.
roband wrote:Mav is a cow.

UniJam 2012: Inter-university Games Jam hosted by Nottingham Trent University DevSoc.
nlug: Nottingham Linux User Group
DevSoc: The Nottingham Trent University Software Development Society
User avatar
RoadieRich
The Black Hand
 
Posts: 1030
Joined: Tue Feb 12, 2008 11:40 am UTC
Location: Somewhere only we know

Re: Coding: Fleeting Thoughts

Postby EvanED » Fri Dec 16, 2011 7:35 pm UTC

Wow, I totally didn't know that. Thanks for the correction.

I explicitly remember learning that what I said was true way back when (and in particular, contrasting Java and C++ and C#). I wonder if my brain just made that up whole cloth or if my source at the time was wrong. (Or if the semantics has changed, but that seems like by far the least likely of the three choices.)
EvanED
 
Posts: 3767
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI

Re: Coding: Fleeting Thoughts

Postby TheChewanater » Fri Dec 23, 2011 5:54 am UTC

Stupidest mistake I've made in a while. I didn't notice this all day...

Code: Select all
CameraAnimator::CameraAnimator(SceneNode* cameraNode,
                               btDynamicsWorld* world,
                               BaseApplication* application,
                               float movementSpeed, float turningSpeed,
                               InputMethod inputMethod):
  mCameraNode(cameraNode),
  mWorld(world),
  mApplication(mApplication), 
ImageImage
http://internetometer.com/give/4279
No one can agree how to count how many types of people there are. You could ask two people and get 10 different answers.
User avatar
TheChewanater
 
Posts: 1261
Joined: Sat Aug 08, 2009 5:24 am UTC

Re: Coding: Fleeting Thoughts

Postby darkspork » Fri Dec 23, 2011 9:30 pm UTC

It is utterly pointless to try to arrange your Java code into folders or "packages" when you have cyclical dependencies. It just doesn't work.

I was trying to clean up my code, but there's only one class that nothing depends on, three classes that don't depend on anything (generated by Javacc) and the other thirty or so classes all pretty much depend on the same two classes which, in turn, depend on a class that depends on those thirty or so classes.
Shameless Website Promotion: Gamma Energy
My new esoteric programming language: GLOBOL
An experiment to mess with Google Search results: HARDCORE PORNOGRAPHY HARDCORE PORNOGRAPHY
User avatar
darkspork
 
Posts: 531
Joined: Tue Sep 23, 2008 12:43 am UTC
Location: Land of Trains and Suburbs

Re: Coding: Fleeting Thoughts

Postby RoadieRich » Sat Dec 24, 2011 10:49 pm UTC

If I want a thread to pause its processing loop, with the option of resuming it on an external event, which of the many c# thread control classes is most appropriate? I wanted to use Thread.suspend() and Thread.Resume(), but they're raising empty exceptions (as in no message at all), and a look at the docs shows them as deprecated.

(It's for a threaded software pwm on a netduino, and when the output is 0 or 100%, I want to stop the Write/sleep cycle.)
roband wrote:Mav is a cow.

UniJam 2012: Inter-university Games Jam hosted by Nottingham Trent University DevSoc.
nlug: Nottingham Linux User Group
DevSoc: The Nottingham Trent University Software Development Society
User avatar
RoadieRich
The Black Hand
 
Posts: 1030
Joined: Tue Feb 12, 2008 11:40 am UTC
Location: Somewhere only we know

Re: Coding: Fleeting Thoughts

Postby TheChewanater » Sun Dec 25, 2011 2:29 am UTC

FT: this is really awesome.
ImageImage
http://internetometer.com/give/4279
No one can agree how to count how many types of people there are. You could ask two people and get 10 different answers.
User avatar
TheChewanater
 
Posts: 1261
Joined: Sat Aug 08, 2009 5:24 am UTC

Re: Coding: Fleeting Thoughts

Postby Yakk » Sun Dec 25, 2011 1:49 pm UTC

RoadieRich wrote:If I want a thread to pause its processing loop, with the option of resuming it on an external event, which of the many c# thread control classes is most appropriate? I wanted to use Thread.suspend() and Thread.Resume(), but they're raising empty exceptions (as in no message at all), and a look at the docs shows them as deprecated.

(It's for a threaded software pwm on a netduino, and when the output is 0 or 100%, I want to stop the Write/sleep cycle.)

Multithreaded logic is rarely as simple as you are describing. It is not just what you want, but the details, that really matter.

The simplest pattern you might want to think about is the multi threaded consumer-producer queue pattern.

Many threads can put tasks into the queue, and one thread eats tasks off the queue and does work based on it.

Now, this doesn't exactly match your vague description of your problem, but it might match your actual problem sufficiently well. And when working in multi-threaded programming with a skill level that doesn't already know why suspend and resume are deprecated, you really really want to use a pattern that someone else has worked the bugs out of, and not come up with something new.

Is the multithreaded consumer-producer (one-many) queue sufficient for your problem? If so, you could google an implementation, or I could sketch one.

(The way you'd do this in your case might be to have small sized tasks that you consume in the write cycle. When you hit 100%, you stop producing these small sized tasks, until it falls. There is some control-theoretic problems whereby hit would oscillate out of control, but hey...)
One of the painful things about our time is that those who feel certainty are stupid, and those with any imagination and understanding are filled with doubt and indecision - BR

Last edited by JHVH on Fri Oct 23, 4004 BCE 6:17 pm, edited 6 times in total.
User avatar
Yakk
 
Posts: 10039
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Re: Coding: Fleeting Thoughts

Postby RoadieRich » Sun Dec 25, 2011 11:42 pm UTC

The scenario is this: I have a digital output that I want to be able to fade in and out, according to events triggered by other code. Currently, my (untested, due to previous problem) code has a thread running "while (true) {write(on); sleep(brightness); write(off); sleep(100-brightness);" (Pseudocode, obviously), with brightness (a percentage value) altered by the event handlers. I want to stop the while loop from thrashing needlessly when brightness is 0 or 100. Should I just run the while loop on that condition, and restart it when needed?
roband wrote:Mav is a cow.

UniJam 2012: Inter-university Games Jam hosted by Nottingham Trent University DevSoc.
nlug: Nottingham Linux User Group
DevSoc: The Nottingham Trent University Software Development Society
User avatar
RoadieRich
The Black Hand
 
Posts: 1030
Joined: Tue Feb 12, 2008 11:40 am UTC
Location: Somewhere only we know

Re: Coding: Fleeting Thoughts

Postby Thesh » Mon Dec 26, 2011 2:26 am UTC

Yes, your while loop should just loop on that condition. To prevent it from running twice if someone clicks twice, you can use a mutex.
Eppur si mouve.
User avatar
Thesh
Has the Brain Worms, In Case You Forgot.
 
Posts: 2440
Joined: Tue Jan 12, 2010 1:55 am UTC
Location: Southern California, USA

Re: Coding: Fleeting Thoughts

Postby RoadieRich » Mon Dec 26, 2011 3:05 am UTC

Ok, I've re-written, and it compiles and runs, but it's just spawning hundreds of threads (once each time Write() is called - _t.IsAlive is always false). What have I done wrong?
Code: Select all
public class SoftwarePWM : OutputPort
{
   public SoftwarePWM(Cpu.Pin portID, bool initialState)
      : base(portID, initialState)
   { }

   private Thread _t;
   private int _b;

   public void Write(int brightness)
   {
      _t = new Thread(modulate);
      _b = brightness <= 100 ? brightness : 100;
      switch (_b)
      {
         case 0:
            base.Write(false);
            break;
         case 100:
            base.Write(true);
            break;
         default:
            if (!_t.IsAlive)
               _t.Start();
            break;
      }
   }
   private void modulate()
   {
      int i;
      while (_b >  0 && _b < 100)
      {
         base.Write(true);
         //for (i = 0; i < _b; i++) { }
         Thread.Sleep(_b);
         base.Write(false);
         //for (; i <= 100 - _b; i++) { }
         Thread.Sleep(100 - _b);
      }
   }
}

Edit: yes, I know from previous discussion that it's not really correct to inherit from OutputPort, but let's deal with that later. The basic skeleton was written before that.
roband wrote:Mav is a cow.

UniJam 2012: Inter-university Games Jam hosted by Nottingham Trent University DevSoc.
nlug: Nottingham Linux User Group
DevSoc: The Nottingham Trent University Software Development Society
User avatar
RoadieRich
The Black Hand
 
Posts: 1030
Joined: Tue Feb 12, 2008 11:40 am UTC
Location: Somewhere only we know

Re: Coding: Fleeting Thoughts

Postby Xeio » Mon Dec 26, 2011 3:45 am UTC

If you don't want a new thread to spawn every time you call write why do you keep calling new?

Needs something like
Code: Select all
 if (_t == null) _t = new Thread (modulate);


Also, unless you're trying to save memory or might not always call write, you could initialize in the variable declaration to save the null check.
User avatar
Xeio
Friends, Faidites, Countrymen
 
Posts: 4411
Joined: Wed Jul 25, 2007 11:12 am UTC
Location: C:\Users\Xeio\

PreviousNext

Return to Coding

Who is online

Users browsing this forum: Fekeenuisance, shealtket, Slageammalymn and 5 guests