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

Write an algorithm for the braking system, Question: a) Consider a car ...

Question: a) Consider a car having a collision detection mechanism. Write an algorithm (in simple English) to illustrate the collision detection mechanism. b) The same car

How to use data validation function in vba, Data validation is a significan...

Data validation is a significant concept in VBA. Application process and a custom dialog box can be used to accurate input errors. You can assign an application process to the fram

Static or dynamic - artificial intelligence, Static or Dynamic - artificial...

Static or Dynamic - artificial intelligence An environment is static if it doesn't change while an agent's program is making the decision about how to act. When programming ag

Retina - biometric computer security systems, Retina - Biometric Computer S...

Retina - Biometric Computer Security Systems The next security technology that explained in this report is retina security technology which is very famous in a lot of spy movi

Describe data parallel model, Describe Data Parallel Model? In data par...

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

Two ethernet switches sw1 and sw2 , Six machines H1, H2, ..., H6 are linked...

Six machines H1, H2, ..., H6 are linked by two Ethernet switches SW1 and SW2 as given below. Both switches have four ports drawn. The port numbers are as indicated. Suppose the two

Define strategy procedure, Q. Define Strategy Procedure? The strategy p...

Q. Define Strategy Procedure? The strategy procedure is called when loaded into memory by DOS or whenever controlled device request service. The major purpose of the strategy i

Describe about analysis, Analysis describes about the logical and statistic...

Analysis describes about the logical and statistical analysis needed for an efficient output. This involves writing of code and performing calculations, but most part of these lang

What is meant by hide area, What is meant by hide area? The hide comman...

What is meant by hide area? The hide command temporarily kept the contents of the field at the present line in a system-controlled memory called as the HIDE AREA.  At an intera

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