POSIX thread library 简介I
(1) 进程和线程有关背景知识
1. 进程包括程序资源和程序执行状态等相关信息:
Process ID, Process Group ID, User ID and Group ID
Environment
Working Directory
Program Instructions
Registers
Stack
Heap
File Desprictors
Singal Action/Singal Libraries
Inter-Process Communication Tools(Such as message queues, pipes, semaphors, shared memory)
2.一个进程中的多个线程共享一下资源:
Address Space
Process Instruction
Open Files
Singals/Singal Handlers
Working Directory
User and Group ID
但是每个线程拥有一下唯一资源:
Therad ID
Registers/Stack Pointer
Stack For Local Variables/Return Address
Singal Mask
Priority
Return Value
(二) POSIX(Portable Operating System Interface),中文名称为“可移植操作系统接口”,是IEEE为了
保持Unix和其他操作系统之间的兼容性为定义的一组操作的API。
POSIX thread library是针对C/C++多线程编程的标准库,头文件形式为<pthread.h>。
pthread API将线程函数分为四个主要的部分:
1.线程管理(Thread Management):包括线程的eating、detaching以及joining。
2.Mutexes:用来处理线程之间的同步(Synchronization),是“Mutex Exclusion”的缩写。
3.Condition Variables:用来处理共享一个mutex的几个线程之间的通信。
4.Synchronization:包括读写locks和barriers。
介绍几个常见的函数:
1.线程创建函数:int pthread_create(pthread_t* thread, const pthread_attr_t* attr,
void* (*start_routine)(void*), void* arg);
参数意义:
thread: 返回线程ID。
attr: 线程属性,设置为NULL,使用默认的线程属性。
start_routine: 函数指针指向线程将要调用的函数,该函数有一个void类型的指针值。
arg: 指向线程调用函数实参的指针。如果要向该函数传递多个参数,该指针要指向一个结构体。
返回值:
如果线程创建成功,返回0,;否则返回一个数值代表出现错误。
2.线程结束函数:void pthread_exit(void* extr);
参数意义:
extr: 线程的返回值。该函数用来终结线程,无返回值。
例如有如下示例程序:
1 #include <iostream> 2 #include <pthread.h> 3 4 void *print_message_function(void *ptr) { 5 char* message = NULL; 6 message = (char*)ptr; 7 std::cout << message << std::endl; 8 } 9 10 int main(int argc, char* argv[]) { 11 pthread_t th1, th2; 12 char *message1 = "Thread 1"; 13 char *message2 = "Thread 2"; 14 15 int iret1 = 0; 16 int iret2 = 0; 17 iret1 = pthread_create(&th1, NULL, &print_message_function, (void*)message1); 18 iret2 = pthread_create(&th2, NULL, *print_message_function, (void*)message2); 19 20 pthread_join(th1, NULL); 21 pthread_join(th2, NULL); 22 23 return 0; 24 }
3. 等待另外一个线程结束:int pthread_join(pthread_t thread, void** thread_return);
参数意义:
thread: 线程被挂起,直到ID为thread的线程执行结束。
thread_return: 如果该参数不为空,则线程thread的返回值存储在指针thread_return所指的内存位置。
返回值:
成功返回值为零,否则返回一个非零值指示操作不成功。
例如,有如下例子:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream.h> 2 #include <pthread.h> 3 #include <stdlib.h> 4 #include <math.h> 5 6 void* BusyWork(void* t) { 7 int i = 0; 8 long tid = 0; 9 double res = 0.0; 10 tid = (long)t; 11 std::cout << "Thread " << tid << "Starting......\n"; 12 for (int i = 0; i < 1000000; i++) { 13 ren = (res + sin(i) + tan(i)); 14 } 15 std::cout << "Thread " << tid << " Done......\n"; 16 pthread_exit((void*)t); 17 } 18 19 int main(int argc, char* argv[]) { 20 pthread_t thread[4]; 21 ptread_attr_t attr; 22 int rc = 0; 23 void* status = 0; 24 25 pthread_attr_init(&attr); 26 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); 27 28 for (int i = 0; i < 4; i++) { 29 std::cout << "Main:Thread " << i << " Creating......\n"; 30 rc = pthread_create(&thread[i], &attr, BusyWork, 31 (void*)i); 32 if (rc) { 33 std::cout << "ERROR,return code from pthread_create() is " << rc << std::endl; 34 exit(-1); 35 } 36 } 37 pthread_attr_destory(&attr); 38 39 for (int i = 0; i < 4; i++) { 40 rc = pthreat_join(&thread[i], &status); 41 if (rc) { 42 std::cout << "ERROR:return code from pthread_join() is: " << rc << std::endl; 43 } 44 std::cout << "Main:Completed join with thread " << i << "having a status " << (long)status << std::endl; 45 } 46 std::cout << "Main,program completed. Exiting." << std::endl; 47 pthread_exit(0); 48 return 0; 49 }
posted on 2013-09-08 02:10 Persistence 阅读(614) 评论(0) 编辑 收藏 举报