Log inRegister an accountBrowse CSDbHelp & documentationFacts & StatisticsThe forumsAvailable RSS-feeds on CSDbSupport CSDb Commodore 64 Scene Database
You are not logged in - nap
CSDb User Forums


Forums > C64 Coding > Some sort of multithreading.
2008-04-08 22:51
gregg
Account closed

Registered: Apr 2005
Posts: 56
Some sort of multithreading.

About a week ago the topic multithreading came up on #c-64. So today I gave it a try. However, there's something wrong with my code and I can't really figure out what it is.

A short description: I have a fixed number of threads running and a CIA IRQ deals with context switching in a round-robin fashion. Every IRQ I save all current state data (SP, status register, PC, A, X, Y) in a structure and fetch the state data for the next thread.

In this example the first thread increments the screen background color (fast), while the second thread changes the border background color (slow). However the wait in the second thread runs too fast every other time, and I have no idea why. It's probably something wrong with the context switch stuff, maybe some of you could take a look at it?

Sources are for ACME.

!to "threading.prg",cbm
!cpu 6510
;!source "mylib.a"
!macro basic_header .a, .b, .c, .d {
        *= $0801
        !byte <.eol,>.eol,0,0,$9e
        !text .a, .b, .c, .d
.eol:   !byte 0,0,0
}

num_threads = 2
thread_num = $fd		; current thread number

;--------------------------------------------------------------------------
+basic_header "2", "0", "6", "1"

*= $080d

init:	sei
		; set up context switch IRQ
		lda #$35
		sta $01

		lda #<context_switch
		ldx #>context_switch
		sta $fffe
		stx $ffff
		
		lda #0
		sta thread_num

		cli
		jmp thread1

;--------------------------------------------------------------------------
context_switch:
		pha
		txa
		pha
		tya
		pha
		lda $dc0d

		; save current thread
		lda thread_num
		; *8
		asl
		asl
		asl
		tay
		; save A,X,Y
		pla
		sta thread_data+6,y
		pla
		sta thread_data+5,y
		pla
		sta thread_data+4,y
		; save PSW
		pla
		sta thread_data+1,y
		; save PC
		pla
		sta thread_data+2,y
		pla
		sta thread_data+3,y
		; save SP
		tsx
		txa
		sta thread_data,y

		; next thread, wraparound
		ldy thread_num
		iny
		cpy #num_threads
		bne +
		ldy #0
+		sty thread_num

		; *8
		tya
		asl
		asl
		asl
		tay

		; restore thread data
		; stack pointer first
		lda thread_data,y
		tax
		txs
		; push PC, PSW for RTI
		lda thread_data+3,y
		pha
		lda thread_data+2,y
		pha
		lda thread_data+1,y
		pha
		; push registers
		lda thread_data+6,y
		pha
		lda thread_data+5,y
		pha
		lda thread_data+4,y
		pha

		pla
		tay
		pla
		tax
		pla
		rti
	
;--------------------------------------------------------------------------
thread1:
		inc $d021
		ldy #$02
		jsr wait2
		jmp thread1


thread2:
		inc $d020
		ldy #$80
		jsr wait2
		jmp thread2
		
wait2:
-		ldx #0
		dex
		bne *-1
		dey
		bne -
		rts

;--------------------------------------------------------------------------
thread_data:
	!fill 8, 0
	!byte $ff-$40, $22, <thread2, >thread2, 0,0,0,0

 
... 211 posts hidden. Click here to view all posts....
 
2008-04-16 19:14
Oswald

Registered: Apr 2002
Posts: 5029
Martin, can you bring two definitions from a "realiable" source about what is multitasking and what is multithreading, and tell me what's the difference between them ?

then can you show me based on the definitions that how's gregg's code a multithreading?

thanks.
2008-04-16 19:22
Oswald

Registered: Apr 2002
Posts: 5029
google: difference multithreading multitasking

first hit:


"Multitasking is running multiple "heavyweight" processes (tasks) by a single OS.

Multithreading is running multiple "lightweight" processes (threads of execution) in a single process / task / program
. See What is the difference between a lightweight and a heavyweight process:

...

--->

Answer
[Short answer: threads are lightweight, programs (aka processes or tasks) are heavyweight. -Alex]

Lightweight and heavyweight processes refer the mechanics of a multi-processing system.

