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 the functionality of olap, Hyper cube or multidimensional cub...

Hyper cube or multidimensional cube forms the core of OLAP system. This having of measures which are arranged according to dimensions. Hyper cube Meta data is formed by star or sno

When a network uses a star topology, A Network uses a star topology if? ...

A Network uses a star topology if? A Network utilizes a star topology if all computers attach to a single central point.

What is big endian and little endian format, What is big endian and little ...

What is big endian and little endian format? The name big endian is used when lower byte addresses are used for the more important of the word. The name little endian is used f

Specifying the problem - learning decision trees, Specifying the Problem: ...

Specifying the Problem: Now next here furtherly we now use to look at how you mentally constructed your decision tree where deciding what to do at the weekend. But if one way

Define congestion, Define Congestion. Congestion: This is uneconomic ...

Define Congestion. Congestion: This is uneconomic to give sufficient equipment to carry all the traffic which could possibly be offered to a telecommunication system. Inside

Analysts in various functional areas, Q. Analysts in various functional are...

Q. Analysts in various functional areas? Today systems analyst's job presents a exciting and fascinating challenge. It provides high management visibility and opportunities for

DBMS, DEFINE FILE ORGANISATION

DEFINE FILE ORGANISATION

Backpropagation, Backpropagation: However Backpropagation can be seen ...

Backpropagation: However Backpropagation can be seen as utilising searching a space of network configurations as weights in order to find a configuration with the least error,

Recursion, Ask qurecurrion for short noteestion

Ask qurecurrion for short noteestion

The statement vline is used to insert vertical lines, Like ULINE the statem...

Like ULINE the statement VLINE is used to insert vertical lines. No , Vline is not used to insert vertical lines.

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