#include <pthread.h>
int pthread_create ( pthread_t* thread, const pthread_attr_t* attr,
void * (*start_routine)(void*) , void* arg);#include <pthread.h> void pthread_exit ( void* retval );
#include <pthread.h> int pthread_join( pthread_t thread, void** retval );
#include <pthread.h> int pthread_cancel ( pthread_t thread );
#include <pthread.h> int pthread_setcancelstate( int state, int *oldstate ); int pthread_setcanceltype ( int type, int *oldtype );
#include <bits/pthreadtypes.h>
#define __SIZEOF_PTHREAD_ATTR_T 36
typedef union
{
char __size[__SIZEOF_PTHREAD_ATTR_T];
long int __align;
} pthread_attr_t;#include <semaphore.h> int sem_init ( sem_t* sem, int pshared, unsigned int value ); // 初始化一个未命名的信号量 int sem_destroy ( sem_t* sem ); // 用于销毁信号量,以释放其占用的内核资源 int sem_wait ( sem_t*sem ); // 以原子操作的方式将信号量减1,如果信号量的值为0,则阻塞,直到该值不为0. int sem_trywait ( sem_t* sem ); // sem_wait的非阻塞版本 int sem_post ( sem_t* sem ); // 以原子操作的方式将信号量的值加1
#include <pthread.h> int pthread_mutex_init ( pthread_mutex_t* mutex, const pthread_mutexattr_t* mutexattr ); int pthread_mutex_destroy ( pthread_mutex_t* mutex ); int pthread_mutex_lock ( pthread_mutex_t* mutex ); int pthread_mutex_trylock ( pthread_mutex_t* mutex ); int pthread_mutex_unlock ( pthread_mutex_t* mutex );
#include <pthread.h> /* 初始化互斥锁属性对象 */ int pthread_mutexattr_init ( pthread_mutexattr_t* attr ); /* 销毁互斥锁属性对象 */ int pthread_mutexattr_destroy ( pthread_mutexattr_t* attr ); /* 获取和设置互斥锁的pshared属性 */ int pthread_mutexattr_getpshared ( const pthread_mutexattr_t* attr, int * pshared ); int pthread_mutexattr_setpshared ( pthread_mutexattr_t* attr, int pthread ); /* 获取和设置互斥锁的type属性 */ int pthread_mutexattr_gettype ( const pthread_mutexattr_t* atr, int * type ); int pthread_mutexattr_settype ( pthread_mutexattr_t* attr, int type );
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
int a = 0;
int b = 0;
pthread_mutex_t mutex_a;
pthread_mutex_t mutex_b;
void* another( void* arg )
{
pthread_mutex_lock( &mutex_b );
printf( "in child thread, got mutex b, waiting for mutex a\n" );
sleep( 5 );
++b;
pthread_mutex_lock( &mutex_a );
b += a++;
pthread_mutex_unlock( &mutex_a );
pthread_mutex_unlock( &mutex_b );
pthread_exit( NULL );
}
int main()
{
pthread_t id;
pthread_mutex_init( &mutex_a, NULL );
pthread_mutex_init( &mutex_b, NULL );
pthread_create( &id, NULL, another, NULL );
pthread_mutex_lock( &mutex_a );
printf( "in parent thread, got mutex a, waiting for mutex b\n" );
sleep( 5 );
++a;
pthread_mutex_lock( &mutex_b );
a += b++;
pthread_mutex_unlock( &mutex_b );
pthread_mutex_unlock( &mutex_a );
pthread_join( id, NULL );
pthread_mutex_destroy( &mutex_a );
pthread_mutex_destroy( &mutex_b );
return 0;
}#include <pthread.h> int pthread_cond_init (pthread_cond_t* cond, const pthread_condattr_t* cond_attr); int pthread_cond_destroy ( pthread_cond_t* cond ); int pthread_cond_broadcast ( pthread_cond_t* cond ); //以广播的形式唤醒一个等待目标条件变量的线程 int pthread_cond_signal ( pthread_cond_t* cond ); //唤醒一个等待目标条件变量的线程 int pthread_cond_wait ( pthread_cond_t* cond, pthread_mutex_t* mutex ); // 等待目标条件变量,mutex参数保证对条件变量及其等待队列的操作原子性。
#ifndef LOCKER_H
#define LOCKER_H
#include <exception>
#include <pthread.h>
#include <semaphore.h>
class sem
{
public:
sem()
{
if( sem_init( &m_sem, 0, 0 ) != 0 )
{
throw std::exception();
}
}
~sem()
{
sem_destroy( &m_sem );
}
bool wait()
{
return sem_wait( &m_sem ) == 0;
}
bool post()
{
return sem_post( &m_sem ) == 0;
}
private:
sem_t m_sem;
};
class locker
{
public:
locker()
{
if( pthread_mutex_init( &m_mutex, NULL ) != 0 )
{
throw std::exception();
}
}
~locker()
{
pthread_mutex_destroy( &m_mutex );
}
bool lock()
{
return pthread_mutex_lock( &m_mutex ) == 0;
}
bool unlock()
{
return pthread_mutex_unlock( &m_mutex ) == 0;
}
private:
pthread_mutex_t m_mutex;
};
class cond
{
public:
cond()
{
if( pthread_mutex_init( &m_mutex, NULL ) != 0 )
{
throw std::exception();
}
if ( pthread_cond_init( &m_cond, NULL ) != 0 )
{
pthread_mutex_destroy( &m_mutex );
throw std::exception();
}
}
~cond()
{
pthread_mutex_destroy( &m_mutex );
pthread_cond_destroy( &m_cond );
}
bool wait()
{
int ret = 0;
pthread_mutex_lock( &m_mutex );
ret = pthread_cond_wait( &m_cond, &m_mutex );
pthread_mutex_unlock( &m_mutex );
return ret == 0;
}
bool signal()
{
return pthread_cond_signal( &m_cond ) == 0;
}
private:
pthread_mutex_t m_mutex;
pthread_cond_t m_cond;
};
#endif
服务器编程入门(9)多线程编程,布布扣,bubuko.com
原文:http://blog.csdn.net/zs634134578/article/details/20706741