In a lightweight process, threads are used to divvy up the workload. Here you would see one process executing in the OS (for this application or service.) This process would posess 1 or more threads. Each of the threads in this process shares the same address space. Because threads share their address space, communication between the threads is simple and efficient. Each thread could be compared to a process in a heavyweight scenario.

What is the difference between a thread and a process?

--->

Answer
A process is an OS-level task or service. A thread runs "inside" a process and may be virtual or simulated. Generally speaking, threads share resources like memory, where processes each have their own separate memory area, and need to take more elaborate steps to share resources."

is this reliable enough? gregg havent implemented processes, thus he cant have threads either.
2008-04-16 19:27
Oswald

Registered: Apr 2002
Posts: 5029
2nd hit:

http://zone.ni.com/devzone/cda/tut/p/id/6424

"Multithreading extends the idea of multitasking into applications, so you can subdivide specific operations within a single application into individual threads."

3rd hit:

"Multithreading is the ability of a program or an operating system process to manage its use by more than one user at a time and to even manage multiple requests by the same user without having to have multiple copies of the program running in the computer"

http://wiki.answers.com/Q/What_is_difference_between_multiprogr..

4ht hit:

"Multitasking is the ability of an operating system to execute more than one program simultaneously. Though we say so but in reality no two programs on a single processor machine can be executed at the same time. The CPU switches from one program to the next so quickly that appears as if all of the programs are executing at the same time. Multithreading is the ability of an operating system to execute the different parts of the program, called threads, simultaneously."

gregg's program doesnt fullfill any of the above.
2008-04-16 19:38
Oswald

Registered: Apr 2002
Posts: 5029
http://en.wikipedia.org/wiki/Thread_(computer_science)

"Threads and processes differ from one operating system to another but, in general, a thread is contained inside a process and different threads in the same process share some resources while different processes do not."

http://www.allinterview.com/showanswers/4943.html

"multitasking infers the mechanism to run many processes
simultaneously with user interaction. in contrast, multithreading is a mechanism of running various threads under single process within its own space."
2008-04-16 19:49
Martin Piper

Registered: Nov 2007
Posts: 646
This is a classic example of you not understanding the sources you cite. This is because what you cite and what you highlight in bold proves my point and disproves what you wrote earlier.

From your own quote:
"Multi-threading is running multiple "lightweight" processes (threads of execution) in a single process / task / program."

"Multitasking is running multiple "heavyweight" processes (tasks) by a single OS."


Since it can be argued that Gregg's code doesn't handle more heavyweight process switching with threads inside each process, i.e. a hierarchy of preemptive systems whereby the lightweight threads operate inside the more heavyweight tasks, then the definition of multi-threading suits Gregg's code better than the term multi-tasking.


Just so you are clear: Multi-tasking is what my OS code was doing because it had the ability to have threads inside each process and each process had its own address area dynamically allocated in memory.

Just so you are also clear: First comes multi-threading, it is more lightweight because the threads operate within the context of a process. Then comes multi-tasking, it is more heavyweight since it has the concept of processes which also can have their own threads operating inside each process.

For example:
Multi-threading:
3 threads
No extra processes. Just one large process.

Multi-tasking:
Process1.exe - 3 threads - Thread1Foo, Thread2Bar, Thread3Render
Process2.exe - 2 threads - SoundThread, VideoThread
Process3.exe - 7 threads - Thread1, Thread2, thread3 ...



In the land of PCs we use the term multi-threaded application, we don't use the term multi-tasking application. We do apply multi-tasking to the OS because the OS handles different processes, which each process capable of having its own threads.


Just so you are really clear: Gregg's code fits the definition of multi-threading a lot better than the definition of multi-tasking.
2008-04-16 20:18
gregg
Account closed

Registered: Apr 2005
Posts: 56
Looks like you're having a lot of fun here! :D
2008-04-16 20:23
Oswald

Registered: Apr 2002
Posts: 5029
"Since ... Gregg's code doesn't handle more heavyweight process switching with threads inside each process"

exactly. that is the key. gregg's code implements a one level hierarchy which is multitasking - without multithreading. multithreading means a 2 level hierarchy: process switching with thread switchings inside each process.
2008-04-16 21:10
Martin Piper

Registered: Nov 2007
Posts: 646
Quote: "Since ... Gregg's code doesn't handle more heavyweight process switching with threads inside each process"

