注意:pthread_t为unsigned long类型
例:
/*void *thread_func(void *arg);*/
void *thread_func(void *arg)
{
int *pval = (int *)arg;
int val = *(int *)arg;
}
pthread_t tid;
int val = 10;
char buf[N] = "hello world";
int ret = 0;
ret = pthread_create(&tid,NULL,thread_func,&val);
if(ret != 0)
{
/*errno = ret;*/
/*perror();*/
fprintf(stderr,"pthread_create : %s\n",strerror(errno));
exit(EXIT_FAILURE);
}
//建议用法
#define handle_error_en(en, msg) \
do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
---------------------------------------------------------------------
void *thread_func(void *arg)
{
char *p = (char *)arg;
}
s = pthread_create(&thr, NULL, &thread_func, buf);
if (s != 0)
handle_error_en(s, "pthread_create");
pthread_t pthread_self(void);
//功能:获得当前线程的tid ,类似进程中的getpid(2),该函数总是成功
练习:创建两个线程,线程1不传参,线程1每秒循环打印自己的PID和TID,死循环;
2.线程退出
void pthread_exit(void *retval);
功能:结束当前线程的执行
参数:
retval 给等待的线程带回一个地址值 ,如果没有值带回,写为NULL
返回值:无
3.线程连接
等待线程退出,作用类似进程中的wait(2)系统调用,可实现线程同步
int pthread_join(pthread_t thread, void **retval);
功能:阻塞方式等待指定的线程退出,并且释放结束线程未释放的资源(例如:线程的私有栈资源等)
参数:
thread 线程TID
retval 获得pthread_exit带回的地址值
返回值:
成功返回0,失败返回错误码
注意:如果等待的线程没有退出,则调用pthread_join的线程会阻塞
例:
//thread1 中调用pthread_exit();
//thread2 中调用pthread_join();
thread1 :tid1
1.
static int val;
val = *(int *)arg;
pthread_exit(&val);
return &val;
2.
int *pval = (int *)malloc(sizeof(int));
pthread_exit(pval);
//===================================================
thread2 :tid2
int *retval = NULL;
pthread_join(tid1,(void **)&retval);
*retval <=> val
4.线程分离。
//提示:
//打开文件获得文件描述符
int fd = open();
//创建线程传文件描述符
pthread_create(&tid1,NULL,write_file,&fd);
//线程函数接收参数
void *write_file(void *arg)
{
int fd = *(int *)arg;
}
//主线程等待两线程退出
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
#include
#include
#include
#include
#include
//return errno
#define handle_error_en(en, msg) \
do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
//set errno
#define handle_error(msg) \
do { perror(msg); exit(EXIT_FAILURE); } while (0)
void *thread_fun1(void *arg)
{
static int val = 10;
while(1)
{
printf("child thread-1 pid = %d tid = %lu\n",getpid(),pthread_self());
sleep(1);
// pthread_exit(&val);
pthread_exit("i am died\n");
}
}
void *thread_fun2(void *arg)
{
int n = 10;
while(n--)
{
printf("child-2 argv = %d\n",(*(int*)arg)++);
sleep(1);
//exit(0);
//pthread_exit(0);
//pthread_exit(NULL);
//return ;
}
}
int main(int argc, const char *argv[])
{
pthread_t tid1,tid2;
int n = 0;
int val = 0;
int i = 5;
int *retval = NULL;
char *s = NULL;
if ((n = pthread_create(&tid1,NULL,thread_fun1,NULL)) != 0)
{
handle_error_en(n,"pthread_create");
}
if ((n = pthread_create(&tid2,NULL,thread_fun2,&val)) != 0)
{
handle_error_en(n,"pthread_create");
}
while(i--)
{
printf(" main thread val = %d tid1 = %lu tid2 = %lu\n",val,tid1,tid2);
sleep(1);
//exit(0);
//pthread_join(NULL,&retval);
pthread_cancel(tid2);
}
//pthread_join(tid1,(void **)&retval);
pthread_join(tid1,(void **)&s);
//printf("child thread 1 exit status %d\n",*retval);
printf("I know on my god! : %s\n",s);
return 0;
}
int init_sem(sem_t *sem,unsigned int value)
{
if(sem_init(sem,0,value) < 0)
{
perror("sem_init");
exit(EXIT_FAILURE);
}
return 0;
}
(3)P操作 : 申请资源,可能会引起调用线程阻塞
int P(sem_t *sem)
{
if(sem_wait(sem) < 0)
{
perror("sem_wait");
exit(EXIT_FAILURE);
}
return 0;
}
(4)V操作 : 释放资源
int V(sem_t *sem)
{
if(sem_post(sem) < 0)
{
perror("sem_post");
exit(EXIT_FAILURE);
}
return 0;
}
2.线程间互斥