EmbLogic's Blog

Article on POSIX THREADS

Thread Progrmming
Thread:Multiple strands of execution in a single program are called threads. A more precise definition is that athread is a sequence of control within a process.All processes have at least one thread of execution.When a process executes a fork call, a new copy of the process is created with its own variables and its own PID. This new process is scheduled independently, and (in general)executes almost independently of the process that created it. When we create a new thread in aprocess, in contrast, the new thread of execution gets its own stack (and hence local variables) butshares global variables, file descriptors, signal handlers, and its current directory state with the process that created it.

Advantages of Threads:

1.The overhead cost of creating a new thread is significantly less than that of creating a newprocess (though Linux is particularly efficient at creating new processes compared with many other operating systems).

2.Switching between threads requires the operating system to do much less work thanswitching between processes.
3.Multiple threads are much less demanding on resourcesthan multiple processes, and it is more practical to run programs that logically require many
threads of execution on single-processor systems.

Drawbacks of Threads:
1.Debugging a multithreaded program is much, much harder than debugging a single-threadedone, because the interactions between the threads are very hard to control.

2.A program that splits a large calculation into two and runs the two parts as different threadswill not necessarily run more quickly on a single processor machine, unless the calculation truly allows multiple parts to be calculated simultaneously and the machine it is executing on has
multiple processor cores to support true multiprocessing.

How to create Thread:

Threads are created by using system call phtread_create.The first argument of pthread_create is a poniter to thread object,second argument is the attributes of threads,third argument is thread_function and last argument is used to provide argument to the thread_function.

How to join Thread:

Threads are join by using system call phtread_join.The first parameter is the thread for which to wait, the identifier that pthread_create filled in foryou. The second argument is a pointer to a pointer that itself points to the return value from the thread. Like pthread_create, this function returns zero for success and an error code on failure.

how to exit Thread:When a thread terminates, it calls the pthread_exit function, much as a process calls exit when it ter-minates. This function terminates the calling thread, returning a pointer to an object. Never use it to returna pointer to a local variable, because the variable will cease to exist when the thread does so, causing aserious bug.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>