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 about local area network, Q. Explain about Local Area Network? ...

Q. Explain about Local Area Network? Local Area Network (LAN):  It is privately owned communication systems that cover up a small area, say a complex of buildings or school. Le

Describe about the desk top publishing, Desk Top Publishing (DTP) As wo...

Desk Top Publishing (DTP) As word processors develop increasingly more sophisticated features, differences with desk top publishing (DTP) packages becomes more and more blurred

Fundamentals of systems, System is a word which is derived from the Greek w...

System is a word which is derived from the Greek word 'Systema' which means an organized relationship among components. A System can be defined as orderly grouping of interdepen

Define relocatable programs, Relocatable programs? Ans. Relocatable pro...

Relocatable programs? Ans. Relocatable programs can be loaded almost anywhere inside the memory.

Animation for project, Final Animation This is the final piece of work ...

Final Animation This is the final piece of work that should stand on its own as a piece of Art or Design. This is your personal response to the idea of Transformation - The pro

Explain working of counters, Q. Explain working of Counters? A counter ...

Q. Explain working of Counters? A counter is a register that goes through a predetermined sequence of states when clock pulse is applied. In principle value of counters is incr

Network throughput-network properties, Network throughput It is an indi...

Network throughput It is an indicative measure of the point carrying capacity of a network. It is distinct as the total number of messages the network can transmit per unit tim

Systems analyst in traditional business, Q. Systems Analyst in Traditional ...

Q. Systems Analyst in Traditional Business? In the traditional business information services are centralized for entire organization or for a specific location. In this organiz

Buses - computer architecture, Buses: Execution of 1 instruction ne...

Buses: Execution of 1 instruction need the following 3 steps to be performed by the CPU: I.  Fetch the contents of the memory location pointed at by the computer syst

Arithmetic pipeline, Arithmetic Pipeline The complex arithmetic operati...

Arithmetic Pipeline The complex arithmetic operations like floating point and multiplication operations use a lot of the time of the ALU. These operations can also be pipelined

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