什么是线程,线程的优点是什么
创建线程 pthread_create
线程创建函数包含四个变量,分别为: 1. 一个线程变量名,被创建线程的标识 2. 线程的属性指针,缺省为NULL即可 3. 被创建线程的程序代码 4. 程序代码的参数
For example: - pthread_t thrd1; - pthread_attr_t attr; - void thread_function(void argument); - char *some_argument; pthread_create(&thrd1, NULL, (void *)&thread_function, (void *) &some_argument)
结束线程 pthread_exit
线程结束调用实例:pthread_exit(void *retval); //retval用于存放线程结束的退出状态
线程等待 pthread_join
pthread_create调用成功以后,新线程和老线程谁先执行,谁后执行用户是不知道的,这一块取决与操作系统对线程的调度,如果我们需要等待指定线程结束,需要使用pthread_join函数,这个函数实际上类似与多进程编程中的waitpid。 举个例子,以下假设 A 线程调用 pthread_join 试图去操作B线程,该函数将A线程阻塞,直到B线程退出,当B线程退出以后,A线程会收集B线程的返回码。 该函数包含两个参数:
pthread_t th //th是要等待结束的线程的标识 void thread_return //指针thread_return指向的位置存放的是终止线程的返回状态。
调用实例:pthread_join(thrd1, NULL);
简单例子
计算0-的总和
#include
#include
#include
#include
#include
using namespace std; int64_t end,now,start,sum; pthread_mutex_t now_t; void *add(void* arg){ while(1){ ┊ pthread_mutex_lock(&now_t); ┊ if(now>end){ ┊ ┊ pthread_mutex_unlock(&now_t); ┊ } ┊ sum+=now; ┊ now++; ┊ pthread_mutex_unlock(&now_t); } } int main(int argc,char *argv[]){ sum=0; now=0; if(argc != 3){ ┊ printf("参数 error\n"); ┊ return 1; } int num_of_thread=0; num_of_thread=atoi(argv[2]); ┊end=atoi(argv[1]); pthread_t tid[num_of_thread]; for(int i = 0;i < num_of_thread;i++){ ┊ pthread_create(&tid[i],NULL,add,NULL); } for(int i=0;i
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/176902.html原文链接:https://javaforall.net
