What is a thread inside a computer system

Status
Not open for further replies.

radhika12

Seniorius Lurkius
1
I have already studied and gone about what is a cpu thread in this forum post only ,but still I am not yet clear with the overall visualization and picture of what the thread actually is?

Also the term THREAD POOL is quite confusing ,so please can someone make me visualize the picture of thread,is it some signal ,some instruction ,what is it and actually when people talk about the fact that the OS selects a particular thread for execution from the thread pool ,then what is this pool actually and where is it present?

Actually the term "THREAD POOL", its still quite confusing.

Also ,when such statements are made like "OS PICKS UP A THREAD FROM THE THREAD POOL ", this statement is highly confusing ,What is the meaning of this and how actually an instruction can be picked up ,It is usually said in JAVA wherein JVM selects the native threads for execution.

I have referred the stackexchange answer posted regarding what is a thread pool and it stated it to be a container ,so when we say that a thread is an instruction ,then how come a container contains pre-instantiated instructions.
 

hanser

Ars Legatus Legionis
43,853
Subscriptor++
[url=http:/civis/viewtopic.php?p=28223093#p28223093:26p5joun said:
Ravensroke[/url]":26p5joun]Did you try reading and understanding the wikipedia entries? They're quite well written.
If the articles are anything like your typical CS Wikipedia entry, they're crap unless you're already well-versed in computer science.
 

minnmass

Ars Tribunus Militum
2,087
Subscriptor
Very basically, a thread is a single series of instructions, and the thread pool is the collection of all of the threads that the operating system is tracking.

A thread can be the whole program, or can be just a part of the larger program (Chrome, for instance, runs each tab in its own thread). There is nothing inherently wrong with a program being single-threaded, nor with it being multi-threaded; some tasks lend themselves better to one model over the other.

At their core, CPUs can only do one thing at a time: only a single thread can be running at any given time (multi-core CPUs and multi-CPU systems can run threads in parallel, but each core is still only running one thread at a time). Modern operating systems let the computer pretend to be able to run more than one thread per core at a time by letting "this" thread run for a very short amount of time, then interrupting its execution to see if there's anything else in the thread pool that needs some time on the CPU.

So, the thread pool is a collection ("pool") of things that need to be done ("threads"). When the OS isn't doing something itself, it checks the pool to see if there's anything that another program wants to do; if so, it chooses one of those tasks ("picks up a thread from the thread pool") and lets it run on the CPU for a bit.

Note for pedantry: some environments (the .NET runtime and, I believe, the JVM) have their own thread pool, which works more-or-less the same as OS-level threads, except that the runtime environment manages the pool itself, and the whole pool looks like a single thread to the OS. There are some performance advantages to this, in some cases. The same general concepts apply, except replace "operating system" with "runtime environment".
 

jbode

Ars Legatus Legionis
11,752
Subscriptor
A process is an instance of a running program. A thread of execution is a sequence of instructions within that process. All processes have at least one thread of execution, but may have multiple threads of execution running in parallel.

Consider the following program:

Code:
#include <stdio.h>

int main( void )
{
  printf( "This is a test\n" );
  exit(0);
}

When run, this program will have a single thread of execution, which is the sequence of instructions to print the string "This is a test" to the console.

A thread is an abstraction of a thread of execution. Depending on the language, it can be a distinct data type (such as the base Thread class in Java) or a collection of data types and functions (such as the POSIX pthread library). Basically, you write a function or procedure that is to be run in its own thread of execution, and then execute it from within the main thread, such as

Code:
void *some_parallel_operation( void *data )
{
  // imagine some really clever code here
  pthread_exit( 0 );
}
...
int main( void )
{
  pthread_t thread_id;
  pthread_attr_t thread_attributes;
  void *data = NULL;
  ...
  if ( pthread_create( &thread_id, &thread_attributes, some_parallel_operation, data ) != 0 )
    perror( "Error creating new thread" );
  else
    pthread_join( thread_id, NULL );

  exit( 0 );
}

When run, this program will have two threads of execution running in parallel: the main thread (which doesn't do much in this example), and the some_parallel_operation thread, which is kicked off by the pthread_create call. You can have more than one some_parallel_operation thread running at a time; you just need to start it with additional pthread_create calls.

The OS keeps track of all running threads, and will either interleave the execution of each thread or schedule different threads on different processor cores so that they appear to run in parallel.

A thread pool is a set of running threads, each of which can be tasked to operate on some piece of input (such as routing print jobs to a printer, or crunching a SETI work unit, or performing some system task). The threads monitor a queue for new items to process, synchronizing their access so that they don't all try to grab the same thing at the same time. When a thread is done processing that input item, it checks the queue for any new items. Starting off a new thread for every input item could get expensive; each thread takes up some resources, and starting a thread can be slow. By having a fixed set of running threads, you avoid these issues.
 

dragonlord

Ars Scholae Palatinae
1,123
Another way to think of it may be to imagine having a stack of postit notes with instructions on them and a needle with some thread attached (because we've got a very very large stack of postit notes). When you want something done you take the instructions off your stack of notes and put them onto the thread in the order that you want them done. When someone has time they pick up the other end of the thread and take the instructions off and perform each instruction. Each person that can do stuff is equivalent to a core in a CPU. What the OS does is the equivalent of having a manager for the different threads who can put stuff onto the other end of a thread (they've got a spare needle) so they take instructions off the thread that you put them on and add them to the actual thread that is going to be run, and when they've done enough instructions they put the current results back on the end of your thread before moving onto the next thread. When it comes round to working on your thread again they put the current results in as the first instruction and then put more of your instructions on that thread.
 
A thread pool is a collection of threads that have been started and then place on a wait queue. When a process wants a task done, it wakes one of the threads and hands the task off to it. The reason for a thread pool is to avoid the overhead of starting a new thread every time. Since the threads are already running (well, actually waiting), the start-up overhead is avoided.
 
Status
Not open for further replies.