例1 多线程及参数传递
#include<iostream>
#include<cstdlib>
#include<pthread.h>
using namespace std;
#define NUM_THREADS 5
void *say_hello1(void *args)
{
int i = *( (int *)args ); //对传入的参数进行强制类型转换,由无类型指针转变为整形指针,再用*读取其指向到内容
cout << "hello in " << i << endl;
} //函数返回的是函数指针,便于后面作为参数
/********************************************************************/
void *say_hello2(void *args)
{
int i = (long)args; //对传入的参数进行强制类型转换,由无类型指针转变为整形指针,再用*读取其指向到内容
cout << "hello in " << i << endl;
} //函数返回的是函数指针,便于后面作为参数
/*****************************************************************************/
int main()
{
pthread_t tids[NUM_THREADS];
cout << "hello in main..." << endl;
for(int i = 0 ; i < NUM_THREADS; ++ i){
//int ret = pthread_create( &tids[i], NULL, say_hello1, (void*)&i);//传入到参数必须强转为void*类型,即无类型指针,&i表示取i的地址,即指向i的指针
int ret = pthread_create( &tids[i], NULL, say_hello2, (void*)i);
cout << "Current pthread id = " << tids[i] << endl;
if(ret != 0){
cout << "pthread_create error: error_code = " << ret << endl;
}
}
pthread_exit(NULL);
}
- 调用void *say_hello1(void *args)输出结果:
hello in main...
Current pthread id = 139639619233536
Current pthread id = 139639610840832
Current pthread id = 139639602448128
hello in hello in 3
hello in 3
3hello in 3
Current pthread id = 139639594055424
Current pthread id = 139639585662720
hello in 5
- 调用void *say_hello2(void *args)输出结果:
hello in main...
Current pthread id = 139654340855552
hello in 0
Current pthread id = 139654332462848
hello in 1
Current pthread id = 139654324070144
hello in 2
Current pthread id = 139654315677440
hello in 3
Current pthread id = 139654307284736
hello in 4
例2 结构体传递多个参数
#include<iostream>
#include<cstdlib>
#include<pthread.h>
using namespace std;
#define NUM_THREADS 5
struct thread_data{
int thread_id;
char *message;
};
void *PrintHello(void *threadarg){
struct thread_data *may_data;
may_data = (struct thread_data *) threadarg;
cout << "thread ID: " << may_data->thread_id;
cout << "message: " << may_data->message << endl;
pthread_exit(NULL);
}
int main()
{
pthread_t threads[NUM_THREADS];
struct thread_data td[NUM_THREADS];
int rc;
int i;
for(i = 0; i < NUM_THREADS; ++ i){
cout << "main(): creating thread, " << i << endl;
td[i].thread_id = i;
td[i].message = "this is message";
rc = pthread_create(&threads[i], NULL, PrintHello, (void*)&td[i]);
if(rc){
cout << "error" << rc << endl;
}
}
pthread_exit(NULL);
}
输出结果:
main(): creating thread, 0
main(): creating thread, 1
main(): creating thread, 2
thread ID: 0message: this is message
thread ID: 1message: this is message
thread ID: 2message: this is message
main(): creating thread, 3
main(): creating thread, 4
thread ID: 3message: this is message
thread ID: 4message: this is message
例3 pthread_join等待线程结束
#include<iostream>
#include<cstdlib>
#include<pthread.h>
#include<unistd.h> //sleep(1)
using namespace std;
#define NUM_THREADS 5
void *wait(void *t)
{
int i;
long tid;
tid = (long)t;
sleep(1);
cout << "Sleeping in thread" << endl;
cout << "Thread with id: " << tid << "...exiting" << endl;
pthread_exit(NULL);
}
int main()
{
int rc;
int i;
pthread_t threads[NUM_THREADS];
pthread_attr_t attr;
void *status;
//Initialize and set thread joinable
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
for(i = 0; i < NUM_THREADS; ++ i){
cout << "main(): creating thread, " << i << endl;
rc = pthread_create(&threads[i], NULL, wait, (void*)i);
if(rc){
cout << "error: " << rc << endl;
}
}
//free attribute and wait for the other threads
pthread_attr_destroy(&attr);
for(i = 0; i < NUM_THREADS; ++ i){
rc = pthread_join(threads[i], &status);
if(rc){
cout << "error: " << rc << endl;
}
cout << "Main: completed thread id: " << i;
cout << "exiting with status: " << status << endl;
}
cout << "Main: program exiting." << endl;
pthread_exit(NULL);
}
输出结果:
main(): creating thread, 0
main(): creating thread, 1
main(): creating thread, 2
main(): creating thread, 3
main(): creating thread, 4
Sleeping in threadSleeping in thread
Thread with id: 1...exiting
Thread with id: 4...exiting
Sleeping in threadSleeping in thread
Thread with id: 2...exiting
Thread with id: 0...exiting
Sleeping in thread
Thread with id: 3...exiting
Main: completed thread id: 0exiting with status: 0
Main: completed thread id: 1exiting with status: 0
Main: completed thread id: 2exiting with status: 0
Main: completed thread id: 3exiting with status: 0
Main: completed thread id: 4exiting with status: 0
Main: program exiting.
例4 pthread_exit() and pthread_join()
#define debug_Msg(fmt, arg...)\
do{\
printf("%s %d : ", __FILE__, __LINE__);\
printf(fmt, ##arg);\
}while(0)
void * doPrint(void *arg)
{
debug_Msg("%s\n", (char*)arg);
char *p = "thread is over";
pthread_exit(p);
}
int main()
{
pthread_t pid;
char * pt = "hello pthread";
pthread_create(&pid, NULL, doPrint, pt);
void * p = NULL;
pthread_join(pid, &p);
debug_Msg("return of thread : [%s]\n", (char*)p);
return 0;
}