Threads, Computer Engineering

First, remember that different processes keep their own data in distinct address spaces. Threads, on the other hand, explicitly share their entire address space with one another. Although this can make things a lot faster, it comes with the cost of making programming a lot more complicated.

In Unix/POSIX, the threads API is composed of two main calls:


pthread create(), which starts a separate thread;


pthread join(), which waits for a thread to complete.

Notice that the general syntax for using these is:

pid = pthread_create(&tid, NULL, function_ptr, argument);

pthread_join(tid, &result);

Example:
void *run(void *d)
{
int q = ((int) d);
int v = 0;
for (int i=0; iv = v + some_expensive_function_call();
return (void *) v;
}
int main()

{
pthread_t t1, t2;
int *r1, *r2;
int arg1=100;
int arg2=666;
pthread_create(&t1, NULL, run, &arg1);
pthread_create(&t2, NULL, run, &arg2);
pthread_join(t1, (void **) &r1);
pthread_join(t2, (void **) &r2);
cout << "r1= " << *r1 << ", r2=" << *r2 << endl;
}
Notice that the above threads maintain different stacks and different sets of registers; except for those, however, they share all their address spaces. Also notice that if you were to run this code in a 2 core machine, it would be expected that it ran roughly twice as fast as it would in a single core machine. If you ran it in a 4 core machine, however, it would run as fast as in the 2 core machine, since there would be no suf?cient threads to exploit the available parallelism.

Posted Date: 3/12/2013 4:16:27 AM | Location : United States







Related Discussions:- Threads, Assignment Help, Ask Question on Threads, Get Answer, Expert's Help, Threads Discussions

Write discussion on Threads
Your posts are moderated
Related Questions
Your shell must accept a change directory command. This will be in the form: cd path  where cd is the change directory command and the path is what you will change the direct

Describe Data Parallel Model? In data parallel model most of parallel work concentrates on performing operations on a data set. Data set is characteristically organised in a co

What is micro programmed control? Micro programmed control is a scheme in which control signals are formed by a program same to machine language program.

Evaluate personality defines? 1. System calls: Linux use a software interrupts to change into kernel mode whilst other UNIX system use an inter segment jump. 2. Message n

Determine the Disadvantages of fact finding -  People are usually uncomfortable being watched and may work in a different way -  What they are watching may not be represent

How can we access the correction and transport system? Each time you make a new object or change an existing object in the ABAP/4 Dictionary, you branch automatically to the W

Adavantages and disadvantages of compilers and interpreters

a) Prototype a macro known AP which takes 4 integer parameters n (number of terms), a (first term), l (last term) and Sum (sum of n terms), provided that: Sum = (a+l)*n/2 b)

Q. How to Load DOS from Hard Disk? To Load DOS from the Hard Disk of your computer, follow these steps: 1. Turn on the power ON/OFF switch of the CPU unit of your PC the pow

CISC (Complex Instruction Set Computer) CISC stands for Complex Instruction Set Computer. If the control unit having a number of micro-electronic circuitry to make a set of con