michile

导航

2013年2月3日

advacing lnux program --条件变量[copy]

摘要: 我们已经展示了如何在两个线程同时访问一个变量的时候利用互斥体进行保护,以及如何使用信号量实现共享的计数器。条件变量是GNU/Linux提供的第三种同步工具;利用它你可以在多线程环境下实现更复杂的条件控制。假设你要写一个永久循环的线程,每次循环的时候执行一些任务。不过这个线程循环需要被一个标志控制:只有当标志被设置的时候才运行,标志被清除的时候线程暂停。代码列表4.13显示了你可以通过在不断自旋(重复循环)以实现这一点。每次循环的时候,线程都检查这个标志是否被设置。因为有多个线程都要访问这个标志,我们使用一个互斥体保护它。这种实现虽然可能是正确的,但是效率不尽人意。当标志没有被设置的时候,线程会 阅读全文

posted @ 2013-02-03 21:37 michile 阅读(209) 评论(0) 推荐(0) 编辑

advacing lnux program --互斥信号量[copy]

摘要: 之前的例子中,我们让几个线程从一个队列中取出并处理任务,每个线程函数都会尝试从队列中取得任务并当没有任务的时候结束线程函数。如果事先给队列中添加好任务,或者至少以比处理线程提取任务更快的速度向队列中添加新任务,这个模型没有问题。但如果工作线程速度太快了,任务列表会被清空而处理线程会退出,而再有新任务到达的时候就没有线程处理任务了。因此,我们更希望有这样一种机制:让工作线程阻塞以等待新的任务的到达。信号量可以很方便地做到这一点。信号量是一个用于协调多个线程的计数器。如互斥体一样,GNU/Linux保证对信号量的取值和赋值操作都是安全的,不会造成竞争状态。每个信号量都有一个非负整数作为计数。信号量 阅读全文

posted @ 2013-02-03 21:17 michile 阅读(298) 评论(0) 推荐(0) 编辑

advacing lnux program --互斥体[copy]

摘要: 对于刚才这个任务队列竞争状态问题的解决方法就是限制在同一时间只允许一个线程访问任务队列。当一个线程开始检查任务队列的时候,其它线程应该等待直到第一个线程决定是否处理任务,并在确定要处理任务时删除了相应任务之后才能访问任务队列。要实现等待这个操作需要操作系统的支持。GNU/Linux提供了互斥体(mutex,全称UTual EXclusion locks,互斥锁)。互斥体是一种特殊的锁:同一时刻只有一个线程可以锁定它。当一个锁被某个线程锁定的时候,如果有另外一个线程尝试锁定这个互斥体,则这第二个线程会被阻塞,或者说被置于等待状态。只有当第一个线程释放了对互斥体的锁定,第二个线程才能从阻塞状态恢复 阅读全文

posted @ 2013-02-03 20:50 michile 阅读(415) 评论(0) 推荐(0) 编辑

线程专有数据(Thread-Specific Data)

摘要: Unlike processes, all threads in a single program share the same address space.Thismeans that if one thread modifies a location in memory (for instance, a global vari-able), the change is visible to all other threads.This allows multiple threads to operateon the same data without the use interproces 阅读全文

posted @ 2013-02-03 19:54 michile 阅读(1249) 评论(0) 推荐(0) 编辑

advacing lnux program --Thread Cancelation[copy]

摘要: Under normal circumstances, a thread terminates when it exits normally, either byreturning from its thread function or by calling pthread_exit. However, it is possiblefor a thread to request that another thread terminate.This is called canceling a thread.To cancel a thread, call pthread_cancel, pass 阅读全文

posted @ 2013-02-03 13:12 michile 阅读(222) 评论(0) 推荐(0) 编辑

advacing lnux program --4.1.5 Thread Attributes[copy]

摘要: Thread attributes provide a mechanism for fine-tuning the behavior of individual threads. Recall that pthread_createaccepts an argument that is a pointer to a threadattribute object. If you pass a null pointer, the default thread attributes are used toconfigure the new thread. However, you may creat 阅读全文

posted @ 2013-02-03 12:56 michile 阅读(219) 评论(0) 推荐(0) 编辑

advacing lnux program --Threads Return Value[copy]

摘要: #include <pthread.h>#include <stdio.h>/* Compute successive prime numbers (very inefficiently). Return theNth prime number, where N is the value pointed to by *ARG. */void* compute_prime (void* arg){int candidate = 2;int n = *((int*) arg);while (1) {int factor;int is_prime = 1;/* Test pr 阅读全文

posted @ 2013-02-03 11:03 michile 阅读(200) 评论(0) 推荐(0) 编辑

advacing lnux program --Joining Threads[copy]

摘要: One solution is to force mainto wait until the other two threads are done.What weneed is a function similar to waitthat waits for a thread to finish instead of a process.That function is pthread_join,which takes two arguments: the thread ID of thethread to wait for, and a pointer to a void*variable 阅读全文

posted @ 2013-02-03 10:50 michile 阅读(124) 评论(0) 推荐(0) 编辑

advacing lnux program --thread[copy]

摘要: We’ve seen how a program can fork a child process.The child process is initiallyrunning its parent’s program, with its parent’s virtual memory, file descriptors, and soon copied.The child process can modify its memory, close file descriptors, and the likewithout affecting its parent, and vice versa. 阅读全文

posted @ 2013-02-03 00:00 michile 阅读(137) 评论(0) 推荐(0) 编辑