A question about threads...

Everything else that doesn't fall into one of the other PB categories.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Amiga5k.

Do threads slow down the program if they do not process messages from Windows? That is, in a typical Win32 program, there's usually somewhere where events are processed (in Powerbasic, you use 'DoEvents', in VB it's done in the background). If my thread is running 'on its own', in a loop, won't that keep Windows from processing messages? Or is this point moot because the main program is doing (should be doing) this?

Thanks
Russell

p.s. On multiprocessor systems, do threads have the ability to run on a separate processor?

***Commodore 64 - Over one million cycles per second, 16 vibrant colors, 3 incredible audio channels and 38,911 Basic Bytes Free! Who could ask for anything more?***
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by ricardo.


Make this easy experiment, run this code and put as comment the delay inside the procedure and see that 100% cpu usage, then uncomment and run it again and you will see that the el goes up.

Code: Select all

Procedure Go(None.l)
Repeat
 Delay(10)
 a+1
 SetGadgetText(1,Str(a))
 ForEver
EndProcedure

If OpenWindow(0,100,150,450,200,#PB_Window_SystemMenu,"Test")
  CreateGadgetList(WindowID())
  StringGadget(1,50,30,350,20,"")
  ButtonGadget(2,150,70,150,25,"Create Thread")
  ButtonGadget(3,150,110,150,25,"Kill Thread")
  Repeat
    EventID=WaitWindowEvent()
    Delay(10)
    Select EventID
    
      Case #PB_EventGadget
        Select EventGadgetID()
          Case 2
            ThreadID = CreateThread(@Go(),0)
          Case 3
            KillThread(ThreadID)
        EndSelect
    
    EndSelect
    
  Until EventID=#PB_EventCloseWindow
EndIf
End
Best Regards

Ricardo

Dont cry anymore...:)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Amiga5k.

Thanks for the example, Ricardo. It seems that commenting out the delay(10) merely does what one would expect in a non-threaded situation: The numbers update slightly more slowly. But what I'm wondering is: In a multitasking environment, would a more complex threaded procedure (let's say one that plays mods in the background) slow down other programs more than it should?

Would it even make sense at all to have an event loop inside the threaded procedure? Is this even allowed?

Thanks in advance,
Russell

***Commodore 64 - Over one million cycles per second, 16 vibrant colors, 3 incredible audio channels and 38,911 Basic Bytes Free! Who could ask for anything more?***
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by freak.

As all threads must be executed by the same CPU, this of course has an affect on other Programs.
How strong this effect will be depends on the priority your thread will get. If you give i a
priority lower than normal, it will almost have no effect, as the other ones will get more
CPU time then.

About message loops:
Each thread has it's own message queue, which means, that if you open a Window in your thread,
the Events will not be send to the main program, so you MUST include an event loop in the thread.

However, threads in PB should not be used that often, as strings for example are not save to
use there. Use threads only for background processing for large data, such as packing/unpacking.

Timo

http://freak.coolfreepages.com/
Amiga5k
Enthusiast
Enthusiast
Posts: 329
Joined: Fri Apr 25, 2003 8:57 pm

Post by Amiga5k »

Thanks, freak, that's good to know (did I mention I L O V E this new faaaast forum? :D).

Anyway, so as long as I don't open any windows in the thread I should be alright, right?

And speaking of string safety in your post, when creating threads that read\write strings what I was doing was creating simple wrapper functions (can't bring myself to call them procedures ;) ) that do this in a safe way, since the user may not do the proper checking first. I usually use a structure to communicate between the main program and the thread: The user fills the type.structure with the info they want to pass, sends the pointer of the type to the thread in CreateThread() and then the thread sets a flag in the type when it is safe to write data. The wrapper functions look for this flag before writing data. The nice part about this approach is that several threads can be started that use their own type (via pointer) and don't interfere with each other :)

Russell
*** Diapers and politicians need to be changed...for the same reason! ***
*** Make every vote equal: Abolish the Electoral College ***
*** www.au.org ***
User avatar
Danilo
Addict
Addict
Posts: 3037
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Post by Danilo »

@freak:
> As all threads must be executed by the same CPU

You dont run multi-processor systems, do you ?? :D
cya,
...Danilo
...:-=< http://codedan.net/work >=-:...
-= FaceBook.com/DaniloKrahn =-
freak
PureBasic Team
PureBasic Team
Posts: 5929
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

Hmm, maybe I was thinking too much of my own Computer only. :oops:

Ok, multi-processor systems can run threads simultanously (also the new
P4 with hyper-threading technology can do so), but it is still not likely that
there is _only one_ thread running on a CPU. Or do you have one CPU
for every of the system and user threads? (must be a expensive
Computer then. :D

So threads are always having some effects on each other in some way,
that was my whole point.

Timo
quidquid Latine dictum sit altum videtur
Amiga5k
Enthusiast
Enthusiast
Posts: 329
Joined: Fri Apr 25, 2003 8:57 pm

Post by Amiga5k »

I remember reading about the Cray XMP about 20 years ago, and how it used 'vector processing', where a single command would be executed on hundreds of processors 8O , but with different data (it's slightly more complex than that, as you can imagine, but that's the general idea :) )

