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的更多相关文章

  1. Android Framework中的线程Thread及它的threadLoop方法

    当初跟踪Camera的代码中的时候一直追到了HAL层,而在Framework中的代码看见了许很多多的Thread.它们普遍的特点就是有一个threadLoop方法.依照字面的意思应该是这个线程能够循环 ...

  2. Optimize(优化实验)

    十大优化法则 1.更快(本课程重点!) 2.更省(存储空间.运行空间) 3.更美(UI 交互) 4.更正确(本课程重点!各种条件下) 5.更可靠 6.可移植 7.更强大(功能) 8.更方便(使用) 9 ...

  3. 转载~kxcfzyk:Linux C语言多线程库Pthread中条件变量的的正确用法逐步详解

    Linux C语言多线程库Pthread中条件变量的的正确用法逐步详解   多线程c语言linuxsemaphore条件变量 (本文的读者定位是了解Pthread常用多线程API和Pthread互斥锁 ...

  4. C语言多线程pthread库相关函数说明

    线程相关操作说明 一 pthread_t pthread_t在头文件/usr/include/bits/pthreadtypes.h中定义: typedef unsigned long int pth ...

  5. C语言使用pthread多线程编程(windows系统)二

    我们进行多线程编程,可以有多种选择,可以使用WindowsAPI,如果你在使用GTK,也可以使用GTK实现了的线程库,如果你想让你的程序有更多的移植性你最好是选择POSIX中的Pthread函数库,我 ...

  6. C语言使用pthread多线程编程(windows系统)一

    运行之前需要做一些配置: 1.下载PTHREAD的WINDOWS开发包 pthreads-w32-2-4-0-release.exe(任何一个版本均可)    http://sourceware.or ...

  7. 4.1/4.2 多线程进阶篇<上>(Pthread & NSThread)

    本文并非最终版本,如有更新或更正会第一时间置顶,联系方式详见文末 如果觉得本文内容过长,请前往本人 “简书” 本文源码 Demo 详见 Githubhttps://github.com/shorfng ...

  8. sqlite嵌入式数据库C语言基本操作(2)

    :first-child{margin-top:0!important}img.plugin{box-shadow:0 1px 3px rgba(0,0,0,.1);border-radius:3px ...

  9. 多线程(pthread、NSThread、GCD)

    pthread C语言编写 跨平台可移植 线程生命周期需要我们来管理 使用困难 NSThread 面向对象的 可直接操作线程对象 线程生命周期需要我们来管理 使用简单 资源互斥(@synchroniz ...

随机推荐

  1. Intellij Idea web项目的部署配置[转]

    原文地址:http://blog.csdn.net/z69183787/article/details/41416189 1.前言 2.项目配置(Project Structure) 2.1 Proj ...

  2. 设计模式 单例模式(Singleton) [ 转载 ]

    设计模式 单例模式(Singleton) [ 转载 ] 转载请注明出处:http://cantellow.iteye.com/blog/838473 前言 懒汉:调用时才创建对象 饿汉:类初始化时就创 ...

  3. 浙江大学 pat 1006题解

    1006. Sign In and Sign Out (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  4. Markdown - Github specific

    这篇文章的内容,必须到github的页面才能全部生效. Github specific Github allows for mistakes There are also Tables in gith ...

  5. Caf音频文件混合

    一.两个同样时常的caf音频文件,可以通过下面的代码混合 二.代码地址: https://github.com/liqiushui/AudioRecorderCafMix  

  6. 关于python的类方法、实例方法和静态方法区别

    python的类方法需要在方法前面加装饰器:@classmethod ,静态方法是在方法前面加装饰器:@staticmethod. 类方法.类属性是属于类自身,属于类自身的命名空间,和实例方法.实例属 ...

  7. WinSnap屏幕截图 V4.5.6 官方最新版

    软件名称: WinSnap屏幕截图软件语言: 多国语言授权方式: 免费试用运行环境: Win7 / Vista / Win2003 / WinXP 软件大小: 2.7MB图片预览: 软件简介:WinS ...

  8. HTTP基础知识

    HTTP是计算机通过网络进行通信的规则,是一种无状态的协议,不建立持久的连接(客户端向服务器发送请求,web服务器返回响应,接着连接就被关闭了): 一个完整的HTTP请求连接,通常有下面7个步骤: 1 ...

  9. Ant 删除目录 vs 清空目录

    Apache Ant 可以用下面的命令来删除目录 <delete dir="${lucene.dir}"/> 但是这会删除整个目录,而我现在只想清空目录内的所有文件和子 ...

  10. 会话技术cookie和session详解

    什么是会话 会话可简单理解为:用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话. 会话技术解决了什么问题 每个用户与服务器进行交互的过程中,各自会有一 ...