pthread_create是UNIX环境创建线程函数

头文件

  #include<pthread.h>

函数声明

  int pthread_create(pthread_t*restrict tidp,const pthread_attr_t *restrict_attr,void*(*start_rtn)(void*),void *restrict arg);

返回值

  若成功则返回0,否则返回出错编号

 

  返回成功时,由tidp指向的内存单元被设置为新创建线程的线程ID。attr参数用于制定各种不同的线程属性。新创建的线程从start_rtn函数的地址开始运行,该函数只有一个万能指针参数arg,如果需要向start_rtn函数传递的参数不止一个,那么需要把这些参数放到一个结构中,然后把这个结构的地址作为arg的参数传入。

 

  linux下用C开发多线程程序,Linux系统下的多线程遵循POSIX线程接口,称为pthread。

 

  由 restrict 修饰的指针是最初唯一对指针所指向的对象进行存取的方法,仅当第二个指针基于第一个时,才能对对象进行存取。对对象的存取都限定于基于由 restrict 修饰的指针表达式中。 由 restrict 修饰的指针主要用于函数形参,或指向由 malloc() 分配的内存空间。restrict 数据类型不改变程序的语义。 编译器能通过作出 restrict 修饰的指针是存取对象的唯一方法的假设,更好地优化某些类型的例程。

参数

  第一个参数为指向线程标识符的指针。

 

  第二个参数用来设置线程属性。

 

  第三个参数是线程运行函数的起始地址。

 

  最后一个参数是运行函数的参数。

 

  另外,在编译时注意加上-lpthread参数,以调用静态链接库。因为pthread并非Linux系统的默认库

示例

  打印线程 IDs

 

  #include <pthread.h>

 

  #include <stdlib.h>

 

  #include <stdio.h>

 

  #include <unistd.h>

 

  #include <string.h>

 

  pthread_t ntid;

 

  void printids(const char *s)

 

  {

 

  pid_t pid;

 

  pthread_t tid;

 

  pid = getpid();

 

  tid = pthread_self();

 

  printf("%s pid %u tid %u (0x%x)\n", s,

 

  (unsigned int)pid, (unsigned int)tid, (unsigned int)tid);

 

  } void *thr_fn(void *arg)

 

  {

 

  printids("new thread: ");

 

  return((void *)0);

 

  }

 

  int main(void)

 

  {

 

  int err;

 

  err = pthread_create(&ntid, NULL, thr_fn, NULL);

 

  if (err != 0)

 

  printf("can't create thread: %s\n", strerror(err));

 

  printids("main thread:");

 

  sleep(1);

 

  exit(0);

 

  }

 

  $ gcc main.c -lpthread

 

  $ ./a.out

向线程函数传递参数详解:

向线程函数传递参数分为两种:

(1)线程函数只有一个参数的情况:直接定义一个变量通过应用传给线程函数。

例子:

#include <iostream>
#include <pthread.h>
using namespace std;
pthread_t thread;
void fn(void *arg)
{
    int i = *(int *)arg;
    cout<<"i = "<<i<<endl;
    return ((void *)0);
}
int main()
{
    int err1;
    int i=10;
   err1 = pthread_create(&thread, NULL, fn, &i);
    pthread_join(thread, NULL);
}
2、线程函数有多个参数的情况:这种情况就必须申明一个结构体来包含所有的参数,然后在传入线程函数,具体如下:

例子:

首先定义一个结构体:

struct  parameter

{

int size,

int count;

。。。。。

。。。 
};

然后在main函数将这个结构体指针,作为void
*形参的实际参数传递

struct parameter arg;

通过如下的方式来调用函数:
pthread_create(&ntid, NULL, fn,& (arg));
函数中需要定义一个parameter类型的结构指针来引用这个参数 

void fn(void *arg)
{
    int i = *(int *)arg;
    cout<<"i = "<<i<<endl;
    return ((void *)0);
}

void thr_fn(void *arg)
{
struct parameter *pstru;
pstru = ( struct parameter *) arg;
然后在这个函数中就可以使用指针来使用相应的变量的值了。
}