The processor boards were submerged in liquid nitrogen (or was it helium?) to keep the temp down at the then outrageous clock speeds. Amazing that the pc on my desktop is running at 3 or 4 times the speed of the xmp! (Although I'm just one processor, not hundreds)...

Anyway, I believe Fuji has the current record for fastest computer (something like 20 tereflops :lol: )

Later,
Russell
*** Diapers and politicians need to be changed...for the same reason! ***
*** Make every vote equal: Abolish the Electoral College ***
*** www.au.org ***
User avatar
geoff
Enthusiast
Enthusiast
Posts: 128
Joined: Sun Apr 27, 2003 12:01 am
Location: Cornwall UK
Contact:

Post by geoff »

The cooling liquid was fluorocarbon at about room temperature.
Amiga5k
Enthusiast
Enthusiast
Posts: 329
Joined: Fri Apr 25, 2003 8:57 pm

Post by Amiga5k »

Well throw me in the freezer and call me a popsicle...you're right! :) I thought I was going to find 'evidence' to the contrary on the net and send you the link, but.... turns out that all these years of hearing about these liquid cooled Crays was wrong! :cry:

I found several cray links (several very interesting ones), with specs galore. They didn't really say what the temperature of the coolant was, but if it was 200 degrees below zero I'm sure they would have mentioned it ;)

Anyway, I found it interesting that the speed of one of the Cray models was only about 70mhz per processor! Impressive at that time, but pretty tame compared to now. Also, Compaq owns DEC, which had a business relationship with Cray and Intel bought the (DEC) Alpha facility. I guess we know where Intel got the high clock speed knowledge, eh? (The Alpha processors were running at over 600mhz when Intel and the rest were struggling with half that speed). I wonder where the Alpha would be now, clockspeed-wise, if they had not been bought out? 6 or 7 Ghz? :twisted:

Russell
*** Diapers and politicians need to be changed...for the same reason! ***
*** Make every vote equal: Abolish the Electoral College ***
*** www.au.org ***
User avatar
geoff
Enthusiast
Enthusiast
Posts: 128
Joined: Sun Apr 27, 2003 12:01 am
Location: Cornwall UK
Contact:

Post by geoff »

Actually fluorocarbon liquid is less efficient in cooling a surface than water, but it has the advantage that it doesn't conduct electricity.

I wonder how long before we see liquid cooling as standard practice for the CPUs in our PCs? I'm getting pretty tired of fan noise myself. :(

Funny how expectations fall with time. When the Cray cost several million dollars we expected it to predict the weather and solve all our problems. So given the massive processing power of a modern PC it's surprising how little we expect from it.

In 1967 the film 2001 featured a computer with human like reasoning and we thought this a real possibility before the end of the century even though we never expected clock speeds to increase quite as much as they have done. :)

So while processor speeds and costs have improved beyond our wildest dreams, software development has been about the biggest disappointment in history. :(

Modern processors have a million times the "clock speed" of the "wet" processor in our heads, so how come AI has been such a disappointment so far :?:

Getting into space hasn't been as easy as we expected, either.
Amiga5k
Enthusiast
Enthusiast
Posts: 329
Joined: Fri Apr 25, 2003 8:57 pm

Post by Amiga5k »

Actually fluorocarbon liquid is less efficient in cooling a surface than water, but it has the advantage that it doesn't conduct electricity.
Yeah, I guess conductivity would be bad :)
I wonder how long before we see liquid cooling as standard practice for the CPUs in our PCs? I'm getting pretty tired of fan noise myself.
Remember when the first 1ghz processor came out (an AMD :) ). At first they showed it with a liquid cooling system, but within a very short time it was merely fan-cooled (with a HUGE heatsink).
In 1967 the film 2001 featured a computer with human like reasoning and we thought this a real possibility before the end of the century even though we never expected clock speeds to increase quite as much as they have done
True. Neural Net processors, at least in the lab, have shown great promise, but they are decades away from being viable (at least that's what they SAY :roll: . According to 'Moore's Law', computer speeds should double every 18 months. Following this trend, computers should be running at 22ghz by 2006! I think Moore's Law may need to be revised.... ;)
So while processor speeds and costs have improved beyond our wildest dreams, software development has been about the biggest disappointment in history
100% correct. People get the idea that Windows is getting faster or more powerful because they are running it on ever faster and faster systems. The truth is that if you run WinXp, for example, on a identical system alongside Windows 3.1 you'll find that Windows has become 20 times more bloated and runs at half speed! So really, Microsoft owes Intel a HUGE debt of gratitude for making Windows bearable. It would be sheer torture to use otherwise (sometimes it's sheer torture anyways! :) )
I would much rather have a leaner, more stable but OLDER version of Windows than the flavor of the month. Seems like for every problem they fix, they create 2 more!

Russell
*** Diapers and politicians need to be changed...for the same reason! ***
*** Make every vote equal: Abolish the Electoral College ***
*** www.au.org ***
Post Reply