exactly. that is the key. gregg's code implements a one level hierarchy which is multitasking - without multithreading. multithreading means a 2 level hierarchy: process switching with thread switchings inside each process.


No, you have it the wrong way around, again.

Multi-threading refers to the threads inside a process. In Gregg's code there is one process that has inside it multiple threads.

Multi-tasking refers to having multiple processes and each process being capable of having multiple threads. Gregg's code does not support a hierarchy.


A single array of threads is multi-threading. This is because, as your links say, multi-threading is more lightweight.


From your own links (I'll remove the words that are confusing you and leave the quotes with the words that make it obvious):
"Multi-threading is running multiple lightweight threads of execution in a single process/program." - Gregg's code - Single array of threads.

"Multi-tasking is running multiple heavyweight processes by a single OS." - Not Gregg's code - An array of processes that each can have an array of threads.

Hence why we say multi-threaded application and multi-tasking OS.
2008-04-16 21:45
Oswald

Registered: Apr 2002
Posts: 5029
http://en.wikipedia.org/wiki/Process_%28computing%29

In computing, a process is an instance of a computer program that is being sequentially executed[1] by a computer system that has the ability to run several computer programs concurrently.

as gregg's code can not run several processes (and threads inside them) it does not do multithreading.

furthermore:

In the computing world, processes are formally defined by the operating system(s)(OS) running them and so may differ in detail from one OS to another.

I can see no formal definition of a process in gregg's code.
2008-04-16 22:02
Martin Piper

Registered: Nov 2007
Posts: 646
Quote: http://en.wikipedia.org/wiki/Process_%28computing%29

In computing, a process is an instance of a computer program that is being sequentially executed[1] by a computer system that has the ability to run several computer programs concurrently.

as gregg's code can not run several processes (and threads inside them) it does not do multithreading.

furthermore:

In the computing world, processes are formally defined by the operating system(s)(OS) running them and so may differ in detail from one OS to another.

I can see no formal definition of a process in gregg's code.


Again you have it the wrong way around.

Multi-threading = Gregg's code because it does not support multiple processes that can have threads threads. No hierarchy.

Multi-tasking = Multiple processes that can support multiple threads. A hierarchy.


Because there is no "formal definition of a process in Gregg's code" that means it is doing multi-threading because it has one process with multiple threads inside that one process.


Again this is from your own post:
"Multi-threading is running multiple lightweight threads of execution in a single program."
Previous - 1 | ... | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | ... | 23 - Next
RefreshSubscribe to this thread:

You need to be logged in to post in the forum.

Search the forum:
Search   for   in  
All times are CET.
Search CSDb
Advanced
Users Online
Bert/RDS
Clown
Krill/Plush
wil
xAD/nIGHTFALL
Sentinel/Excess/TREX
Airwolf/F4CG
sebalozlepsi
Dano/Padua
csabanw
katon/Lepsi De
Fred/Channel 4
bugjam
Guests online: 106
Top Demos
1 Next Level  (9.7)
2 13:37  (9.7)
3 Mojo  (9.7)
4 Coma Light 13  (9.7)
5 Edge of Disgrace  (9.6)
6 Aliens in Wonderland  (9.6)
7 No Bounds  (9.6)
8 Comaland 100%  (9.6)
9 Uncensored  (9.6)
10 Wonderland XIV  (9.6)
Top onefile Demos
1 Happy Birthday Dr.J  (9.7)
2 Layers  (9.6)
3 It's More Fun to Com..  (9.6)
4 Cubic Dream  (9.6)
5 Party Elk 2  (9.6)
6 Copper Booze  (9.6)
7 TRSAC, Gabber & Pebe..  (9.5)
8 Rainbow Connection  (9.5)
9 Dawnfall V1.1  (9.5)
10 Daah, Those Acid Pil..  (9.5)
Top Groups
1 Nostalgia  (9.4)
2 Oxyron  (9.3)
3 Booze Design  (9.3)
4 Censor Design  (9.3)
5 SHAPE  (9.3)
Top Logo Graphicians
1 Sander  (9.9)
2 Facet  (9.6)
3 Mermaid  (9.4)
4 Pal  (9.4)
5 Shine  (9.3)

Home - Disclaimer
Copyright © No Name 2001-2024
Page generated in: 0.057 sec.