Threads, Computer Engineering

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);

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.

Posted Date: 3/12/2013 4:16:27 AM | Location : United States

Related Discussions:- Threads, Assignment Help, Ask Question on Threads, Get Answer, Expert's Help, Threads Discussions

Write discussion on Threads
Your posts are moderated
Related Questions
What is transformation? A transformation is a mapping from the domain of models to the range of models. When modeling, it is significant not only to focus on customer needs, bu

Can you tell me some of system tasks and their purpose? $display, $displayb, $displayh, $displayo, $write, $writeb, $writeh, $writeo. The most useful of these is $display. T

Software Project Planning In the previous days of computing software costs comprised a small % of overall computer based system cost. In order of magnitude error software cost

Classification according to pipeline configuration: According to the configuration of a pipeline, the following parts are recognized under this classification: Unifunct

Question : 1. Define the term Color Depth. Explain how it impacts on image quality and file size. 2. Using an appropriate example, explain when you will consider using GIF

Show Library and its types. Libraries constitute a simple meaning of gathering many object files together: 1. Static: during link editing Library code is integrated with t

Describe five bit even parity checker. Ans: Five bit even parity checker: EX-OR gates are utilized for checking the parity as they generate output 1, while the input ha

(i)  A multiplexer combines four 100-Kbps channels using a time slot of 4 bits. Each Frame has the size of 16 bits. a)  Show the output with the four inputs as shown in the figu

? XML can be used to explain and recognize information accurately and unambiguously, in a way that computers can be programmed to? Understand? (Well, at least influence as if they

Step 1: Click on the icon in the object tool bar Or Insert -> Media -> Choose the media type to be inserted Step 2: Find and select the file required (browse) Step 3: C