继续以前C语言线程的文章:文章1 文章2 来了解基本的线程操做。html
下面代码中关键的地方在于:python
经过 res = pthread_attr_init(&thread_attr);
初始化一个线程属性编程
经过 res = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
将属性设置为脱离状态(PTHREAD_CREATE_DETACHED
),即不能经过调用 pthread_join
来得到另外一个线程的退出状态segmentfault
另外还有一个经常使用的默认状态是 PTHREAD_CREATE_JOINABLE
,能够容许两个线程从新合并。多线程
属性用完后对其进行清理回收 (void)pthread_attr_destroy(&thread_attr);
框架
经过共享的变量 thread_finished
来检测子线程是否已经结束函数
代码以下:线程
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <pthread.h> void *thread_function(void *arg); char message[] = "Hello World"; int thread_finished = 0; int main() { int res; pthread_t a_thread; pthread_attr_t thread_attr; res = pthread_attr_init(&thread_attr); if (res != 0) { perror("Attribute creation failed"); exit(EXIT_FAILURE); } res = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED); if (res != 0) { perror("Setting detached attribute failed"); exit(EXIT_FAILURE); } res = pthread_create(&a_thread, &thread_attr, thread_function, (void *)message); if (res != 0) { perror("Thread creation failed"); exit(EXIT_FAILURE); } (void)pthread_attr_destroy(&thread_attr); while(!thread_finished) { printf("Waiting for thread to say it's finished...\n"); sleep(1); } printf("Other thread finished, bye!\n"); exit(EXIT_SUCCESS); } void *thread_function(void *arg){ printf("thread_function is running. Argument was %s\n", (char *)arg); sleep(4); printf("Second thread setting finished flag, and exiting now\n"); thread_finished = 1; pthread_exit(NULL); }
线程库提供如下调度策略:设计
| SCHED_FIFO | 先进先出 (FIFO) 调度。每一个线程都有一个固定的优先级;当多个线程具备相同的优先级时,它们按照先进先出 (FIFO) 的顺序运行直到完成 | | SCHED_RR | 循环 (RR) 调度。每一个线程都有固定的优先级;当多个线程具备相同的优先级时,它们按照先进先出 (FIFO) 的顺序在一个 固定的时间片内运行。 | | SCHED_OTHER | 缺省的 AIX® 调度。每一个线程都有一个由调度程序根据线程的活动动态修改的初始优先级;线程的执行是按时间分割的。在其余系统上,这个调度策略可能会不一样。 |
设置调度属性和设置很类似:code
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <pthread.h> void *thread_function(void *arg); char message[] = "Hello World"; int thread_finished = 0; int main() { int res; pthread_t a_thread; pthread_attr_t thread_attr; int max_priority; int min_priority; struct sched_param scheduling_value; res = pthread_attr_init(&thread_attr); if (res != 0) { perror("Attribute creation failed"); exit(EXIT_FAILURE); } res = pthread_attr_setschedpolicy(&thread_attr, SCHED_OTHER); if (res != 0) { perror("Setting schedpolicy failed"); exit(EXIT_FAILURE); } res = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED); if (res != 0) { perror("Setting detached attribute failed"); exit(EXIT_FAILURE); } res = pthread_create(&a_thread, &thread_attr, thread_function, (void *)message); if (res != 0) { perror("Thread creation failed"); exit(EXIT_FAILURE); } max_priority = sched_get_priority_max(SCHED_OTHER); min_priority = sched_get_priority_min(SCHED_OTHER); scheduling_value.sched_priority = min_priority; res = pthread_attr_setschedparam(&thread_attr, &scheduling_value); if (res != 0) { perror("Setting schedpolicy failed"); exit(EXIT_FAILURE); } (void)pthread_attr_destroy(&thread_attr); while(!thread_finished) { printf("Waiting for thread to say it's finished...\n"); sleep(1); } printf("Other thread finished, bye!\n"); exit(EXIT_SUCCESS); } void *thread_function(void *arg) { printf("thread_function is running. Argument was %s\n", (char *)arg); sleep(4); printf("Second thread setting finished flag, and exiting now\n"); thread_finished = 1; pthread_exit(NULL); }
经过 int pthread_cancel(pthread_t thread);
来请求一个线程终止
经过 int pthread_setcancelstate(int state, int *oldstate)
来设置接受的进程是容许取消请求仍是忽略它
经过 int pthread_setcanceltype(int type, int *oldtype
) 来设置取消类型, PTHREAD_CANCEL_ASYCHRONOUS
表明接收到取消请求后当即行动, THREAD_CANCEL_DEFERRED
表示在接收到请求后,等待函数执行下述动做之一后才取消线程: pthread_join, pthread_cond_wait, pthread_cond_timeout, pthread_test_cancel, 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("Caceling 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; res = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); if (res != 0) { perror("Thread pthread_setcalcelstate 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); }
代码以下:
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <pthread.h> #define NUM_THREADS 6 void *thread_function(void *arg); int main() { int res; pthread_t a_thread[NUM_THREADS]; void *thread_result; int lots_of_threads; for(lots_of_threads = 0; lots_of_threads < NUM_THREADS; lots_of_threads++) { res = pthread_create(&(a_thread[lots_of_threads]), NULL, thread_function, (void *)&lots_of_threads); if (res != 0) { perror("Thread creation failed"); exit(EXIT_FAILURE); } sleep(1); } printf("Waiting for threads to finish...\n"); for(lots_of_threads = NUM_THREADS - 1; lots_of_threads >= 0; lots_of_threads--) { res = pthread_join(a_thread[lots_of_threads], &thread_result); if (res == 0) { printf("Picked up a thread\n"); } else { perror("pthread_join failed"); } } printf("All done\n"); exit(EXIT_SUCCESS); } void *thread_function(void *arg) { int my_number = *(int *)arg; int rand_num; printf("thread_function is running. Argument was %d\n", my_number); rand_num=1+(int)(9.0*rand()/(RAND_MAX+1.0)); sleep(rand_num); printf("Bye from %d\n", my_number); pthread_exit(NULL); }
运行结果以下:
thread_function is running. Argument was 0 Bye from 0 thread_function is running. Argument was 1 thread_function is running. Argument was 2 Bye from 1 thread_function is running. Argument was 3 thread_function is running. Argument was 4 thread_function is running. Argument was 5 Waiting for threads to finish... Bye from 5 Picked up a thread Bye from 3 Bye from 2 Bye from 4 Picked up a thread Picked up a thread Picked up a thread Picked up a thread Picked up a thread All done
《Linux 程序设计》
不得不认可,我失败了。曾计划天天分享一篇python相关知识点但没有作到。失败的缘由想找能够找不少好比最近在作一个小的项目、这几天出去聚会没有时间、工做出现问题加班到比较晚等等,然而总结起来不外乎在内心它的重要程度是怎么样的。我反思了下几乎作到一天一篇的这一个月过程作出改变,再也不要求一天一篇,而是当我有好的素材要分享时才分享,这样与我与你们都是一件好事,我能够动态调度本身的精力和注意力,好比最近实现了一半的一个odoo项目依赖关系分析器能够尽快把它作完,对于读者来讲也能够减小干扰看到更好的分享而不是像我以前有几篇那样划水的。但每周应该会有3-4篇Python的知识能够分享。另外我目前的工做是基于Odoo的,我计划尝试作一些基础的教程来分享这个我熟悉的框架,若是有进展必定会告知感兴趣的读者。感谢读者。
最后向漩涡鸣人致敬,朝他的“说到作到,这就是个人忍道”努力。