linux线程笔记1之创建线程
2024-09-02 05:25:44
1 线程与进程的对比
这里有一个笔记详细的阐述
http://blog.csdn.net/laviolette/article/details/51506953
2 创建线程函数
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
参数意义:
typedef unsigned long int pthread_t
(1)pthread_t *thread:存储线程ID 唯一
(2)const pthread_attr_t *attr:线程属性 设置与栈相关的属性 通常为NULL
(3)void *(*start_routine) (void *):函数指针 指定运行哪个函数代码
(4)void *arg:运行函数的参数地址 通常给自定义函数传入参数
例子:创建新线程 并使用结构体传递多个参数 注意编译的时候就加上-lpthread
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h> struct student
{
int age;
char* name;
}; void *print(struct student *str)
{
printf("the age is %d\n",str->age);
printf("the name is %s\n",str->name);
} int main()
{
struct student stu;
pthread_t thread_id;
stu.age = ;
stu.name = "gogogo";
pthread_create(&thread_id,NULL,(void*)*print,&stu);//创建线程
printf("the new thread id is %u\n",thread_id);
pthread_join(thread_id,NULL);//在主线程等待子线程结束
return ;
}
补充:int pthread_join(pthread_t thread, void **retval);线程等待函数
最新文章
- 图表插件Charts.js的使用
- hdu3530 单调队列
- Coursera台大机器学习课程笔记5 -- Theory of Generalization
- js学习心得之思维逻辑与对象上下文环境(一)
- OAF_开发系列10_实现OAF动态LOV设定
- Html - 仿QQ空间右下角工具浮动块
- thinkphp框架dump友好调试输出函数
- visualSVN server库迁移(转)
- Windows文件监视器 1.0 绿色版
- 泛型集合转化为DataTable
- Linux学习笔记一
- java中Arrays的用法
- 第三百五十节,Python分布式爬虫打造搜索引擎Scrapy精讲—selenium模块是一个python操作浏览器软件的一个模块,可以实现js动态网页请求
- python3 读入一个jpg格式的图片,并转换长宽像素个数,然后进行绘制
- 关于angularjs中路由页面强制更新的问题
- Sweetkang 的机器学习实验室 (1)
- centos7.5安装opendesktop
- IO流入门-概述
- OC语言自定义打印
- ICCV 2017 Best Paper Awards
热门文章
- 【Todo】Java Callable和Future学习
- $.ajax里一个中文全角逗号引发的惨案
- 生活娱乐 Wifi机器人的制作流程
- centos或者ubuntu设置ssh免密码登陆
- 【转载】C#中回滚TransactionScope的使用方法和原理
- 使用 Docker 在 Linux 上托管 ASP.NET Core 应用程序
- 怎样更改Linux中默认的openjdk为自己安装的JDK
- Java Unit Testing - JUnit &; TestNG
- MapReduce算法形式四:mapjoin
- 一个基本的spring+mybatis所需要的包