linux多线程编程

1、多线程优点:

(1)We can simplify code that deals with asynchronous(异步事件) events by assigning a separate thread to handle each event type. Each thread can then handle its event using a synchronous programming model. A synchronous programming model is much simpler than an asynchronous one.

 

(2)Multiple processes have to use complex mechanisms provided by the operating system to share memory and file descriptors, Threads, on the other hand, automatically have access to the same memory address space and file descriptors.

 

(3)Some problems can be partitioned so that overall program throughput(吞吐量) can be improved. A single process that has multiple tasks to perform implicitly serializes(串行化) those tasks, because there is only one thread of control. With multiple threads of control, the processing of independent tasks can be interleaved(交叉) by assigning a separate thread per task. Two tasks can be interleaved only if they don't depend on the processing performed by each other.

 

(4)Similarly, interactive(交互) programs can realize improved response time by using multiple threads to separate the portions of the program that deal with user input and output from the other parts of the program.

 

2、线程组成:

A thread consists of the information necessary to represent an execution context within a process. This includes a thread ID that identifies the thread within a process, a set of register values, a stack, a scheduling priority and policy, a signal mask, an errno variable , and thread-specific data . Everything within a process is sharable among the threads in a process, including the text of the executable program, the program's global and heap memory, the stacks, and the file descriptors.

 

3、由于线程ID的类型是pthread_t,为了可移植性,不能简单的把pthread_t看做整型。下面这个函数用于比较两个线程ID是否相等。

 

#include <pthread.h>

int pthread_equal(pthread_t tid1, pthread_t tid2);

Returns: nonzero if equal, 0 otherwise

ps.Linux 2.4.22 uses an unsigned long integer for the pthread_t data type. Solaris 9 represents the pthread_t data type as an unsigned integer. FreeBSD 5.2.1 and Mac OS X 10.3 use a pointer to the pthread structure for the pthread_t data type

4、获得自身线程ID函数:

 

#include <pthread.h>

pthread_t pthread_self(void);

pthead_self可以和pthead_equal连用来帮助每个工作线程识别主线程分配给它的作业:

ps.This function can be used with pthread_equal when a thread needs to identify data structures that are tagged with its thread ID. For example, a master thread (主线程) might place work assignments on a queue and use the thread ID to control which jobs go to each worker thread. This is illustrated in Figure . A single master thread places new jobs on a work queue. A pool of three worker threads removes jobs from the queue. Instead of allowing each thread to process whichever job is at the head of the queue, the master thread controls job assignment by placing the ID of the thread that should process the job in each job structure. Each worker thread then removes only jobs that are tagged with its own thread ID.

 参考书籍《unix环境高级编程》

posted @ 2012-05-15 21:04  王耀it  阅读(244)  评论(0编辑  收藏  举报