8f0 Subj : Re: help with basic multithreading in C on solaris To : comp.programming.threads From : Giancarlo Niccolai Date : Sat Feb 26 2005 06:51 pm Winbatch wrote: > Hi, I'm trying to learn multithreading and it doesn't seem to be working > for > me. I have a feeling it has to do with the fact that I'm writing to files > rather than to printf, but maybe not. Basically, I wanted to see if it > would be faster to write to 4 files at the same time (parallel) rather > than > 4 in a row (serially). printf IS a file function (and calls write() which is a blocking/cancellation point); just, it USUALLY writes to another process (the console) rather than storing on disk. > however, when my multithreaded code executes, it > seems to do them in order anyway (I expected to see Starting/Ending all > mixed rather than in order). Does anyone know what I'm doing wrong? > (Note, I also tried to do it with 4 different functions (ie, go, go1, go2, > etc.., but that didn't seem to fix it). Unless one or more of those files are actually stored on a different physical device than the others, you are unlikely to see any advantage; the advantage in parallelizing the set-ups that goes before the physical write/ blocking wait step would be probably eaten up by the context switches, and even on multiple CPU there's some synchronization to write on the same device going under the hood, probably minimizing or removing any advantage. Also, the advantage to write in parallel to different physical disk is questionable, and depends on the physical architecture (i.e. IDE, EIDE, SCSI), the disk controller abilities, OS management of DMA channels, overall system load at that moment and so on. If the devices ARE different (i.e. disk, memory, network, and so on), you are likely to take an advantage from MT, else, if only performance is at stake, you are probably better to just serialize file access (however, nothing forbids you to do parallel file accesses in case this makes your program easier/more readable/maintainable/cleaner, even if it may cost a little performance edge). See http://www.niccolai.ws/works/articoli/art-multithreading-en-1a.html Bests, Giancarlo Niccolai . 0