linux下的多线程,pthread_create函数的更多相关文章

  1. 【转】 Linux下的多线程编程

    作者:gnuhpc 出处:http://www.cnblogs.com/gnuhpc/原文链接:http://www.cnblogs.com/gnuhpc/archive/2012/12/07/280 ...

  2. Linux下的多线程编程

    1 引言 线程(thread)技术早在60年代就被提出,但真正应用多线程到操作系统中去,是在80年代中期,solaris是这方面的佼佼者.传统的 Unix也支持线程的概念,但是在一个进程(proces ...

  3. 【转】Linux下的多线程编程

    1 引言 线程(thread)技术早在60年代就被提出,但真正应用多线程到操作系统中去,是在80年代中期,solaris是这方面的佼佼者.传统的 Unix也支持线程的概念,但是在一个进程(proces ...

  4. 《转》Linux下的多线程编程

    原地址:http://linux.chinaunix.net/doc/program/2001-08-11/642.shtml 1 引言 线程(thread)技术早在60年代就被提出,但真正应用多线程 ...

  5. Linux下精确控制时间的函数

    Linux下精确控制时间的函数 在测试程序接口运行时间的时候,常用time,gettimeofday等函数,但是这些函数在程序执行的时候是耗费时间的,如果仅仅测试时间还行,但是如果程序中用到时间控制类 ...

  6. Linux下模拟多线程的并发并发shell脚本

    分享一个在Linux下模拟多线程的并发脚本,使用这个脚本可以同时批量在定义数量的服务器上执行相关命令,比起普通for/while循环只能顺序一条一条执行的效率高非常多,在管理大批服务器时非常的实用.  ...

  7. Linux下基于多线程的echo

    准备开始写一些Linux 下网络编程以及多线程的blog,就从这个简单的echo程序开始吧. 在echo的服务端使用多线程与客户进行通信,可以实现一个服务端程序同时连接多个客户的功能.那么,到底在服务 ...

  8. Linux Linux下特殊的printf函数和fputs函数

    Linux下,printf函数必须以'\n'结尾才会立刻输出到屏幕,如果没有'\n'直到输出缓冲区满了以后才会打印到屏幕上(敲击换行也算),如果需要不换行的输出,一般可以使用write函数代替.'\n ...

  9. linux下时间有关的函数和结构体

    1.时间类型.Linux下常用的时间类型有6个:time_t,struct timeb, struct timeval,struct timespec,clock_t, struct tm. (1) ...

随机推荐

  1. Hibernate之关于多对多单向关联映射

    [Hibernate]之关于多对多单向关联映射 老师和学生,最典型的多对多关联, Teacher和Student.所谓单向意思就是说.老师知道自己的教的是哪些学生而学生不知道是哪些老师教. 也能够这么 ...

  2. Java IO:SocketChannel和Selector在ZooKeeper中应用

    转载请注明出处:jiq•钦's technical Blog 假设不了解SocketChannel和Selector.请先阅读我的还有一篇博文:点击打开链接 ZooKeeper的启动从QuorumPe ...

  3. 14.NPM 常用命令

    转自:http://www.runoob.com/nodejs/nodejs-npm.html PM提供了很多命令,例如install和publish,使用npm help可查看所有命令. NPM提供 ...

  4. java DSA Signature Sign And Verify

    SignatureSignAndVerify import java.security.KeyPair; import java.security.KeyPairGenerator; import j ...

  5. 数组&对象

    一.遍历数组的几种方式   var arr = [1,2,3]; Array.prototype.test=function(){} arr.name='jq'   1. for  /* * inde ...

  6. mySQL函数根据经纬度计算两点距离 复制代码

    http://www.cnblogs.com/lujiulong/p/6185041.html https://my.oschina.net/u/2273085/blog/505172?p={{pag ...

  7. POJ1308——Is It A Tree?

    Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22631   Accepted: 7756 De ...

  8. 5. Spring Boot 拦截器

    转自:https://blog.csdn.net/catoop/article/details/50501696

  9. 16、cgminer学习之:pthread_mutex_init和pthread_cond_init

    1.原理 假设有两个线程同时访问一个全局变量 n,这个全局变量的初始值等于0. Int  n = 0 ; 消费者线程 A 进入临界区,访问 n,A 必须等到 n 大于 0 才能接着往下执行,如果 n= ...

  10. ORACLE使用WITH AS和HINT MATERIALIZE优化SQL解决FILTER效率低下

     在做项目的过程中,一个页面使用类似例如以下的SQL查询数据.为了保密和使用方便,我把项目中有关的表名和字段替换使用ORACLE数据库中的系统表和字段. 在我所做的项目中.类似ALL_TABLES ...