中国摄影在线网站,个人证书查询,织梦手机网站教程,seo是什么软件进程线程区别
创建线程
#include pthread.h
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); -功能#xff1a;创建一个子线程#xff0c;一般情况下main函数所在的线程称为主线程#xff0c;…
进程线程区别
创建线程
#include pthread.h
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); -功能创建一个子线程一般情况下main函数所在的线程称为主线程其余的为子线程。 -参数 -thread传出参数线程创建成功后子线程的ID被写入这个变量 -attr:设置线程的属性一般使用默认值NULL -start_routinue:函数指针这个函数是子线程需要处理的函数 -arg给第三个参数使用传参 -返回值 - 成功0 -失败:返回错误号和之前的错误号errno不太一样 -获取错误的信息 char * strerror(int errnum)
#includestdio.h
#includeunistd.h
#includesys/types.h
#includefcntl.h
#includesys/stat.h
#includepthread.h
#includestring.hvoid * callback(void * arg) {printf(child thread);return NULL;
}int main(){pthread_t tid;int ret pthread_create(tid, NULL, callback, NULL);if(ret ! 0) {char * errstr strerror(ret);printf(error: %s\n, errstr);}for(int i 0; i 5; i) {printf(%d\n, i);}sleep(1);return 0;
}
终止进程
/*#include pthread.hvoid pthread_exit(void *retval);功能终止一个线程在哪个线程中调用终止哪个参数retval传递一个指针作为一个返回值可以在pthread_join()中获取到int pthread_equal(pthread_t t1, pthread t2);功能比较两个线程ID是否相等不同的操作系统实现pthread_t类型的方式不同有的是无符号的长整型有的是结构体
*/
#includestdio.h
#includepthread.h
#includestring.h
#includeunistd.hvoid * callback(void* arg) {printf(child thread id %ld\n, pthread_self());return NULL;
}int main() {pthread_t tid;int ret pthread_create(tid, NULL, callback, NULL);if(ret ! 0) {char * strerr strerror(ret);printf(error:%s\n, strerr);}for(int i 0; i 5; i) {printf(%d\n, i);}printf(child thread id %ld, main tid: %ld\n, tid, pthread_self());//主线程退出其他线程不受影响pthread_exit(NULL);return 0;
}