From: 7stud -- Date: 2007-10-14T00:11:14+09:00 Subject: Re: Is there a standard pattern for threaded access to a file? Francis Cianfrocca wrote: > On 10/12/07, Jon Handler wrote: >> 1. Open the file >> 2. Create 5 threads >> >> Each thread should read a line of the file and process it, but no 2 >> threads should get the same line. > > > > Why are you doing this in the first place? Do you have a computer with > five > processors and five memory buses? According to pickaxe2, p. 135, your question is irrelevant: "Finally, if your machine has more than one processor, Ruby threads won't take advantage of that fact--because they run in a single process, and in a single native thread, they are constrained to run on one processor at a time." Perhaps a better question for the op is: does your processing result in any pauses in the code? For instance, do you use the information in the log file to send requests to websites where you are waiting for a response? Threads do not actually allow any code to run at the same time. What really happens is that execution rapidly shifts from one thread to another, which gives the appearance that the threads are executing at the same time. If you have five methods that each take 2 seconds to execute, and you run those five methods one after another, your program with take 10 seconds to execute. On the other hand, if you use five threads to execute those methods, your program will still take 10 seconds to execute. For example, suppose each thread gets 1 second to execute before execution shifts to another thread, something like this will occur: thread1: 1 sec | V thread2: 1 sec | V thread3: 1 sec | V thread4: 1 sec | V thread5: 1 sec | V thread1: 1 sec | V thread2: 1 sec | V thread3: 1 sec | V thread4: 1 sec | V thread5: 1 sec If you total up the time, it still takes 10 seconds for your program to execute when using five threads. The only way threads help speed up execution is if there are pauses in your code where nothing is happening. During those pauses, threads allow execution to shift to other code that is ready to execute. -- Posted via http://www.ruby-forum.com/.