C语言 - pthread
pthread_create函数
原型:int pthread_create((pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
用法:#include <pthread.h>
功能:创建线程(实际上就是确定调用该线程函数的入口点),在线程创建以后,就开始运行相关的线程函数。
说明:thread:线程标识符;
attr:线程属性设置;
start_routine:线程函数的起始地址;
arg:传递给start_routine的参数;
返回值:成功,返回0;出错,返回-1。
举例:
#include <pthread.h>
#include <stdio.h>
#include <sys/types.h> #include <unistd.h>
#include <sys/syscall.h>
#define gettid() syscall(SYS_gettid) #define ARRAYSIZE 17
#define NUMTHREADS 4 struct ThreadData {
int start, stop;
int* array;
}; void* squarer(void* td)
{
struct ThreadData* data=(struct ThreadData*) td; int start=data->start;
int stop=data->stop;
int* array=data->array;
int i;
pid_t tid1; tid1 = gettid(); //error at this statement//`
printf("tid : %d\n",tid1); for (i=start; i<stop; i++) {
// sleep(1);
array[i]=i*i;
printf("arr[%d] = [%d]\n",i,array[i]);
printf("%d to %d", start, stop);
}
return NULL;
} int main(void) {
int array[ARRAYSIZE];
pthread_t thread[NUMTHREADS];
struct ThreadData data[NUMTHREADS];
int i; int tasksPerThread=(ARRAYSIZE+NUMTHREADS-1)/NUMTHREADS; for (i=0; i<NUMTHREADS; i++) {
data[i].start=i*tasksPerThread;
data[i].stop=(i+1)*tasksPerThread;
data[i].array=array;
} data[NUMTHREADS-1].stop=ARRAYSIZE; for (i=0; i<NUMTHREADS; i++) {
pthread_create(&thread[i], NULL, squarer, &data[i]);
} for (i=0; i<NUMTHREADS; i++) {
pthread_join(thread[i], NULL);
} for (i=0; i<ARRAYSIZE; i++) {
printf("%d ", array[i]);
}
printf("\n"); return 0;
}
编译: gcc -pthread 3.cc
运行: ./a.out

C语言 - pthread的更多相关文章
- Android Framework中的线程Thread及它的threadLoop方法
当初跟踪Camera的代码中的时候一直追到了HAL层,而在Framework中的代码看见了许很多多的Thread.它们普遍的特点就是有一个threadLoop方法.依照字面的意思应该是这个线程能够循环 ...
- Optimize(优化实验)
十大优化法则 1.更快(本课程重点!) 2.更省(存储空间.运行空间) 3.更美(UI 交互) 4.更正确(本课程重点!各种条件下) 5.更可靠 6.可移植 7.更强大(功能) 8.更方便(使用) 9 ...
- 转载~kxcfzyk:Linux C语言多线程库Pthread中条件变量的的正确用法逐步详解
Linux C语言多线程库Pthread中条件变量的的正确用法逐步详解 多线程c语言linuxsemaphore条件变量 (本文的读者定位是了解Pthread常用多线程API和Pthread互斥锁 ...
- C语言多线程pthread库相关函数说明
线程相关操作说明 一 pthread_t pthread_t在头文件/usr/include/bits/pthreadtypes.h中定义: typedef unsigned long int pth ...
- C语言使用pthread多线程编程(windows系统)二
我们进行多线程编程,可以有多种选择,可以使用WindowsAPI,如果你在使用GTK,也可以使用GTK实现了的线程库,如果你想让你的程序有更多的移植性你最好是选择POSIX中的Pthread函数库,我 ...
- C语言使用pthread多线程编程(windows系统)一
运行之前需要做一些配置: 1.下载PTHREAD的WINDOWS开发包 pthreads-w32-2-4-0-release.exe(任何一个版本均可) http://sourceware.or ...
- 4.1/4.2 多线程进阶篇<上>(Pthread & NSThread)
本文并非最终版本,如有更新或更正会第一时间置顶,联系方式详见文末 如果觉得本文内容过长,请前往本人 “简书” 本文源码 Demo 详见 Githubhttps://github.com/shorfng ...
- sqlite嵌入式数据库C语言基本操作(2)
:first-child{margin-top:0!important}img.plugin{box-shadow:0 1px 3px rgba(0,0,0,.1);border-radius:3px ...
- 多线程(pthread、NSThread、GCD)
pthread C语言编写 跨平台可移植 线程生命周期需要我们来管理 使用困难 NSThread 面向对象的 可直接操作线程对象 线程生命周期需要我们来管理 使用简单 资源互斥(@synchroniz ...
随机推荐
- [CSS备忘] css3零散
-webkit-overflow-scrolling:touch;下拉滚动回弹
- 关于JavaScript语法的小笔记
1.pop() 函数用来移出数组中最后一个元素:shift() 函数移出数组中的第一项,unshift(移入)一个元素到数组的头部. 例如: ], []]; var a=myArray.pop(); ...
- css实现三列布局,左右固定值,中间自适应。
这里主要用到的是position:absolute;及margin属性;代码很简单,一看就明白. <!DOCTYPE html> <html lang="zh_CN&quo ...
- jquery 实现 点击一个按钮添加多个div
<script type="text/javascript"> var blockNum=10; $(document).ready(function(){ var p ...
- 利用yield关键字输出杨辉三角
最近学习了下python,发现里面也有yield的用法,本来对C#里的yield不甚了解,但是通过学习python,对于C#的yield理解更深了!! 不多说了,小学生水平的表达能力伤不起.... 直 ...
- tomcat配置多个虚拟主机
先修改默认端口(8080),http的默认端口是80,我们将8080改成80,这样域名就不用带上端口了 修改conf文件夹下的server.xml文件 添加多个主机(以下三个主机对应三个域名,三个域名 ...
- 【Machine Learning in Action --3】决策树ID3算法预测隐形眼睛类型
本节讲解如何预测患者需要佩戴的隐形眼镜类型. 1.使用决策树预测隐形眼镜类型的一般流程 (1)收集数据:提供的文本文件(数据来源于UCI数据库) (2)准备数据:解析tab键分隔的数据行 (3)分析数 ...
- 模拟IIC协议时序
IIC是飞利浦公司开发的两线式串行总线,主要应用在单片机和外围电子器件之间的数据通讯. IIC总线优点是节约总线数,稳定,快速,是目前芯片制造上非常流行的一种总线,大多数单片机已经片内集成了IIC总线 ...
- php学习的第8天
晚上老师布置啦17题算数问题的作业 我一看 感觉挺容易的 动手就开始做啦起来 不知不觉也9点多啊 终于做好前部题目 发现这我3个月前也遇到类似的题目 那是我还一题都做一题用上1个钟左右 好多也做不出 ...
- 关于array_agg 函数
今天一个客户问怎样把表中相同键值对应的文本按照一定顺序拼接起来.如果使用SQL实现将非常麻烦,并且效率低下.GP4.1以后提供了一个函数array_agg可以方便快捷,高效的实现该功能 比如原始查询是 ...