Could someone please explain how "threads" work

khold

Member
Mar 5, 2000
172
0
0
I am curious how "threads" in programs work. (for instance, if you are building a dual processor system with an SMP setup).

thanks for any explanation
 

Goi

Diamond Member
Oct 10, 1999
6,772
7
91
You don't need a dual-processor system to run multithreaded programs.

Anyway, a tread is a basic unit of CPU utilization. It consists of a program counter(PC, indicating where your current/next instruction is in memory), a register set, and a stack space. It shares the OS resoueces and code/data section with other threads.
 

Conroy9

Senior member
Jan 28, 2000
611
0
0
A thread is just something that is running code - so normally each program runs in its own thread
that's why, without a multithreaded operating system, you can only run one program at a time generally
 

DaddyG

Banned
Mar 24, 2000
2,335
0
0
You can have multiple threads in a single cpu environment, but only one thread will be actually processing at any given time. The difficulty with MP systems is that two (or more) threads can be active at the same time and if the threads are not programmed correctly they can compete for resources and screw up.
 

ccc

Member
Apr 29, 2000
133
0
0
conroy9,
not entirely true, I think you got confused between
multithread and multiprocess.

Multi-threading is the ability of independent program to
perform more than 1 task at what seems to be the same time.
A task is a computational unit and corresponds to 1 thread.

A program that load data file while also reading
user input is said to have 2 tasks and is therefore multithreaded.

A process is an independent program. A process can contains
more than 1 thread. Processes can also run at the same time.

So what is the different between process and thread ?
Process has its own resources, while thread uses shared
resources (or resource from the process).

Goi is correct, multithreaded programs don't
need multi-processor, best is to use multi-processor
but under one processor it can also simulate 2 or
more threads running at the same time by performing
context switching (for example if you have 2 threads,
first it let first thread to run for maybe 1 millisec,
then let another thread run for the same time,
then switch back to first thread and keep switching,
from user point of view, these two threads appear
to run at the same time).

There are whole lots of discussion on multithreaded, so
you can find a lot of information from the net. Best is
to search from Modern Operation System courses which offered
by many university.
 

Conroy9

Senior member
Jan 28, 2000
611
0
0
well yeah, one process can have several threads too, but it doesn't need to..
i should have said each app runs in its own thread, but can also create more threads to run different tasks at the same time

i was assuming that he didn't want to know the details of how the resources are shared...

thanks for clarification