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
Or-Introduction : Thus if we know about one thing is true, and also we know that a sentence when there thing is in a disjunction is true. Here if we consider example, like we

Any function can be expressed in a truth table.A truth table lists all possible combinations ofinputs and gives the output produced in eachcase.Truth tables must include all combin

The Concept of Concurrent and Parallel Execution Real world systems are concurrent in nature and computer science is about modeling the real world. Illustrations of real world

Distinguish among the functions islower() and tolower().   islower( ) and tolower( ): islower(c) is a character testing function described  in ctype.h header file. This

Elements are given 3,14,7,1,8,5,11,17,,6,23,12,20,26,4,16,18,24,25,19 We will construct b tree and avl tree And after that delete some integers

Q. Explain about Deadlock Avoidance? To prevent deadlocks two kinds of techniques are used: 1) Static prevention:  It employs P and V operators in addition to Semaphores to

Define Word/byte rotation and XOR bit masking encryption technique to make sure secured transactions on the net? Word/byte rotation and XOR bit masking: In this process t

What functions connect( ) and accept ( ) call in Socket interfacing? connect() system call: Clients use procedures connect to initiate connection with an exact server. The

E-commerce is working in tandem with the real shop and if all the products are online running the e-commerce shop is becoming simpler. Owner log in on the special admin side of the

What are the essential components of a 3-tier client server In a three-tier or multi-tier environment, the client executes the presentation logic (the client). The business log