小记——linux线程
来源:互联网 发布:js固定悬浮导航 编辑:程序博客网 时间:2024/06/05 19:10
linux线程标准是POSIX threads, 也就是著名的 Pthreads。
Pthreads实现于glibc库中,分别有两个版本:LinuxThreads和Native POSIX Thread Library (NPTL) 。libc2.6后包含了NPTL。后续版本是NGPL(Next Generation POSIX Threads)。
现在一般说的linux线程多指NPTL版本,本文也只讨论NPTL版本的线程及其相关API。
头文件#include <pthread.h>
APIS:
1. 创建线程
int pthread_create (pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);Upon successful invocation, a new thread is created and begins executing the function provided by start_routine, passed the sole argument arg.
线程创建API的特别之处在于其失败时并不设置errno。下面是示例代码:
pthread_t tread;int ret;ret = pthread_create (&thread, NULL, start_routine, NULL);if (!ret) {errno = ret;perror("pthread_create");return -1;}2. 获得自身线程
pthread_t pthread_self (void);Usage is simple, because the function cannot fail
3. 线程相等性判断
int pthread_equal (pthread_t t1, pthread_t t2);
通过线程ID来判断其相等性,相等则返回非零
4. 线程本身退出Threads may terminate under several circumstances, all of which have analogues to process termination:
• If a thread returns from its start routine, it terminates. This is akin to “falling off the end” of main().
• If a thread invokes the pthread_exit() function (discussed subsequently), it terminates. This is akin to calling exit().
• If the thread is canceled by another thread via the pthread_cancel() function, it terminates. This is akin to being sent the SIGKILL signal via kill().
4.1 结束自己
void pthread_exit (void *retval);
4.2 结束其它线程
int pthread_cancel (pthread_t thread);该函数会阻塞直到指定的线程结束,当指定的线程是不可被其它线程结束时,该函数会一直阻塞。
一个线程可以设置为可被其它线程结束和不可被其它线程结束,缺省为可以被其它线程结束。
4.2.1
int pthread_setcancelstate (int state, int *oldstate);
设置线程可被其它线程结束与否
state may be PTHREAD_CANCEL_ENABLE or PTHREAD_CANCEL_DISABLE, to enable or disable cancellation, respectively.4.2.2
int pthread_setcanceltype (int type, int *oldtype);设置结束类型:异步或者延迟
type may be PTHREAD_CANCEL_ASYNCHRONOUS or PTHREAD_CANCEL_DEFERRED, to use asynchronous or deferred cancellation, respectively
5. 等待线程结束
int pthread_join (pthread_t thread, void **retval);如果等待的线程已经结束,函数将立刻返回。
Once thread terminates, the invoking thread is woken up and, if retval is not NULL, provided the return value the terminated thread passed to pthread_exit() or returned from its start routine.
6. 分离线程
int pthread_detach (pthread_t thread);
对于不希望join的线程,可将其detach.
On success, pthread_detach() detaches the thread specified by thread and returns zero. Results are undefined if you call pthread_detach() on a thread that is already detached. On error, the function returns ESRCH indicating that thread is invalid.
EXAMPLE:
#include <stdlib.h>#include <stdio.h>#include <pthread.h>void * start_thread (void *message){printf ("%s\n", (const char *) message);return message;}int main (void){pthread_t thing1, thing2;const char *message1 = "Thing 1";const char *message2 = "Thing 2";/* Create two threads, each with a different message. */pthread_create (&thing1, NULL, start_thread, (void *) message1);pthread_create (&thing2, NULL, start_thread, (void *) message2);/** Wait for the threads to exit. If we didn't join here,* we'd risk terminating this main thread before the* other two threads finished.*/pthread_join (thing1, NULL);pthread_join (thing2, NULL);return 0;}7. 互斥变量(Pthread Mutexes)
互斥变量仅仅能被一个线程使用。
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;int pthread_mutex_lock (pthread_mutex_t *mutex);int pthread_mutex_unlock (pthread_mutex_t *mutex);实现局部锁:
class ScopedMutex {public:ScopedMutex (pthread_mutex_t& mutex):mutex_ (mutex){ pthread_mutex_lock (&mutex_);} ~ScopedMutex (){ pthread_mutex_unlock (&mutex_);}private: pthread_mutex_t& mutex_;};
- 小记——linux线程
- 小记——linux目录
- 小记——linux信号
- 小记——linux时间
- 小记——linux睡眠
- linux 关于线程不安全函数localtime小记
- 小记——linux文件元属性
- 小记——linux文件名与链接
- 小记——linux定时器之alarm
- 线程小记
- 线程小记
- 线程小记
- 线程小记
- Linux——线程
- linux——线程
- linux—线程使用
- 小记——linux复制文件与重命名
- Linux小记——开机之后怎么到了救援模式?
- 【转载】JAVA知识点(中)
- The MAX(sort) hd 1803
- Android TV APPs 的介绍与创建
- @Aspect注解形式实现AOP实例
- 有趣的Autolayout示例-Masonry实现
- 小记——linux线程
- 高级语言调用系统命令
- 【转载】JAVA知识点(下)
- 数组拼接成字符串
- linux 下查看机器是cpu是几核的
- 第四部分:UI开发常用控件(TextView、Button、EditText、ImageView、ProgressBar、AlertDialog、ProgressDialog)
- StringBuffer和StringBuilder编程遇到的问题
- 杭电1084What Is Your Grade?
- MATLAB画固定大小的彩色图片