unix_c++学习笔记5
内容:
* 多线程编程
1. 多线程
* 运行在同一进程空间的多个函数
* 共享进程空间的 env,全局区,代码区,堆区,表项
* 进程空间的数据栈为每个线程开辟单独的数据栈,代码栈不再使用
线程到LWP(Light Weight Process) 申请代码栈
* os 会提供一些维护线程控制线程的调度
* 一个多线程的程序启动后,main退化为一个线程
2. create一个线程
* 建立后立即进入可执行状态
#include<pthread.h>
int pthread_create( pthread_t *tid, const pthread_attr_t *attr, void* (*func)(void*), void *arg );
* tid 存放新线程的ID
* attr NULL
* func 指向新线程的函数指针
* arg 新线程的参数
* 返回值
0,成功
>0,返回错误号
例子:
// 无参线程
#include<pthread.h>
#include<iostream>
#include<unistd.h>
#include<sys/types.h>
using namespace std;
void * func( void* );
int main(){
cout<<"process id= "<< getpid() <<endl;
pthread_t tid;
if( pthread_create( &tid, NULL, func, NULL ) ){
cout<<"create thread error "<<endl;
exit( -1 );
}else{
cout<<"create thread successful, tid = "<< tid <<endl;
}
while( 1 ){
cout<<"int main function... "<<endl;
sleep( 1 );
}
return 0;
}
void * func( void* arg ){
while( 1 ){
cout<< pthread_self() <<" running ... "<<endl;
sleep( 1 );
}
}
// 有参线程
#include<pthread.h>
#include<unistd.h>
#include<sys/types.h>
#include<iostream>
using namespace std;
struct person{
int age;
char name[ 30 ];
};
void* func( void* );
int main(){
pthread_t tid;
struct person p;
memset( &p, 0x00, sizeof( p ) );
p.age=20;
strcpy( p.name, "tom" );
if( pthread_create( &tid, NULL, func, (void*)&p ) ){
cout<<"create thread error ... "<<endl;
exit( -1 );
}else{
cout<<"create successful, tid = "<< tid <<endl;
}
//sleep( 5 );
return 0;
}
void* func( void* arg ){
struct person * p=( struct person* )arg;
cout<<"person[ "<< p->name <<","<< p->age <<" ]"<<endl;
}
3. join 函数
* 等待一个线程的结束,阻塞函数
#include<pthread.h>
int pthread_join( pthread_t tid, void** status );
* tid, 等待的线程号
* status, 存储线程的返回值
* 返回值:
0, 成功
>0, 返回错误号
例子:
#include<iostream>
#include<pthread.h>
#include<sys/types.h>
#include<unistd.h>
using namespace std;
void* func( void* );
int main(){
pthread_t tid;
if( pthread_create( &tid, NULL, func, NULL ) ){
cout<<"create error"<<endl;
exit( -1 );
}else{
cout<<"ok tid = "<< tid <<endl;
}
cout<<"begain join ..."<<endl;
int * p;
pthread_join( tid, (void**)&p );
cout<<"end join ... "<<endl;
//cout<<"thread return "<< *p <<endl; //return (void*)&i;
cout<<"thread return "<< p <<endl; //return (void*)3;
return 0;
}
void *func( void* arg ){
for( int i=0; i<5; i++ ){
cout<<"remaind "<< 5-i <<endl;
sleep( 1 );
}
//static int i=3;
//return (void*)&i;
return (void*)3;
}
4. detach 函数
* 将一个线程设为分离线程,进程放弃对线程的控制权,完全交由kernel
* 如果一个线程被join后设为分离线程,对调用join他的线程状态不变
如果设为分离线程后调用join则无效
#include<pthread.h>
int pthread_detach( pthread_t tid );
* tid, 被设为分离线程的ID
* 返回值:
0, 成功
>0, 返回错误号
例子:
#include<iostream>
#include<pthread.h>
#include<sys/types.h>
#include<unistd.h>
using namespace std;
void* func( void* arg ){
for( int i=0; i<5; i++ ){
cout<<"remaind "<< 5-i <<endl;
sleep( 1 );
}
}
int main(){
pthread_t tid;
if( pthread_create( &tid, NULL, func, NULL ) ){
exit( -1 );
}else{
cout<<"ok tid = "<< tid <<endl;
}
pthread_detach( tid );
cout<<"------------before join-----------"<<endl;
if( pthread_join( tid, NULL ) ){
cout<<"join error"<<endl;
//exit( -1 );
}else
cout<<"join ok"<<endl;
cout<<"------------after join------------"<<endl;
sleep( 10 );
return 0;
}
5. 线程的互斥锁
* 如果该锁被其他线程锁住了,则本线程被阻塞
#include<pthread.h>
int pthread_mutex_lock( pthread_mutex_t *mptr ); //拿不到则阻塞
int pthread_mutex_trylock( pthread_mutex_t *mptr ); // 拿不到则返回
* mptr, 指向互斥锁的一个指针,该锁可以初始化为
PTHREAD_MUTEX_INITIALIZER
* 返回值:
0, 成功
>0, 返回错误号
例子:
#include<iostream>
#include<pthread.h>
#include<sys/types.h>
#include<unistd.h>
using namespace std;
int g_var = 10;
pthread_mutex_t mutex =PTHREAD_MUTEX_INITIALIZER; // must be global and only one
void* func( void* arg ){
if( pthread_mutex_trylock( &mutex ) ){
cout<<"can't"<<endl;
pthread_exit( NULL );
}else{
cout<<"get lock"<<endl;
}
g_var += 10;
sleep( 1 );
g_var -= 10;
sleep( 1 );
cout<<"in "<< pthread_self() <<" g_var = "<< g_var <<endl;
pthread_mutex_unlock( &mutex );
}
int main(){
pthread_t tid[ 5 ];
for( int i=0; i<5; i++ ){
pthread_create( &tid<i>, NULL, func, NULL );
}
for( int i=0; i<5; i++ ){
pthread_join( tid<i>, NULL );
}
}
6. 条件互斥锁
例子:
#include <pthread.h>
#include <queue>
#include <list>
#include <unistd.h>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAX_C = 20 ;
class PROD{
int _no;
public:
PROD( int i ):_no( i ){}
~PROD(){}
};
void* factor( void* ) ;
void* customer( void* ) ;
queue<PROD *> item ;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER ;
pthread_cond_t cond[2] ={PTHREAD_COND_INITIALIZER ,
PTHREAD_COND_INITIALIZER };
template<class T>
void m_free( T t ){
delete t ;
}
int main(){
pthread_t tid[ MAX_C + 1 ] ;
if(pthread_create( &tid[0] , NULL , factor , NULL )){
cout<<"create factor error. " << endl;
}
sleep( 5 ) ;
for( int i = 1 ; i <= MAX_C ; i++ ){
pthread_create( &tid<i> , NULL , customer , NULL ) ;
}
for( int i = 1 ; i<= MAX_C ; i++ ){
pthread_join( tid<i> , NULL ) ;
}
return 0 ;
}
void* factor( void * arg ){
int prod_no = 0 ;
cout<<"factor running....... " << endl;
while( 1 ){
if( prod_no >= MAX_C * 100 ) break ;
pthread_mutex_lock( &mutex ) ;
if( item.size() == 10 ) {
pthread_cond_wait( &cond[0], &mutex ) ;
}else{
item.push( new PROD( ++prod_no ) ) ;
if( item.size() == 1 ){
pthread_cond_broadcast( &cond[1] ) ;
}
}
pthread_mutex_unlock( &mutex ) ;
}
pthread_exit( NULL ) ;
}
void* customer( void * arg ){
cout<<"customer " << pthread_self() << " is comming.... " << endl;
list<PROD * > l ;
while( 1 ){
if( l.size() >= 100 ) break ;
pthread_mutex_lock( &mutex ) ;
if( item.size() == 0 ){
pthread_cond_broadcast( &cond[0] );
pthread_cond_wait( &cond[1] , &mutex ) ;
}else{
l.push_back( item.front() ) ;
item.pop() ;
}
pthread_mutex_unlock( &mutex ) ;
}
for_each( l.begin() , l.end() , m_free< PROD* > ) ;
cout<<"customer " << pthread_self() << " return " << endl;
pthread_exit( NULL ) ;
}
* 多线程编程
1. 多线程
* 运行在同一进程空间的多个函数
* 共享进程空间的 env,全局区,代码区,堆区,表项
* 进程空间的数据栈为每个线程开辟单独的数据栈,代码栈不再使用
线程到LWP(Light Weight Process) 申请代码栈
* os 会提供一些维护线程控制线程的调度
* 一个多线程的程序启动后,main退化为一个线程
2. create一个线程
* 建立后立即进入可执行状态
#include<pthread.h>
int pthread_create( pthread_t *tid, const pthread_attr_t *attr, void* (*func)(void*), void *arg );
* tid 存放新线程的ID
* attr NULL
* func 指向新线程的函数指针
* arg 新线程的参数
* 返回值
0,成功
>0,返回错误号
例子:
// 无参线程
#include<pthread.h>
#include<iostream>
#include<unistd.h>
#include<sys/types.h>
using namespace std;
void * func( void* );
int main(){
cout<<"process id= "<< getpid() <<endl;
pthread_t tid;
if( pthread_create( &tid, NULL, func, NULL ) ){
cout<<"create thread error "<<endl;
exit( -1 );
}else{
cout<<"create thread successful, tid = "<< tid <<endl;
}
while( 1 ){
cout<<"int main function... "<<endl;
sleep( 1 );
}
return 0;
}
void * func( void* arg ){
while( 1 ){
cout<< pthread_self() <<" running ... "<<endl;
sleep( 1 );
}
}
// 有参线程
#include<pthread.h>
#include<unistd.h>
#include<sys/types.h>
#include<iostream>
using namespace std;
struct person{
int age;
char name[ 30 ];
};
void* func( void* );
int main(){
pthread_t tid;
struct person p;
memset( &p, 0x00, sizeof( p ) );
p.age=20;
strcpy( p.name, "tom" );
if( pthread_create( &tid, NULL, func, (void*)&p ) ){
cout<<"create thread error ... "<<endl;
exit( -1 );
}else{
cout<<"create successful, tid = "<< tid <<endl;
}
//sleep( 5 );
return 0;
}
void* func( void* arg ){
struct person * p=( struct person* )arg;
cout<<"person[ "<< p->name <<","<< p->age <<" ]"<<endl;
}
3. join 函数
* 等待一个线程的结束,阻塞函数
#include<pthread.h>
int pthread_join( pthread_t tid, void** status );
* tid, 等待的线程号
* status, 存储线程的返回值
* 返回值:
0, 成功
>0, 返回错误号
例子:
#include<iostream>
#include<pthread.h>
#include<sys/types.h>
#include<unistd.h>
using namespace std;
void* func( void* );
int main(){
pthread_t tid;
if( pthread_create( &tid, NULL, func, NULL ) ){
cout<<"create error"<<endl;
exit( -1 );
}else{
cout<<"ok tid = "<< tid <<endl;
}
cout<<"begain join ..."<<endl;
int * p;
pthread_join( tid, (void**)&p );
cout<<"end join ... "<<endl;
//cout<<"thread return "<< *p <<endl; //return (void*)&i;
cout<<"thread return "<< p <<endl; //return (void*)3;
return 0;
}
void *func( void* arg ){
for( int i=0; i<5; i++ ){
cout<<"remaind "<< 5-i <<endl;
sleep( 1 );
}
//static int i=3;
//return (void*)&i;
return (void*)3;
}
4. detach 函数
* 将一个线程设为分离线程,进程放弃对线程的控制权,完全交由kernel
* 如果一个线程被join后设为分离线程,对调用join他的线程状态不变
如果设为分离线程后调用join则无效
#include<pthread.h>
int pthread_detach( pthread_t tid );
* tid, 被设为分离线程的ID
* 返回值:
0, 成功
>0, 返回错误号
例子:
#include<iostream>
#include<pthread.h>
#include<sys/types.h>
#include<unistd.h>
using namespace std;
void* func( void* arg ){
for( int i=0; i<5; i++ ){
cout<<"remaind "<< 5-i <<endl;
sleep( 1 );
}
}
int main(){
pthread_t tid;
if( pthread_create( &tid, NULL, func, NULL ) ){
exit( -1 );
}else{
cout<<"ok tid = "<< tid <<endl;
}
pthread_detach( tid );
cout<<"------------before join-----------"<<endl;
if( pthread_join( tid, NULL ) ){
cout<<"join error"<<endl;
//exit( -1 );
}else
cout<<"join ok"<<endl;
cout<<"------------after join------------"<<endl;
sleep( 10 );
return 0;
}
5. 线程的互斥锁
* 如果该锁被其他线程锁住了,则本线程被阻塞
#include<pthread.h>
int pthread_mutex_lock( pthread_mutex_t *mptr ); //拿不到则阻塞
int pthread_mutex_trylock( pthread_mutex_t *mptr ); // 拿不到则返回
* mptr, 指向互斥锁的一个指针,该锁可以初始化为
PTHREAD_MUTEX_INITIALIZER
* 返回值:
0, 成功
>0, 返回错误号
例子:
#include<iostream>
#include<pthread.h>
#include<sys/types.h>
#include<unistd.h>
using namespace std;
int g_var = 10;
pthread_mutex_t mutex =PTHREAD_MUTEX_INITIALIZER; // must be global and only one
void* func( void* arg ){
if( pthread_mutex_trylock( &mutex ) ){
cout<<"can't"<<endl;
pthread_exit( NULL );
}else{
cout<<"get lock"<<endl;
}
g_var += 10;
sleep( 1 );
g_var -= 10;
sleep( 1 );
cout<<"in "<< pthread_self() <<" g_var = "<< g_var <<endl;
pthread_mutex_unlock( &mutex );
}
int main(){
pthread_t tid[ 5 ];
for( int i=0; i<5; i++ ){
pthread_create( &tid<i>, NULL, func, NULL );
}
for( int i=0; i<5; i++ ){
pthread_join( tid<i>, NULL );
}
}
6. 条件互斥锁
例子:
#include <pthread.h>
#include <queue>
#include <list>
#include <unistd.h>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAX_C = 20 ;
class PROD{
int _no;
public:
PROD( int i ):_no( i ){}
~PROD(){}
};
void* factor( void* ) ;
void* customer( void* ) ;
queue<PROD *> item ;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER ;
pthread_cond_t cond[2] ={PTHREAD_COND_INITIALIZER ,
PTHREAD_COND_INITIALIZER };
template<class T>
void m_free( T t ){
delete t ;
}
int main(){
pthread_t tid[ MAX_C + 1 ] ;
if(pthread_create( &tid[0] , NULL , factor , NULL )){
cout<<"create factor error. " << endl;
}
sleep( 5 ) ;
for( int i = 1 ; i <= MAX_C ; i++ ){
pthread_create( &tid<i> , NULL , customer , NULL ) ;
}
for( int i = 1 ; i<= MAX_C ; i++ ){
pthread_join( tid<i> , NULL ) ;
}
return 0 ;
}
void* factor( void * arg ){
int prod_no = 0 ;
cout<<"factor running....... " << endl;
while( 1 ){
if( prod_no >= MAX_C * 100 ) break ;
pthread_mutex_lock( &mutex ) ;
if( item.size() == 10 ) {
pthread_cond_wait( &cond[0], &mutex ) ;
}else{
item.push( new PROD( ++prod_no ) ) ;
if( item.size() == 1 ){
pthread_cond_broadcast( &cond[1] ) ;
}
}
pthread_mutex_unlock( &mutex ) ;
}
pthread_exit( NULL ) ;
}
void* customer( void * arg ){
cout<<"customer " << pthread_self() << " is comming.... " << endl;
list<PROD * > l ;
while( 1 ){
if( l.size() >= 100 ) break ;
pthread_mutex_lock( &mutex ) ;
if( item.size() == 0 ){
pthread_cond_broadcast( &cond[0] );
pthread_cond_wait( &cond[1] , &mutex ) ;
}else{
l.push_back( item.front() ) ;
item.pop() ;
}
pthread_mutex_unlock( &mutex ) ;
}
for_each( l.begin() , l.end() , m_free< PROD* > ) ;
cout<<"customer " << pthread_self() << " return " << endl;
pthread_exit( NULL ) ;
}
irini
2005-10-17 21:49:06
评论:0
阅读:797
引用:0
