Threads, Computer Engineering

Assignment Help:

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.


Related Discussions:- Threads

Explain the various interface circuits, Explain the various interface circu...

Explain the various interface circuits.  An I/O interface having of circuitry required to connect an I/O device to computer bus. One side having of a data path with its associa

Derivatives and applications of derivatives, What can you say about the exi...

What can you say about the existence of a stationary point in the interval [ 1; 3] for the function f (x) = x 2 2x 3.

When gravity is the merely force acting on an object, Q. When gravity is th...

Q. When gravity is the merely force acting on an object the object is in what? Answer:- One respond is that it is in free-fall in a vacuum to eliminate atmospheric drag.

In java system.out is an object of which type, In Java System.out is an obj...

In Java System.out is an object of type? In Java System.out is an object of form Print Stream.

What is macro, A series of keystrokes and mouse clicks that can be abbrevia...

A series of keystrokes and mouse clicks that can be abbreviated into a one keystroke or mouse click.

Inspirations of artificial intelligence, Inspirations of artificial intelli...

Inspirations of artificial intelligence: Artificial Intelligence research can be easily understood by following example in terms of how the following question has been answere

Explain derived equations and models, Each student will be assigned a binar...

Each student will be assigned a binary system. The experimental references and the conditions are indicated in the table below. The student should make use of software available on

Equivalence between vhdl and c, Equivalence between VHDL and C? There i...

Equivalence between VHDL and C? There is concept of understanding in C there is structure.Based upon requirement structure provide facility to store collection of various data

Explain significance of init() & destroy( ) method of applet, Explain the s...

Explain the significance of init() and destroy( ) methods of an applet? Also explain two ways of invoking an applet. The init( ) method is utilized for fundamental initializati

Describe the term- system analyst design, Describe the term- System analyst...

Describe the term- System analyst Design Once analysis has taken place and systems analyst has some idea of the scale of problem and what needs to be done, subsequent stage is

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd