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

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.

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
Show by induction that your closed formula from (c) is the number of times that the given statement is implemented for a problem of size n.Note: if your proof is easily  a proof th


I have converted my MS Access database to a present conversion. I am getting conversion errors? Ans) Design specs have changed in new releases of MS Access. Visit the on-line

Q. What is Shadow Mask? Shadow Mask is a metal sheet that has fine perforations (holes) in it and is situated a short distance before phosphor coated screen. The Phosphor dots

Explain User Datagram Protocol. UDP(User Datagram Protocol) : User Datagram Protocol uses a connectionless communication paradigm. It is an application using UDP does not re

Coupling and cohesion can be shown using a:- Dependence matrix

Convert the decimal number 430 to Excess-3 code ? Ans. Excess  3  is  a  digital  code  acquired  from  adding  3  to  every  decimal  digit  and  after that converting the res

State about the Logic Micro-operations These operations are performed on binary data stored in register. For a logic micro-operation each bit of a register is treated as a diff

Obtain 1's and 2's complement of 1010 using only four-digit representation. Solution: 1's complement: 1's complement of 1010 is  Please note that wherever you ha

Q. Illustrate Clock signals of clock pulse generator? Synchronization in a sequential circuit is attained by a clock pulse generator that gives continuous clock pulse.  Figure