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 ...
随机推荐
- [妙味JS基础]第五课:函数传参、重用、价格计算
知识点总结 函数传参,传的参数=数据类型(即:数值.字符串.布尔.函数.对象.未定义) 通过传参来重用代码 1.尽量保证 HTML 代码结构一致,可以通过父级选取子元素 2.把核心主程序实现,用函数包 ...
- oracle 11g dblink配置
关于DBLINK的概念在本文中不再赘述,本文主要介绍DBLINK的创建. 1.创建环境描述 本文将在两台操作系统为红帽5.5版本.装有Oracle 11g的虚拟机中进行DBLINK的创建以及测试工作. ...
- Modernizr 测试浏览器是否兼容相应属性
Modernizr 测试浏览器是否兼容相应属性
- Nginx 搭建反向代理服务器过程详解
1.1 反向代理初印象 反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet ...
- hdu_4417_Super Mario(主席树)
题目链接:hdu_4417_Super Mario 题意: 给你n个树,有m个询问,每个询问有一个区间和一个k,问你这个区间内不大于k的数有多少个. 题解: 考虑用主席树的话就比较裸,当然也可以用其他 ...
- Android中GridView、ListView 的 getChildAt() 方法返回null 问题
开发的Android app用到了GridView或者ListView,通常使用getChildAt(int position)方法获取当前点击或者选中的View(即position对应的View). ...
- Linux中kettle启动spoon.sh遇到的问题
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAccAAAAYCAIAAAAAgaGrAAAE1klEQVR4nO1b2ZWrMAylLgpyPa7Gzb
- Erlang OTP gen_event
转自:http://www.myexception.cn/program/1569725.html Erlang OTP gen_event (0) 原英文文档:http://www.erlang.o ...
- Smarty练习增删改
<?php //将题目表显示在页面 include("../init.inc.php"); include("../DBDA.php"); $db = n ...
- 对http协议断点续传的理解
断点续传指的是下载传输文件可以中断,之后重新下载时可以接着中断的地方开始下载,而不必从头开始下载.断点续传需要客户端和服务端都支持. 原理是客户端一块一块的请求数据,最后将下载回来的数据块拼接成完整的 ...