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

Copy machine problem, We are trying to figure out how many copy machines we...

We are trying to figure out how many copy machines we should install in the basement of Stranahan Hall.  Copy machines should be available 24/7.  Students may walk up at any time o

Instruction level-levels of parallel processing, Instruction Level It r...

Instruction Level It refers to the condition where different instructions of a program are implemented by different processing elements. Most processors have numerous execution

Shell script, shell script for addnames that has to be called as class list...

shell script for addnames that has to be called as class list in the name of the class list file and username is a particular student''s username

Explain about annotational notations, Explain about Annotational Notations ...

Explain about Annotational Notations These notations may be applied to describe remark and illuminate about any element in the model. They are considered as explanatory of U

Develop website for my party venue- hall rental website, Project Descriptio...

Project Description: I want to prepare website for my party venue where i can post the photos and detail of place along with availability calendar. In future, I buy some more ha

What is guard bits, What is guard bits? Guard bits are extra bits which...

What is guard bits? Guard bits are extra bits which are produced while the intermediate steps to yield maximum accuracy in the final results.

Disadvantages of edi(electronic data interchange), Disadvantages  1.  T...

Disadvantages  1.  The X12 standard is so large and general  2.  EDI communications negotiate a technical agreement to explain exactly what subset of EDI they will use

Two ways in which one can retrieve data from tables, In general what are th...

In general what are the two ways in which one can retrieve data from tables? Using Select statements, Database Program.

The new optimal solution, Electrocomp's management realizes that it forgot ...

Electrocomp's management realizes that it forgot to contain two critical constraints. In particular, management decides that to make sure an adequate supply of air conditioners for

What is a piconet in a bluetooth context, Problem: (a) IEEE802.11 supp...

Problem: (a) IEEE802.11 supports two types of network architecture, describe these architectures with the support of diagrams detailing the network components. (b) The MAC

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