1、线程概念
•
一个典型的
Unix
进程可以认为是单一线程
•
多线程的好处:
•
1
、简化异步事件的处理,单独一个线程专职处理事件类型
•
2
、简化内存地址空间及文件描述符的共享
•
3
、可以提升整体吞吐量
•
4
、交互程序可以提高响应速度
•
线程包括用以描述一个执行上下文的必要信息:
•
线程
ID
、一组寄存器值、一个堆栈、一个调度优先级、信号掩码、
errno
变量、线程特定数据
•
可以检测宏
_POSIX_THREADS
,以确定当前环境是否支持线程
2、线程标识
•
线程是归属于进程的,每个线程有一个线程
ID
,只在所属的进程内有意义
•
比较线程
ID
是否相等
•
#include <
pthread.h
>
•
int
pthread_equal
(
pthread_t
tid1,
pthread_t
tid2);
•
线程获取自己的线程
ID
•
pthread_t
pthread_self
(void);
3、创建线程
•
一个进程启动时,初始只有一个线程
•
创建新的线程
•
int
pthread_create
(
pthread_t
*restrict
tidp
,
const
pthread_attr_t
*restrict
attr
, void *(*
start_rtn
)(void *), void *restrict
arg
);
•
参数
tidp
,保存新建的线程
ID
•
attr
,指定线程属性,可以指定为
NULL
•
start_rtn
,指定线程的入口函数,它只有单个参数
arg
•
如果给入口函数传递多个参数,封装在一个结构中,在
arg
中存放结构的地址
•
编译命令:
gcc
thrtest.c
-
l
pthread
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void printids(const char *s)
{
printf("%s pid %u tid %u (0x%x)\n",
s, (unsigned int)getpid(),
(unsigned int)pthread_self(), (unsigned int)pthread_self());
}
void * thr_fn(void *p)
{
printids("new thread: ");
return((void *)0);
}
int main(void)
{
pthread_t ntid;
if( pthread_create(&ntid, NULL, thr_fn, NULL) )
{
perror("can't create thread");
exit(0);
}
printf("ntid=%#x\n", ntid);
printids("main thread:");
sleep(1); /* Giving a chance for the new thread to run */
exit(0);
}
4、线程终止
•
如果进程内的任何一个线程调用
exit
,整个进程将终止
•
单个线程的终止可以有
3
种方式:
•
1
、从线程自己的入口函数
return
•
2
、被同一个进程内的另外一个线程
cancel
•
3
、调用
pthread_exit
•
void
pthread_exit
(void *
rval_ptr
);
•
int
pthread_join
(
pthread_t
thread, void **
rval_ptr
); //
等待线程终止
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void * thr_fn1(void *arg)
{
printf("thread 1 returning\n");
return((void *)1);
}
void * thr_fn2(void *arg)
{
printf("thread 2 exiting\n");
pthread_exit((void *)2);
}
void err_quit(char *p)
{
perror(p);
exit(0);
}
int main(void)
{
int err;
pthread_t tid1, tid2;
void *tret;
err = pthread_create(&tid1, NULL, thr_fn1, NULL);
if( err != 0 ) err_quit("can't create thread 1");
err = pthread_create(&tid2, NULL, thr_fn2, NULL);
if( err != 0 ) err_quit("can't create thread 2");
err = pthread_join(tid1, &tret);
if( err != 0 ) err_quit("can't join with thread 1");
printf("thread 1 exit code %d\n", (int)tret);
err = pthread_join(tid2, &tret);
if( err != 0 ) err_quit("can't join with thread 2");
printf("thread 2 exit code %d\n", (int)tret);
exit(0);
}
•
注意:
pthread_exit
()
不能返回一个局部变量的地址
一个错误示例:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
struct foo {
int a, b, c, d;
};
void err_quit(char *p)
{
perror(p);
exit(0);
}
void printfoo(const char *s, const struct foo *fp)
{
printf(s);
printf(" structure at 0x%x\n", (unsigned)fp);
printf(" foo.a = %d\n", fp->a);
printf(" foo.b = %d\n", fp->b);
printf(" foo.c = %d\n", fp->c);
printf(" foo.d = %d\n", fp->d);
}
void * thr_fn1(void *arg)
{
struct foo foo = {1, 2, 3, 4};
printfoo("thread 1:\n", &foo);
pthread_exit((void *)&foo);
}
void * thr_fn2(void *arg)
{
printf("thread 2: ID is %u\n", pthread_self());
pthread_exit((void *)0);
}
int main(void)
{
int err;
pthread_t tid1, tid2;
struct foo *fp;
err = pthread_create(&tid1, NULL, thr_fn1, NULL);
if (err != 0) err_quit("can't create thread 1");
err = pthread_join(tid1, (void *)&fp);
if (err != 0) err_quit("can't join with thread 1");
sleep(1);
printf("parent starting second thread\n");
err = pthread_create(&tid2, NULL, thr_fn2, NULL);
if (err != 0) err_quit("can't create thread 2");
sleep(1);
printfoo("parent:\n", fp);
exit(0);
}
•
int
pthread_cancel
(
pthread_t
tid
); //
请求线程终止
•
线程清理处理(回顾一下进程退出时的清理,
atexit
()
)
•
void
pthread_cleanup_push
(void (*
rtn
)(void *), void *
arg
);
•
void
pthread_cleanup_pop
(
int
execute);
•
必须成对使用
•
清理函数的调用时机:
•
1
、当线程调用
pthread_exit
()
时
•
2
、线程被
cancel
时
•
3
、使用非
0
参数调用
pthread_cleanup_pop
()
时
•
注意:线程
return
时,并不执行清理函数
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void cleanup(void *arg)
{
printf("cleanup: %s\n", (char *)arg);
}
void * thr_fn1(void *arg)
{
printf("thread 1 start\n");
pthread_cleanup_push(cleanup, "thread 1 first handler");
pthread_cleanup_push(cleanup, "thread 1 second handler");
printf("thread 1 push complete\n");
if (arg) return((void *)1);
pthread_cleanup_pop(0);
pthread_cleanup_pop(0);
return((void *)1);
}
void * thr_fn2(void *arg)
{
printf("thread 2 start\n");
pthread_cleanup_push(cleanup, "thread 2 first handler");
pthread_cleanup_push(cleanup, "thread 2 second handler");
printf("thread 2 push complete\n");
if (arg) pthread_exit((void *)2);
pthread_cleanup_pop(0);
pthread_cleanup_pop(0);
pthread_exit((void *)2);
}
void err_quit(char *p)
{
perror(p);
exit(0);
}
int
main(void)
{
int err;
pthread_t tid1, tid2;
void *tret;
err = pthread_create(&tid1, NULL, thr_fn1, (void *)1);
if (err != 0) err_quit("can't create thread 1");
err = pthread_create(&tid2, NULL, thr_fn2, (void *)1);
if (err != 0) err_quit("can't create thread 2");
err = pthread_join(tid1, &tret);
if (err != 0) err_quit("can't join with thread 1");
printf("thread 1 exit code %d\n", (int)tret);
err = pthread_join(tid2, &tret);
if (err != 0) err_quit("can't join with thread 2");
printf("thread 2 exit code %d\n", (int)tret);
exit(0);
}
•
int
pthread_detach
(
pthread_t
tid
); //
分离一个线程
•
线程结束后,资源立即释放,不能通过
pthread_join
()
获取其退出状态
•
备注:调用
pthread_join
后,自动将线程置为
detached
状态
5、
线程同步
•
互斥体
•
int
pthread_mutex_init
(
pthread_mutex_t
*restrict
mutex
,
const
pthread_mutexattr_t
*restrict
attr
);
•
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
);
•
读写锁
•
int
pthread_rwlock_init
(
pthread_rwlock_t
*restrict
rwlock
,
const
pthread_rwlockattr_t
*restrict
attr
);
•
int
pthread_rwlock_destroy
(
pthread_rwlock_t
*
rwlock
);
•
int
pthread_rwlock_rdlock
(
pthread_rwlock_t
*
rwlock
);
•
int
pthread_rwlock_wrlock
(
pthread_rwlock_t
*
rwlock
);
•
int
pthread_rwlock_unlock
(
pthread_rwlock_t
*
rwlock
);
•
int
pthread_rwlock_tryrdlock
(
pthread_rwlock_t
*
rwlock
);
•
int
pthread_rwlock_trywrlock
(
pthread_rwlock_t
*
rwlock
);
•
条件变量
•
int
pthread_cond_init
(
pthread_cond_t
*restrict
cond
,
pthread_condattr_t
*restrict
attr
);
•
int
pthread_cond_destroy
(
pthread_cond_t
*
cond
);
•
int
pthread_cond_wait
(
pthread_cond_t
*restrict
cond
,
pthread_mutex_t
*restrict
mutex
);
•
int
pthread_cond_timedwait
(
pthread_cond_t
*restrict
cond
,
pthread_mutex_t
*restrict
mutex
,
const
struct
timespec
*restrict timeout);
•
int
pthread_cond_signal
(
pthread_cond_t
*
cond
);
•
int
pthread_cond_broadcast
(
pthread_cond_t
*
cond
);
•
条件变量总是与互斥体搭配使用的
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
int a = 0;
pthread_cond_t qready = PTHREAD_COND_INITIALIZER;
pthread_mutex_t qlock = PTHREAD_MUTEX_INITIALIZER;
void * reader(void *arg)
{
for( ;; )
{
pthread_mutex_lock(&qlock);
while( a == 0 )
pthread_cond_wait(&qready, &qlock);
printf("%d: a=%d\n", (int)arg, a);
a = 0;
pthread_mutex_unlock(&qlock);
}
}
void * writer(void *arg)
{
int i = 1;
for( ; i < 100; i++ )
{
pthread_mutex_lock(&qlock);
a = i;
pthread_mutex_unlock(&qlock);
pthread_cond_signal(&qready); // 通知等待线程:“条件变量已改变”
}
}
void err_quit(char *p)
{
perror(p);
exit(0);
}
int main(void)
{
int i, err;
pthread_t tid1, tid[64];
for( i = 0; i < 64; i++ )
{
err = pthread_create(&tid[i], NULL, reader, (void *)i);
if( err != 0 ) err_quit("can't create thread 2");
}
printf("sleep 3 ...\n");
sleep(3);
err = pthread_create(&tid1, NULL, writer, NULL);
if( err != 0 ) err_quit("can't create thread 1");
sleep(3);
exit(0);
}