linux下多线程学习(一)
1、头文件:#include <pthread.h>
初始化线程锁
/*函数原型*/ int pthread_mutex_init (pthread_mutex_t *__mutex,const pthread_mutexattr_t *__mutexattr) /* 功能:函数是以动态方式创建互斥锁的,参数互斥锁的属性在创建锁的时候指定,在LinuxThreads实现中仅有一个锁类型属性, 不同的锁类型在试图对一个已经被锁定的互斥锁加锁时表现不同。函数成功执行后,互斥锁被初始化为未锁住态。 __mutex: __mutexattr:指定了新建互斥锁的属性。如果参数__mutexattr为空,则使用默认的互斥锁属性,默认属性为快速互斥锁 。 return: 0:成功 非0:错误 */
pthread_create创建线程函数
/*函数原型*/ int pthread_create (pthread_t *__restrict __newthread, const pthread_attr_t *__restrict __attr, void *(*__start_routine) (void *), void *__restrict __arg); /* * 作用:创建一个线程 * __restrict:为指向线程标识符的指针 * __attr:用来设置线程属性,一般不设置,为NULL * __start_routine:是线程运行函数的起始地址 * __arg:是运行函数的参数,传递的参数不止一个,那么需要把这些参数放到一个结构中,然后把这个结构的地址作为arg的参数传入,没有参数置NULL * return: 0:成功 非0:错误 */
pthread_join使一个线程等待另一个线程结束
/*函数原型*/ int pthread_join (pthread_t __th, void **__thread_return); /* * 描述:阻塞的方式等待thread指定的线程结束。当函数返回时,被等待线程的资源被收回。如果线程已经结束, * 那么该函数会立即返回。并且thread指定的线程必须是joinable的。 * __th:线程标识符,即线程ID,标识唯一线程 * __thread_return:用户定义的指针,用来存储被等待线程的返回值 * return: * 0:成功 * 非0:错误 */
/*函数原型 加锁*/ int pthread_mutex_lock (pthread_mutex_t *__mutex); /*函数原型 解锁*/ int pthread_mutex_lock (pthread_mutex_t *__mutex); /*函数原型 终止进程*/ void pthread_exit (void *__retval);
示例:
1 #include <stdio.h> 2 #include<pthread.h> 3 #include <string.h> 4 #include <sys/time.h> 5 #define MAX 10 6 pthread_t thread[2]; 7 pthread_mutex_t mut; 8 int number = 0, i; 9 10 void *thread1() 11 { 12 printf("thread1 : I'm thread 1\n"); 13 for (i = 0; i < MAX; i++) 14 { 15 printf("thread1 : number = %d\n", number); 16 pthread_mutex_lock(&mut); 17 number++; 18 pthread_mutex_unlock(&mut); 19 sleep(2); 20 } 21 printf("thread1 :主函数在等我完成任务吗?\n"); 22 pthread_exit(NULL); 23 } 24 void *thread2() 25 { 26 printf("thread2 : I'm thread 2\n"); 27 for (i = 0; i < MAX; i++) 28 { 29 printf("thread2 : number = %d\n", number); 30 pthread_mutex_lock(&mut); 31 number++; 32 pthread_mutex_unlock(&mut); 33 sleep(3); 34 } 35 printf("thread2 :主函数在等我完成任务吗?\n"); 36 pthread_exit(NULL); 37 } 38 void thread_create(void) 39 { 40 int temp; 41 memset(&thread, 0, sizeof(thread)); 42 //comment1 43 /*创建线程*/ 44 if ((temp = pthread_create(&thread[0], NULL, thread1, NULL)) != 0) 45 //comment2 46 printf("线程 1 创建失败!\n"); 47 else 48 printf("线程 1 被创建\n"); 49 if ((temp = pthread_create(&thread[1], NULL, thread2, NULL)) != 0) 50 //comment3 51 printf("线程 2 创建失败"); 52 else 53 printf("线程 2 被创建\n"); 54 } 55 void thread_wait(void) 56 { 57 /*等待线程结束*/ 58 if (thread[0] != 0) 59 { 60 //comment4 61 pthread_join(thread[0], NULL); 62 printf("线程 1 已经结束\n"); 63 } 64 if (thread[1] != 0) 65 { 66 //comment5 67 pthread_join(thread[1], NULL); 68 printf("线程 2 已经结束\n"); 69 } 70 } 71 72 int main(int argc, char **argv) 73 { 74 /*用默认属性初始化互斥锁,用来生成一个互斥锁*/ 75 pthread_mutex_init(&mut,NULL); 76 printf("我是主函数哦,我正在创建线程,呵呵\n"); 77 thread_create(); 78 printf("我是主函数哦,我正在等待线程完成任务阿,呵呵\n"); 79 thread_wait(); 80 return 0; 81 }
结果(每次运行的结果可能不同,线程之间会抢占CUP资源):
我是主函数哦,我正在创建线程,呵呵
线程 1 被创建
线程 2 被创建
我是主函数哦,我正在等待线程完成任务阿,呵呵
thread2 : I'm thread 2
thread2 : number = 0
thread1 : I'm thread 1
thread1 : number = 1
thread1 : number = 2
thread2 : number = 3
thread1 : number = 4
thread2 : number = 5
thread1 : number = 6
thread1 : number = 7
thread2 : number = 8
thread1 : number = 9
thread2 : number = 10
thread1 :主函数在等我完成任务吗?
线程 1 已经结束
thread2 :主函数在等我完成任务吗?
线程 2 已经结束