pthread_create()函数声明
#include <pthread.h>
int pthread_create(pthread_t * thread ,const pthread_attr_t * attr ,
void *(* start_routine )(void *),void * arg );
thread指向的地址存储创建的线程号;
attr 这个参数是一个 pthread_attr_t 结构体的指针,用来在线程创建的时候指定新线程的属性。如果在创建线程时,这个参数指定为 NULL, 那么就会使用默认属性。
新线程通过调用start_routine()开始执行; arg作为start_routine()的唯一参数传递
下面举个例子
#include <iostream>
#include <cstdlib>
#include <pthread.h>
using namespace std;
#define NUM_THREADS 5
void *PrintHello(void *threadid)
{
// 对传入的参数进行强制类型转换,由无类型指针变为整形数指针,然后再读取
long tid = *((long*)threadid);
cout << "Hello Runoob!