Teen Programmers Unite  


Return to forum top

Multithreading Homemade

Posted by CViper [send private reply] at April 22, 2002, 02:46:50 PM

Always wondered how to make a "real" multithreading implementation (eg without all win32 stuff).

I know how to switch "threads", eg changing the esi, esp ... registers, the problem currently is just, that i have to call SwapThreads() or something like that from inside the thread-function.

How do you actually switch threads "automaticly", like the win32 implementation does (you dont have to call any functions in your code to make it work).

Posted by unknown_lamer [send private reply] at April 22, 2002, 03:21:07 PM

What is this SwapThreads you are talking about? Is it part of the threads system that you have programmed? A simple way to get thread-like behaviour would be to set up an alarm() to fire off every second and cause the SIGALRM handler to be called (which would change thread context). Of course, if the program used SIGALRM, it would fail to work. If you want to actually see a (cooperative) threading system's source code, look at QuickThreads (http://www.cs.washington.edu/research/compiler/papers.d/quickthreads.html).
That should give you some help on seeing how you would implement a threads system.

Posted by taubz [send private reply] at April 22, 2002, 08:48:20 PM

I don't have any experience in this, but what I might try is... Take a block that you want to execute, add after it the machine instructions to call your SwapThreads function, and then execute that modified version of the program. Be careful that you don't split up cmp and jmp statements and the like.

- taubz

Posted by CViper [send private reply] at April 24, 2002, 01:54:53 AM

Yeah SwapThreads() is a function i wrote... sorry forgot to tell that :/ (it just 'push'es some registers and flags, swaps esp (stack pointer) and pops of the new threads registers)

That with the alarm() should work. Just need a bit better resolution than 1 sec :) Oh, well i'm just playing around anyway.

Had something like taubz idea in mind, but there are some problems when the code calls some other function or jumps out of the block.

Gonna take a look at QuickThreads as soon as i have time :|

Posted by Mike_L [send private reply] at April 29, 2002, 07:23:20 PM


Consumer operating systems have pre-emptive multitasking. This means that each thread is managed and switched by the OS. All your application need do is call the appropriate library function to create a new thread. The program will then continue on and the new thread will execute simultaneously. On a multi-processor machine, the two threads could indeed execute simultaneously.

A good way to learn about multi-threading is to make a simple program. Here is some pseudocode:

def ChildThread {
loop 3-times {
print "child\n"
sleep 2

def main {
makethread ChildThread
loop 2-times {
print "main\n"
sleep 3

In a unix environment you can use the pthreads library for threading. The Windows threading interface is built into the Win32 API. Windows NT and its derivatives are very efficient in dealing with threads. This is why IIS plugins run best as separate threads inside the main IIS process.

After you learn how to create threads, you should learn how to use thread synchronization functions. The basic idea is that the OS provides a logical object that may be accessed atomically by any thread. Locking objects allow threads to safely access memory or other resources without conflicting with each other. Each thread just has to lock the resource's corresponding lock before using it.

Use the API! =)


Posted by taubz [send private reply] at April 30, 2002, 02:24:10 PM

Mike_L, your post was quite off topic, since his question clearly asked how multitasking is programmed without the aide of the OS.

- taubz

You must be logged in to post messages and see which you have already read.

Log on
Save for later automatic logon

Register as a new user
Copyright TPU 2002. See the Credits and About TPU for more information.