当在进程中创建一个新线程时,新的执行线程将拥有自己的栈(因为也拥有自己的局部变量),但与它的创建者共享局部变量
,文件描述符,信号处理函数和当前目录状态。
#include<pthread.h>
int pthread_creat(pthread_t *thread , pthread_attr_t *attr , void *(*start_routine)(void *) , void *arg);
这个函数定义看起来相当复杂,其实用起来相当的简单。第一个参数是指向pthread_t 类型数据的指针。线程被创建时,这个指针指向的
变量中将被写入一个标识符。
第二个参数是用于设置线程的属性。我们一般不需要特殊的属性,所以只需要设置参数为NULL。
最后两个参数告诉我们线程将要启动的执行的函数和传递给该函数的参数.
void *(* start_routine)(void *)
上面一行告诉我们必须要传递一个函数地址,该函数以一个指向void的指针为参数,返回的也是一个指向void的指针。
#include<pthread.h>
void pthread_exit(void *retval);
线程通过调pthread_exit函数终止执行,就如同进程在结束时调用exit函数一样。这个函数的作用是,终止调用它的线程并返回一个指向
某个对象的指针。
#include<pthread.h>
int pthread_join(pyhread_t th , void **thread_return);
pthread_join函数在线程中的作用等价进程中用来子进程信息的wait函数,第一个参数指定了要等待的线程,线程通过pthread_creat
返回的标识符来指定。第二个参数是一个真挚,它指向另一个指针,而后者指向线程的返回值。
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
void *thread_function(void *arg);
char message[] = "Hello World";
int main() {
int res;
pthread_t a_thread;
void *thread_result;
res = pthread_create(&a_thread, NULL, thread_function, (void *)message);
if (res != 0) {
perror("Thread creation failed");
exit(EXIT_FAILURE);
}
printf("Waiting for thread to finish...\n");
res = pthread_join(a_thread, &thread_result);
if (res != 0) {
perror("Thread join failed");
exit(EXIT_FAILURE);
}
printf("Thread joined, it returned %s\n", (char *)thread_result);
printf("Message is now %s\n", message);
exit(EXIT_SUCCESS);
}
void *thread_function(void *arg) {
printf("thread_function is running. Argument was %s\n", (char *)arg);
sleep(3);
strcpy(message, "Bye!");
pthread_exit("Thank you for the CPU time");
}
cc -D_REENTRANT -I/usr/include/nptl thread1.c -o thread1 -L/use/lib/nptl -lpthread
cc -D_REENTRANT -I/usr/include/nptl thread1.c -o thread1 -lpthread
取消一个线程
#include<pthread.h>
int pthread_cancel(pthread_t thread);
这个函数的用来给线程发送取消请求。
接收端可以用pthread_setcancelstate设置自己的取消状态。
#include<pthread.h>
int pthread_setcanclestate(int state , int *oldstate);
第一个参数的取值可以是PTHREAD_CANCEL_ENABLE,这个值允许线程接收取消请求,或者是PTHREAD_CANCEL_DISABLE,它的作用是忽略
取消请求。oldstate指针用于获取先前的取消状态。如果你对它没有兴趣,只需传递NULL给它。如果取消请求被接受了,线程就可以进入第二个控
制层次,用pthread_setcanceltype设置取消类型。
#include<pthread.h>
int pthread_setcanceltype(int type , int *oldtype);
type参数可以有两种取值,一个是PTHREAD_CANCEL_ASYNCHRONOUS,它将使在接收到取消请求后立刻采取行动。另一个是
PTHREAD_CANCEL_DEFERRED,它将使得在接收到取消请求后,一直等待直到线程执行下列函数之一后才采取行动。具体的函数是pthread_join
pthread_wait,pthread_cond_wait,pthread_ cond_timedwait , pthread_testcancel , sem_wait和sigwait。
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
void *thread_function(void *arg);
int main() {
int res;
pthread_t a_thread;
void *thread_result;
res = pthread_create(&a_thread, NULL, thread_function, NULL);
if (res != 0) {
perror("Thread creation failed");
exit(EXIT_FAILURE);
}
sleep(3);
printf("Canceling thread...\n");
res = pthread_cancel(a_thread);
if (res != 0) {
perror("Thread cancelation failed");
exit(EXIT_FAILURE);
}
printf("Waiting for thread to finish...\n");
res = pthread_join(a_thread, &thread_result);
if (res != 0) {
perror("Thread join failed");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
void *thread_function(void *arg) {
int i, res, j;
res = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
if (res != 0) {
perror("Thread pthread_setcancelstate failed");
exit(EXIT_FAILURE);
}
res = pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
if (res != 0) {
perror("Thread pthread_setcanceltype failed");
exit(EXIT_FAILURE);
}
printf("thread_function is running\n");
for(i = 0; i < 10; i++) {
printf("Thread is still running (%d)...\n", i);
sleep(1);
}
pthread_exit(0);
}