多线程代码实例
来源:互联网 发布:wow采集助手数据库 编辑:程序博客网 时间:2024/05/24 23:12
例1 多线程及参数传递
#include<iostream>#include<cstdlib>#include<pthread.h>using namespace std;#define NUM_THREADS 5void *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 = 139639619233536Current pthread id = 139639610840832Current pthread id = 139639602448128hello in hello in 3hello in 33hello in 3Current pthread id = 139639594055424Current pthread id = 139639585662720hello in 5
- 调用void *say_hello2(void *args)输出结果:
hello in main...Current pthread id = 139654340855552hello in 0Current pthread id = 139654332462848hello in 1Current pthread id = 139654324070144hello in 2Current pthread id = 139654315677440hello in 3Current pthread id = 139654307284736hello in 4
例2 结构体传递多个参数
#include<iostream>#include<cstdlib>#include<pthread.h>using namespace std;#define NUM_THREADS 5struct 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, 0main(): creating thread, 1main(): creating thread, 2thread ID: 0message: this is messagethread ID: 1message: this is messagethread ID: 2message: this is messagemain(): creating thread, 3main(): creating thread, 4thread ID: 3message: this is messagethread 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 5void *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, 0main(): creating thread, 1main(): creating thread, 2main(): creating thread, 3main(): creating thread, 4Sleeping in threadSleeping in threadThread with id: 1...exitingThread with id: 4...exitingSleeping in threadSleeping in threadThread with id: 2...exitingThread with id: 0...exitingSleeping in threadThread with id: 3...exitingMain: completed thread id: 0exiting with status: 0Main: completed thread id: 1exiting with status: 0Main: completed thread id: 2exiting with status: 0Main: completed thread id: 3exiting with status: 0Main: completed thread id: 4exiting with status: 0Main: 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;}
阅读全文
0 0
- 多线程死锁代码实例
- 多线程实例代码
- python多线程代码实例
- 多线程代码实例
- java多线程基础实例代码
- java多线程编程技术 +代码实例
- Python多线程下载文件实例代码
- java多线程编程技术 +代码实例
- java多线程编程技术 +代码实例
- day11_python多线程实例代码——02
- c#TCP多线程服务器实例代码
- c#tcp多线程服务器实例代码
- 多线程的同步问题代码实例
- java多线程-多线程同步代码块-银行存款实例-锁synchronized
- 关于java多线程编程的基础代码分析实例
- Linux 多线程编程( POSIX )( 三 )------->代码区 ( 信号灯实例 )
- Linux 多线程编程( POSIX )( 四 )------>代码区 ( 互斥量实例 )
- Linux 多线程编程( POSIX )( 五 )----->代码区 ( 条件变量实例 )
- idea安装ZK framework plugin
- [Android study note]安装Apache服务器
- hdu_2594_求串s1的前缀和串s2后缀最大相同部分
- ACM周中总结—9月28日
- 初学使用ViewPager组件
- 多线程代码实例
- LCD接口总结
- laravel框架数据迁移、填充(简单示例)--学习笔记
- string 类主要函数操作笔记
- 对数据进行哈希加盐加密
- Stm32的TFT LCD显示器控制学习笔记
- Codevs 2033 邮票
- C++项目总结三之内存泄漏
- 直方图计算