|
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. |
On March 05 2013 19:19 Rixxe wrote:C# Threading question: What is the best way to manage excess Threads? Threadpool isn't what i need as some of the Threads lock various files and controls (From what i've read, if this is the case it's best not to use a Threadpool). However i'm only able to run 10 background threads at once, and would like to complete new threads as old ones finish. Currently i don't create a thread for each new task until need to start it. I thought i could store 'p' (object) in an Array of some kind, and just empty it as i get space. Not sure this is generally a good idea or not. + Show Spoiler + for (; ; ) { Thread.Sleep(60000);
if (server.Pending()) { TcpClient client = server.AcceptTcpClient(); NetworkStream strm = client.GetStream(); IFormatter formatter = new BinaryFormatter(); TCPClassLibrary.GenObject p = (TCPClassLibrary.GenObject)formatter.Deserialize(strm); while(ServerFront.ThreadsInProcess == ServerFront.ProcessMaximum) { }
switch(p.Action) { case "0": ServerFront.ThreadsInProcess++; ThreadStart TasksStarter = delegate { CurrentTasks.AddTask(p, Directory, m_bgw, Status, m_currentForm, ServerFront.ThreadsInProcess); }; Thread TasksThread = new Thread(TasksStarter); TasksThread.IsBackground = true; TasksThread.Start(); break; case "1": ThreadStart TasksAborted = delegate { CurrentTasks.AbortTask(p, m_bgw, Status, m_currentForm, ServerFront.ThreadsInProcess); }; Thread TasksABThread = new Thread(TasksAborted); TasksABThread.IsBackground = true; TasksABThread.Start(); break; default: etc ........
Cheers for any ideas.
There's a difference between asynchronous and threading. I think what you want is async handling of your code, without managing the threads yourself (and without making too much threads). You can look into the Task Parallel Libary (TPL) and C# async keyword. These are built in the .net framework since .net 4.0 / 4.5. Since the TPL I haven't used System.Threading anymore, and TPL is a joy to use 
On March 05 2013 20:31 Frigo wrote: Is it possible to use non-blocking I/O in C#? Sounds like it would be a solution to your problem.
Yes I think with the TPL and async it is possible. In the Windows 8 app model all IO is non-blocking, to make the app responsive.
|
The code for that example looks wrong. I think it should say
while x > 1:
and then it would have the behavior they're talking about. You can't make a number negative by repeatedly dividing by 2.
|
On March 06 2013 00:47 KurtistheTurtle wrote:This is more of a math question, I'm learning to measure the complexity of a function + Show Spoiler +Here's the whole function, it's in Python: + Show Spoiler + def program2(x): total = 0 for i in range(1000): total = i
while x > 0: x /= 2 total += x
return total
Directly on the while x>0, x/=2 part: The explanation I'm given is Because we divide x by 2 every time through the loop, we only execute this loop a logarithmic number of times. log2(n) divisions of x by 2 will get us to x = 1; we'll need one more division to get x <= 0 but I still don't get it. x / (2 *log2(n)) = 1? random math which might not be right (I haven't done logs since high school) 2 * log2(n) = 1 * x log2(n) = x/2 2^x/2 = n for program2(x) = 10 10 5 2.5 1.25 .625 so n larger than 3 and less than 4 2^ wait...is that base 2 or log base 10? i'll come back to it I can help with this one.
When you take a number like 16 and divide it by 2, you get 8. Divide it again and you get 4. Divide it again and you get 2. Again and you get 1. You would have to divide 4 times for the code execution for a number like 16.
log base 2 of 16 = 4. 2^4=16 Similarly with numbers like 17,21,23, you would have to execute on the order of the same number of steps given an input size roughly that of 16.
Even if some border conditions were changed in the code, like instead of x>1, you did x>2, or x>200, for large input values, you would still have to go through the loop roughly log base 2 of n times.
Do more practice is all I can say.
|
On March 06 2013 00:57 KonekoTyriin wrote:You can't make a number negative by repeatedly dividing by 2. Maybe you're just not trying hard enough.
---
Oh, I just realized there's an alternative explanation to the others' suggestion that the while loop's comparison is written incorrectly. I don't know Python, but it's common that if you perform division on an integer, you get an integer result created by truncating the value (IE, rounding down to the nearest integer). If that's the case in Python, and assuming you're supposed to be calling your function with integer values, then the logic works. The while loop for program2(10) would set x to 5, 2, 1, then 0 and terminate.
... and in fact, testing your program on pythonfiddle.com yields exactly that result.
|
total = 0 for i in range(1000): total = i This seems like a slow and misleading way to set total = 999.
|
On March 05 2013 19:19 Rixxe wrote:C# Threading question: What is the best way to manage excess Threads? Threadpool isn't what i need as some of the Threads lock various files and controls (From what i've read, if this is the case it's best not to use a Threadpool). However i'm only able to run 10 background threads at once, and would like to complete new threads as old ones finish. Currently i don't create a thread for each new task until need to start it. I thought i could store 'p' (object) in an Array of some kind, and just empty it as i get space. Not sure this is generally a good idea or not. + Show Spoiler + for (; ; ) { Thread.Sleep(60000);
if (server.Pending()) { TcpClient client = server.AcceptTcpClient(); NetworkStream strm = client.GetStream(); IFormatter formatter = new BinaryFormatter(); TCPClassLibrary.GenObject p = (TCPClassLibrary.GenObject)formatter.Deserialize(strm); while(ServerFront.ThreadsInProcess == ServerFront.ProcessMaximum) { }
switch(p.Action) { case "0": ServerFront.ThreadsInProcess++; ThreadStart TasksStarter = delegate { CurrentTasks.AddTask(p, Directory, m_bgw, Status, m_currentForm, ServerFront.ThreadsInProcess); }; Thread TasksThread = new Thread(TasksStarter); TasksThread.IsBackground = true; TasksThread.Start(); break; case "1": ThreadStart TasksAborted = delegate { CurrentTasks.AbortTask(p, m_bgw, Status, m_currentForm, ServerFront.ThreadsInProcess); }; Thread TasksABThread = new Thread(TasksAborted); TasksABThread.IsBackground = true; TasksABThread.Start(); break; default: etc ........
Cheers for any ideas.
At my work, we have an automated FTP program that breaks out multiple ftp tasks into threads. We set a hard code limit to the number of threads it can use at one time. Here's some rough pseudocode on how that works (Hope you don't mind that it's in VB, not C#):
'Make sure this part is declared appropriately so that it's not lost when the main run loop ends Dim MyThreads(5) as Threading.Thread Dim MyTasks(5) as Task
'Put this in your run loop Dim i as Integer For i = 0 to MyThreads.GetUpperBound(0) If IsNothing(MyThreads(i)) OrElse MyThreads(i).IsAlive = False Then MyTasks(i) = New Task If MyTasks(i).IsThereATaskToDo Then MyThreads(i) = New Threading.Thread(AddressOf MyTasks(i).StartTask) MyThreads(i).Start() End If End If Next
I think it's pretty self explanatory, but if you have any questions, feel free to ask.
|
On March 06 2013 01:31 gyth wrote: total = 0 for i in range(1000): total = i This seems like a slow and misleading way to set total = 999. The point isn't to generate a result, it's to learn how to analyze algorithmic complexity.
|
On March 06 2013 01:11 obesechicken13 wrote: When you take a number like 16 and divide it by 2, you get 8. Divide it again and you get 4. Divide it again and you get 2. Again and you get 1. You would have to divide 4 times for the code execution for a number like 16. I get this...
log base 2 of 16 = 4. 2^4=16 Similarly with numbers like 17,21,23, you would have to execute on the order of the same number of steps given an input size roughly that of 16.
I can just straight say log2 of x = n? I was making this much more complicated than it had to be. Thanks for the explanation, I appreciate it!
|
On March 06 2013 03:44 KurtistheTurtle wrote:Show nested quote +On March 06 2013 01:11 obesechicken13 wrote: When you take a number like 16 and divide it by 2, you get 8. Divide it again and you get 4. Divide it again and you get 2. Again and you get 1. You would have to divide 4 times for the code execution for a number like 16. I get this... Show nested quote +log base 2 of 16 = 4. 2^4=16 Similarly with numbers like 17,21,23, you would have to execute on the order of the same number of steps given an input size roughly that of 16.
I can just straight say log2 of x = n? I was making this much more complicated than it had to be. Thanks for the explanation, I appreciate it! I know you get that. I just find it beneficial when someone tries to explain something to me gradually.
I wouldn't say it's that simple. You can say that the order of growth follows the pattern that log2 of x = n for arbitrarily large values of x. If for some stupid reason you had to iterate through the loop 2 more times... then if n were equal to the number of iterations, n would equal (log2 of x) + 2 but the order of growth would still be log2 of x. Similarly, if you had to go through the loop twice times as often, the order of growth would be 2 * (log2 of x) but that is still the same order as log 2 of x. We don't care about scalar multiplication or addition.
|
Haha, you're following my material exactly. Honestly this reminds me of calculus when I started doing derivatives and integrals. If the lesson had come from the angle "order of growth as relates to computation" from the start I wouldn't have been confused at all.
New Question:
I have nobody to talk to about this stuff in real life. My friends are nerds, but not true nerds. Definitely not straight programming nerds. After I finish this course I'm going to be focusing on Java specifically for a while, but I need some community. What are some good sites to start participating in?
|
Nevermind. I started reading the OP, I think I might go learn C hurriedly
|
But Java...I'm swinging back and forth on the subject, but I'm interviewing a senior developer tomorrow so I'll just follow his advice on what to do
|
On March 06 2013 04:52 KurtistheTurtle wrote: Haha, you're following my material exactly. Honestly this reminds me of calculus when I started doing derivatives and integrals. If the lesson had come from the angle "order of growth as relates to computation" from the start I wouldn't have been confused at all.
New Question:
I have nobody to talk to about this stuff in real life. My friends are nerds, but not true nerds. Definitely not straight programming nerds. After I finish this course I'm going to be focusing on Java specifically for a while, but I need some community. What are some good sites to start participating in? I highly recommend hanging out on StackOverflow. I read SO every day at work, and it's a really good resource for programmers of all levels.
|
On March 06 2013 06:07 AmericanUmlaut wrote:Show nested quote +On March 06 2013 04:52 KurtistheTurtle wrote: Haha, you're following my material exactly. Honestly this reminds me of calculus when I started doing derivatives and integrals. If the lesson had come from the angle "order of growth as relates to computation" from the start I wouldn't have been confused at all.
New Question:
I have nobody to talk to about this stuff in real life. My friends are nerds, but not true nerds. Definitely not straight programming nerds. After I finish this course I'm going to be focusing on Java specifically for a while, but I need some community. What are some good sites to start participating in? I highly recommend hanging out on StackOverflow. I read SO every day at work, and it's a really good resource for programmers of all levels. Agreed. I like stack and I don't even code that much anymore.
Some of those links in the OP are old. SO OLD. I wouldn't use those.
|
Hyrule19174 Posts
Well the give better ones.
|
On March 06 2013 06:20 obesechicken13 wrote:Show nested quote +On March 06 2013 06:07 AmericanUmlaut wrote:On March 06 2013 04:52 KurtistheTurtle wrote: Haha, you're following my material exactly. Honestly this reminds me of calculus when I started doing derivatives and integrals. If the lesson had come from the angle "order of growth as relates to computation" from the start I wouldn't have been confused at all.
New Question:
I have nobody to talk to about this stuff in real life. My friends are nerds, but not true nerds. Definitely not straight programming nerds. After I finish this course I'm going to be focusing on Java specifically for a while, but I need some community. What are some good sites to start participating in? I highly recommend hanging out on StackOverflow. I read SO every day at work, and it's a really good resource for programmers of all levels. Agreed. I like stack and I don't even code that much anymore. Some of those links in the OP are old. SO OLD. I wouldn't use those. Put stackoverflow on my daily reading list.
But as for the OP, I'm gonna side with Tofu on this one. I've got no other references, and even if they don't grade at A+ a B is so much better than an F. And I don't think it's possible for resources like that to grade at -B, but this analogy has gone too far already
|
This just happened for me.
After spending 1½ish days (of effective work time) trying to snipe a bug in the game project that I'm working on at my university, I finally found out the reason why it wasn't working, and it was such a simple mistake that I hate myself for not noticing it earlier...
|
On March 06 2013 23:44 WindWolf wrote: This just happened for me.
After spending 1½ish days (of effective work time) trying to snipe a bug in the game project that I'm working on at my university, I finally found out the reason why it wasn't working, and it was such a simple mistake that I hate myself for not noticing it earlier...
Happens man, it always happens.
Most of the time when I'm stuck at a bug I simply leave for a while and go do something else to refresh my mind. It helps you to "think outside of the box"
|
On March 06 2013 23:50 fabiano wrote:Show nested quote +On March 06 2013 23:44 WindWolf wrote: This just happened for me.
After spending 1½ish days (of effective work time) trying to snipe a bug in the game project that I'm working on at my university, I finally found out the reason why it wasn't working, and it was such a simple mistake that I hate myself for not noticing it earlier... Happens man, it always happens. Most of the time when I'm stuck at a bug I simply leave for a while and go do something else to refresh my mind. It helps you to "think outside of the box"  This is really good advice. I literally go outside and juggle (I keep my juggling balls at work for exactly this purpose) or play the piano in the basement for ten or fifteen minutes when I'm really stuck on a bug. I can't tell you how often the answer has leapt into my brain as soon as I got myself really focused on something else, or I've come back to it and realized some obvious mistake I was making that I was blind to from staring at the code for too long.
|
On March 07 2013 00:32 AmericanUmlaut wrote:Show nested quote +On March 06 2013 23:50 fabiano wrote:On March 06 2013 23:44 WindWolf wrote: This just happened for me.
After spending 1½ish days (of effective work time) trying to snipe a bug in the game project that I'm working on at my university, I finally found out the reason why it wasn't working, and it was such a simple mistake that I hate myself for not noticing it earlier... Happens man, it always happens. Most of the time when I'm stuck at a bug I simply leave for a while and go do something else to refresh my mind. It helps you to "think outside of the box"  This is really good advice. I literally go outside and juggle (I keep my juggling balls at work for exactly this purpose) or play the piano in the basement for ten or fifteen minutes when I'm really stuck on a bug. I can't tell you how often the answer has leapt into my brain as soon as I got myself really focused on something else, or I've come back to it and realized some obvious mistake I was making that I was blind to from staring at the code for too long. I agree, but the thing is here was that when I got stuck, I worked on other things that I could work on and did some other stuff as well, but I still never could get any closer to the solution. And when I figured out what the problem really was, I became a bit extra disappointed on myself, cause I was close to find the problem a few times earlier.
|
|
|
|
|
|