摘要: //前提是查找的数组已是有序 #include //非递归 int binary(int *arr,int low,int high,int key) { while(low arr[mid]) { low = mid + 1; } else { high = mid ... 阅读全文
posted @ 2019-07-29 21:36 王清河 阅读(165) 评论(0) 推荐(0) 编辑
摘要: #include void swap(int *a,int *b) { int temp = *a; *a = *b; *b = temp; } int MinKey(int *arr,int low,int high) { int min = low; int key = arr[low]; int i; for(i = low + ... 阅读全文
posted @ 2019-07-29 19:12 王清河 阅读(113) 评论(0) 推荐(0) 编辑
摘要: #include void swap(int *a,int *b) { int temp = *a; *a = *b; *b = temp; } void bubble(int array[],int n) { int i; int j; for(i = 0; i array[j]) swap(&array[j... 阅读全文
posted @ 2019-07-29 18:58 王清河 阅读(126) 评论(0) 推荐(0) 编辑
摘要: 有时候我们需要用到线程同步来控制线程运行顺序。 exbot@ubuntu:~/wangqinghe/thread/20190729$ ./synchro 2 thread0 1 thread0 2 thread1 1 thread1 2 thread2 1 thread2 2 thread3 1 t 阅读全文
posted @ 2019-07-29 16:31 王清河 阅读(173) 评论(0) 推荐(0) 编辑
摘要: int pthread_cancel(pthread_t th); 该函数运行一个线程取消指定的另一个线程th 函数成功,返回0,否则,返回非0; 函数运行结果: exbot@ubuntu:~/wangqinghe/thread/20190729$ ./cancel thread_create su 阅读全文
posted @ 2019-07-29 16:30 王清河 阅读(118) 评论(0) 推荐(0) 编辑
摘要: int pthread_detach(pthread_t th); pthread_detach函数使线程处于被分离状态。 如果不等待一个线程,同时对线程的返回值不感兴趣,可以设置这个线程为分离状态,让系统在线程退出的时候自动回收它所占用的资源。 一个线程不能自己调用pthread_detach改变 阅读全文
posted @ 2019-07-29 16:29 王清河 阅读(243) 评论(0) 推荐(0) 编辑
摘要: 进程终止时exit()函数,那么线程终止的是什么呢? 线程终止的三种情况: 运行结果: exbot@ubuntu:~/wangqinghe/thread/20190729$ gcc exit.c -o exit -lpthread exbot@ubuntu:~/wangqinghe/thread/2 阅读全文
posted @ 2019-07-29 11:00 王清河 阅读(168) 评论(0) 推荐(0) 编辑
摘要: 有时候在一个线程中创建了另外一个线程,主线程要等到创建的线程返回了,获取该线程的返回值后才退出,这个时候就需要把线程挂起。 int pthread_join(pthread_t th,void ** thr_return); pthread_join函数用去挂起当前线程,直至th指定的线程终止为止。 阅读全文
posted @ 2019-07-29 10:48 王清河 阅读(1387) 评论(0) 推荐(0) 编辑
摘要: strerror() 包含在string.h的函数中 EXIT_SUCCESS 定义包含在stdlib.h的函数中 运行结果: exbot@ubuntu:~/wangqinghe/thread/20190729$ ./thread thread_create success func run... 阅读全文
posted @ 2019-07-29 10:21 王清河 阅读(211) 评论(0) 推荐(0) 编辑