pthread_create的使用方法
pthread_create() 给创建的线程传递参数

} ARGS;
void * sub_thread_entry(void * arg)
{
ARGS args;
/* retrieve args */
args.callback = ((ARGS *)arg)->callback;
}
int my_pthread_create(pthread_t *thread, const pthread_attr_t *attr,
FUNCALLBACK start_routine, void *arg1, void * arg2, void * arg3)
{
ARGS * args;
ห้องสมุดไป่ตู้
typedef void * (*FUNCALLBACK)(void * arg1, void * arg2, void * arg3);
typedef stuct {
FUNCALLBACK callback;
void * arg1;
void * arg2;
if (NULL == (args=(ARGS *)malloc(sizeof(ARGS))))
return -1;
/* push args into buffer */
args->callback = start_routine;
args->arg1 = arg1;
args->arg2 = arg2;
args->arg3 = arg3;
return pthread_create(pthread, attr, sub_thread_entry, args);
linux中的线程(线程的创建,销毁)附例程

注意! ! ! ! ! ! ! ! ! ! ! ! ! ! 比如你的源程序叫做 myapp.c, 目标编译时 myapp, 则: gcc -o myapp myapp.c -lpthread 表示链接到 pthread 库创建线程linux线程的创建java线程销毁android线程销毁linux驱动例程linux例程销毁线程线程销毁的方法手动销毁线程
处理 Linux 中的线程 1. 线程的创建 pthread_create() #include <pthread.h> int pthread_create(pthread_t*thread,pthread_attr_t*attr,void*(*start_routine)(void*),void*arg); thread 指向一个 pthread_t 结构,新创建线程的标识就存放在这里; attr 用来指定即将创建的新线程的属性,若为 NULL,则用默认属性; start_routine 是新线程所执行的函数; arg 是执行函数的参数。 若返回 0:线程标识存放在 thread 中。 错误返回非零:非 0 的错误码。 2. 线程的终止 pthread_exit() #include<pthread.h> void pthread_exit(void*retval); pthread_exit 终止线程的运行。线程函数中的 returen 也有同样的作用。 retval 是线程的返回码,可被 pthread_join()使用。 返回:无 3. 等待子线程的终止 pthread_join() #include<pthread.h> int pthread_join(pthread_t th,void **thread_return); th 为等待终止的子线程。若 thread_return 不为 NULL,则线程 th 的返回值存放在 thread_return 所指向的地方。 4. 多线程编程举例 #include<pthread.h> #include<stdio.h> #include<unistd.h> #include<stdlib.h> #include<string.h> #define TN 3 void routine(void*arg); int main() { pthread_t thread[TN]; int retval,i; char msg[TN][10]; puts(“main thread is running.”) for(i=0;i<TN;i++) { sprint(msg[i],”thread %d”,i); retval=pthread_create(&thread[i],NULL,(void*)routine,msg[i]); if(retval!=0) {
pthread_create如何传递多个参数

53. void* receive(void* arg) 54. { 55. 56. 57. 58. 59. 60. 61. 62. 63. struct mypara *recv_para; recv_para = (struct mypara *)arg; i = (*recv_para).thread_id; s = (*recv_para).thread_name; printf("NO : %d Name : %s\n",i,s); printf("Start receive\n"); int i = 0; char *s = NULL;
pthread_create 如何传递多个参数
参照了网上的一些资料,就那么几个,还老有错误,火大了,调了半天才调通,水平不行! 可是网上那些有错误的代码也真够可恶的,那些转载的人难道就没有发现吗?坑爹啊! 不说了,直接上代码!
[cpp] view plaincopyprint?
1. #include<stdio.h> 2. #include<stdlib.h> 3. #include<pthread.h> 4. #include<errno.h> 5. #include<unistd.h> 6. 7. typedef void* (*fun)(void*); 8. 9. 10. static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; 11. static pthread_cond_t recv_over = PTHREAD_COND_INITIALIZER; 12. static pthread_cond_t decode_over = PTHREAD_COND_INITIALIZER; 13. static pthread_cond_t play_over = PTHREAD_COND_INITIALIZER; 14. 15. void* receive(void*); 16. void* decode(void*); 17. void* play(void*); 18. 19. pthread_t tdec, tplay, trecv; 20. 21. struct mypara 22. { 23. 24. 25. }; 26. 27. int main(int argc, char** argv) 28. { int thread_id; char *thread_name;
pthreads使用示例

pthreads使用示例pthreads是一种在多线程编程中常用的库,它提供了一系列函数和数据结构,方便程序员在多线程环境下进行线程的创建、同步与通信。
本文将以示例的方式介绍pthreads的使用。
## 示例1:线程的创建与销毁在使用pthreads创建线程之前,我们需要先包含头文件`pthread.h`。
下面的示例展示了如何创建一个简单的线程,并在线程中输出一句话。
```c#include <stdio.h>#include <pthread.h>void* thread_func(void* arg) {printf("Hello, pthreads!\n");pthread_exit(NULL);}int main() {pthread_t thread;pthread_create(&thread, NULL, thread_func, NULL);pthread_join(thread, NULL);return 0;}```在上述示例中,首先定义了一个`thread_func`函数,它是线程的入口函数。
在该函数中,我们使用`printf`函数输出一句话。
然后,在`main`函数中,我们调用`pthread_create`函数创建一个新的线程,并将`thread_func`作为线程的入口函数。
最后,我们使用`pthread_join`函数等待线程的结束。
## 示例2:线程的同步与互斥在多线程编程中,线程之间的同步与互斥是非常重要的。
pthreads 提供了一些函数和数据结构,可以帮助我们实现线程的同步与互斥。
下面的示例展示了如何使用互斥锁在多个线程之间实现数据的同步与互斥访问。
```c#include <stdio.h>#include <pthread.h>pthread_mutex_t mutex;int count = 0;void* thread_func(void* arg) {for (int i = 0; i < 1000000; i++) {pthread_mutex_lock(&mutex);count++;pthread_mutex_unlock(&mutex);}pthread_exit(NULL);}int main() {pthread_t threads[10];pthread_mutex_init(&mutex, NULL);for (int i = 0; i < 10; i++) {pthread_create(&threads[i], NULL, thread_func, NULL);}for (int i = 0; i < 10; i++) {pthread_join(threads[i], NULL);}pthread_mutex_destroy(&mutex);printf("Final count: %d\n", count);return 0;}```在上述示例中,我们首先定义了一个互斥锁`mutex`和一个全局变量`count`。
pthread_create 原理-概述说明以及解释

pthread_create 原理-概述说明以及解释1.引言1.1 概述pthread_create是一个用于创建线程的函数,它的作用是创建一个新的线程并执行指定的函数。
在多线程编程中,pthread_create函数是非常重要的一个函数,它可以实现并发执行,提高程序的性能和效率。
通过pthread_create函数,我们可以轻松地创建多个线程来执行不同的任务,从而实现程序的并发执行。
在传统的单线程程序中,所有的任务都是顺序执行的,当遇到阻塞或耗时任务时,整个程序会被阻塞,影响程序的执行效率。
而通过多线程编程,可以将这些任务分配给不同的线程来执行,从而提高程序的并发性和响应速度。
本文将介绍pthread_create函数的原理、用法和应用场景,帮助读者更好地了解和掌握这个重要的多线程操作函数。
通过深入理解pthread_create函数,读者可以更好地利用多线程编程提高程序性能,并更好地应对实际软件开发中的并发需求。
1.2 文章结构:本文将围绕pthread_create函数展开讨论,主要分为三个部分:引言、正文和结论。
在引言部分,将对pthread_create函数进行概述,介绍文章的结构以及明确文章的目的。
在正文部分,将详细介绍pthread_create函数的功能和用法,分析其原理并探讨其应用场景。
在结论部分,将总结pthread_create函数的重要性,提出使用该函数时需要注意的事项,并展望其未来的发展前景。
结构部分的内容1.3 目的本文旨在深入探讨pthread_create函数的原理和应用,帮助读者更好地理解多线程编程的基本原理和实践方法。
通过对pthread_create函数的介绍和分析,读者可以了解到如何使用该函数创建新的线程,并且掌握线程管理的关键技巧。
同时,本文还将探讨pthread_create函数的应用场景,帮助读者更好地应用多线程技术解决实际问题。
通过阅读本文,读者可以深入了解pthread_create函数的实现原理,为提高程序的并发性能和可维护性提供参考。
C语言创建线程thread_create()的方法

C语⾔创建线程thread_create()的⽅法在头⽂件 threads.h 中,定义和声明了⽀持多线程的宏、类型和函数。
所有直接与线程相关的标识符,均以前缀 thrd_ 作为开头。
例如,thrd_t 是⼀个对象类型,它标识了⼀个线程。
函数 thrd_create()⽤于创建并开始执⾏⼀个新线程。
函数 thrd_create()的其中⼀个参数为在新线程中需要被执⾏的函数thrd_create()的其中⼀个参数为在新线程中需要被执⾏的函数。
thrd_create()的完整原型是:int thrd_create(thrd_t *thr, thrd_start_t func, void *arg);参数 func 是⼀个指针,它指向在新线程需要被执⾏的函数,⽽ void 指针 arg ⽤于向该函数传递参数。
换句话说,新线程将执⾏函数调⽤ func(arg)。
参数 func 的类型为 thrd_start_t,它被定义为 int(*)(void*)(这是⼀个函数指针,指向⼀个 void 指针作为其参数并返回⼀个 int 值的函数),因此,该线程执⾏的函数返回⼀个 int 类型的值。
程序在后续过程中可以通过调⽤函数 thread_join()获得这个 int 类型的返回值(必要时,需等待该线程执⾏完)。
如果⼀个线程启动成功,函数 thread_create()将新线程写⼊⼀个对象进⾏标识,并通过参数 thr 指向该对象,然后返回宏值thread_success。
在⼤多数情况下,后续的其他操作均依赖于该线程的执⾏结果,并且只有当该线程完成后,才能执⾏其他操作。
函数thread_join()⽤于确保⼀个线程已完成。
它的原型是:int thrd_join(thrd_t thr, int *result);调⽤ thread_join()的线程会被阻塞,直到通过 thr 标识的线程执⾏完成,这⾥“阻塞”(block)指的是:线程会在调⽤thread_join()的位置停留必要的时间。
pthread 编程

pthread 编程pthread编程是一种多线程编程的方法,主要用于在C/C++语言中创建和管理线程。
本文将介绍pthread编程的基本概念、使用方法以及一些常见的注意事项。
一、pthread编程的基本概念1. 线程:线程是一个独立的执行序列,可以与其他线程并发执行。
2. 多线程:指在一个程序中同时运行多个线程。
3. 线程库:用于创建和管理线程的函数集合,例如pthread库。
4. 线程标识符:用于唯一标识一个线程的整数值。
5. 线程创建:使用pthread_create函数创建一个新线程。
6. 线程执行:使用pthread_join函数等待一个线程的结束,并获取其返回值。
7. 线程同步:使用互斥锁、条件变量等机制,确保多个线程之间的正确协同执行。
二、pthread编程的使用方法1. 引入pthread库:在C/C++程序中引入pthread库,使用#include <pthread.h>。
2. 创建线程:使用pthread_create函数创建一个新线程,并指定线程执行的函数。
3. 线程执行函数:定义一个函数作为线程的执行函数,函数的参数为void*类型。
4. 线程执行:通过调用pthread_create函数,传入线程执行函数的指针和参数,创建并启动新线程。
5. 线程等待:使用pthread_join函数等待一个线程的结束,并获取其返回值。
6. 线程退出:在线程执行函数中使用pthread_exit函数退出线程,并返回一个值。
7. 线程同步:使用互斥锁、条件变量等机制,确保多个线程之间的正确协同执行。
三、常见的pthread编程注意事项1. 线程安全:在多线程编程中,需要注意多个线程之间对共享资源的访问,避免出现数据竞争等问题。
2. 互斥锁:使用互斥锁(pthread_mutex)来保护共享资源的访问,确保同一时间只有一个线程可以访问。
3. 条件变量:使用条件变量(pthread_cond)来实现线程之间的协调与通信,例如线程的等待和唤醒。
Linux多线程实例练习-pthread_create()

Linux多线程实例练习-pthread_create()Linux多线程实例练习 pthread_create():创建⼀个线程int pthread_create(pthread_t *tidp,const pthread_attr_t *attr,(void*)(*start_rtn)(void*),void *arg);1、代码如下xx_pthread_create.c1 #include <pthread.h>2 #include <stdio.h>3 #include <unistd.h>4 #include <sys/time.h> // for gettimeofday56#define debugMsg(fmt, arg...)\7do{\8 unsigned long lSec = 0; unsigned long lUSec = 0;\9 getTimeuSec(&lSec, &lUSec);\10 printf("[%ld.%06ld]:", lSec, lUSec);\11 printf(fmt, ##arg);\12 }while(0)1314int getTimeuSec(unsigned long *lSec, unsigned long *lUSec)15 {16struct timeval start;17 gettimeofday( &start, NULL );18 *lSec = _sec;19 *lUSec = _usec;2021return0;22 }23void * doPrint(void *arg)24 {25int i = 0;26while(i < 30)27 {28 debugMsg("pthread %2d; main %d\n", i++, *(int*)arg);29 usleep(200000);30 }3132return NULL;33 }3435int main()36 {37 pthread_t pid;38int iX = 123;39 pthread_create(&pid, NULL, doPrint, &iX);40while(iX <= 20 + 123)41 {42 debugMsg("main : %d\n", iX++);43 usleep(300000);44 }4546return0;47 }2、CentOS 下编译通过g++ -g -c -o xx_pthread_create.o xx_pthread_create.cg++ -g -o xx_pthread_create xx_pthread_create.o -lpthread3、运⾏结果[1422496189.763862]:main : 123[1422496189.764341]:pthread 0; main 124[1422496189.965627]:pthread 1; main 124[1422496190.065601]:main : 124[1422496190.166510]:pthread 2; main 125[1422496190.366393]:main : 125[1422496190.367391]:pthread 3; main 126[1422496190.568275]:pthread 4; main 126[1422496190.667215]:main : 126[1422496190.769157]:pthread 5; main 127[1422496190.968039]:main : 127[1422496190.970323]:pthread 6; main 128[1422496191.171922]:pthread 7; main 128[1422496191.269869]:main : 128 [1422496191.373803]:pthread 8; main 129 [1422496191.571696]:main : 129 [1422496191.574958]:pthread 9; main 130 [1422496191.776566]:pthread 10; main 130 [1422496191.873512]:main : 130 [1422496191.977457]:pthread 11; main 131 [1422496192.174348]:main : 131 [1422496192.178362]:pthread 12; main 132 [1422496192.379214]:pthread 13; main 132 [1422496192.475159]:main : 132 [1422496192.580095]:pthread 14; main 133 [1422496192.776006]:main : 133 [1422496192.781267]:pthread 15; main 134 [1422496192.981968]:pthread 16; main 134 [1422496193.076864]:main : 134 [1422496193.182797]:pthread 17; main 135 [1422496193.377656]:main : 135 [1422496193.384089]:pthread 18; main 136 [1422496193.584595]:pthread 19; main 136 [1422496193.678472]:main : 136 [1422496193.785406]:pthread 20; main 137 [1422496193.980296]:main : 137 [1422496193.987689]:pthread 21; main 138 [1422496194.189201]:pthread 22; main 138 [1422496194.281149]:main : 138 [1422496194.390049]:pthread 23; main 139 [1422496194.582987]:main : 139 [1422496194.590944]:pthread 24; main 140 [1422496194.792793]:pthread 25; main 140 [1422496194.883821]:main : 140 [1422496194.993852]:pthread 26; main 141 [1422496195.184826]:main : 141 [1422496195.195665]:pthread 27; main 142 [1422496195.397447]:pthread 28; main 142 [1422496195.486468]:main : 142 [1422496195.598408]:pthread 29; main 143 [1422496195.787329]:main : 143。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
pthread_create的使用方法.txt人生重要的不是所站的位置,而是所朝的方向。
不要用自己的需求去衡量别人的给予,否则永远是抱怨。
1:pthread_create 函数的安全使用问题做过linux多线程开发的人都会用过pthread_create函数,但是很少人会注意到:主线程在使用pthread_create创建线程时,如果pthread_create 函数的第四个参数(指针参数)传入的值会被主线程随时修改时,这时我们的线程从pthread_create 函数的第四个参数获取的值可能就不是我们想要传入的值了。
看看下面代码:在 main 函数的 for 循环中创建10个线程;每创建一个线程时都把循环变量 i 的值通过 pthread_create 函数的第四个参数传入线程函数中;但是线程获取的值可能不并不是我们想传入的值。
#include <pthread.h>#include <stdio.h>void * threadFunc( void * pm_iPthreadId ){unsigned int iId = * ( unsigned int * ) pm_iPthreadId;printf( "### No.%d pthread start exec; pthread id = %u !\n", iId, pthread_self() );time_t iStartTime = time( NULL );while(1){time_t iTempTime = time( NULL );if ( iTempTime - iStartTime > 2 ){printf( "$$$ NO.%d pthread cirle exec !\n", * ( unsigned int * ) pm_iPthreadId );iStartTime = time( NULL );}}}int main(){pthread_t * pthreadId;pthreadId = new pthread_t[10];for( unsigned int i = 0; i < 10; i++ ){int iRet = pthread_create( pthreadId + i, NULL, threadFunc, &i ); if ( iRet == 0 ){printf( "@@@ create No.%d pthread success; pthreadId = %u !\n", i, pthreadId + i );}}while(1){}return 0;}程序运行结果如下(结果是随机的,每次运行可能都不一样):@@@ create No.0 pthread success; pthreadId = 138174472 ! ### No.1 pthread start exec; pthread id = 3076803472 ! ### No.1 pthread start exec; pthread id = 3087293328 ! @@@ create No.1 pthread success; pthreadId = 138174476 ! @@@ create No.2 pthread success; pthreadId = 138174480 ! ### No.3 pthread start exec; pthread id = 3055823760 ! ### No.3 pthread start exec; pthread id = 3066313616 ! @@@ create No.3 pthread success; pthreadId = 138174484 ! ### No.4 pthread start exec; pthread id = 3045333904 ! @@@ create No.4 pthread success; pthreadId = 138174488 ! @@@ create No.5 pthread success; pthreadId = 138174492 ! ### No.6 pthread start exec; pthread id = 3024354192 ! ### No.6 pthread start exec; pthread id = 3034844048 ! @@@ create No.6 pthread success; pthreadId = 138174496 ! @@@ create No.7 pthread success; pthreadId = 138174500 ! ### No.8 pthread start exec; pthread id = 3003374480 ! ### No.8 pthread start exec; pthread id = 3013864336 ! @@@ create No.8 pthread success; pthreadId = 138174504 ! @@@ create No.9 pthread success; pthreadId = 138174508 ! ### No.10 pthread start exec; pthread id = 2992884624 ! $$$ NO.10 pthread cirle exec !$$$ NO.10 pthread cirle exec !$$$ NO.10 pthread cirle exec !$$$ NO.10 pthread cirle exec !$$$ NO.10 pthread cirle exec !$$$ NO.10 pthread cirle exec !$$$ NO.10 pthread cirle exec !$$$ NO.10 pthread cirle exec !$$$ NO.10 pthread cirle exec !$$$ NO.10 pthread cirle exec !$$$ NO.10 pthread cirle exec !$$$ NO.10 pthread cirle exec !$$$ NO.10 pthread cirle exec !$$$ NO.10 pthread cirle exec !$$$ NO.10 pthread cirle exec !从以“###”开头的字符结果可以看出:以“###No.0”、“###No.2”、“###No.5、“###No.7”、“###No.7”的字符没有;从这可以看出,我们从pthread_create 函数的第四个参数传入的值是不安全的,应该受到安全保护。
2:此问题原因pthread_create 函数是非阻塞函数;会立即返回,主线程不会在创建线程后阻塞到线程函数执行完后返回,导致线程函数获取到循环变量 i 的值被主线程立即改变。
3:此问题解决方法利用条件变量的保护,在线程函数中把 pthread_create 第四个参数传入的值保存为局部变量。
#include <pthread.h>#include <stdio.h>pthread_cond_t m_Cond;pthread_mutex_t m_Locker;void * threadFunc( void * pm_iPthreadId ){pthread_mutex_lock(&m_Locker);unsigned int iId = * ( unsigned int * ) pm_iPthreadId;printf( "### No.%d pthread start exec; pthread id = %u !\n", iId, pthread_self() );pthread_cond_signal(&m_Cond);pthread_mutex_unlock(&m_Locker);time_t iStartTime = time( NULL );while(1){time_t iTempTime = time( NULL );if ( iTempTime - iStartTime > 2 ){printf( "$$$ NO.%d pthread cirle exec !\n", * ( unsigned int * ) pm_iPthreadId );iStartTime = time( NULL );}}}int main(){pthread_mutex_init(&m_Locker, NULL);pthread_cond_init(&m_Cond, NULL);pthread_t * pthreadId;pthreadId = new pthread_t[10];for( unsigned int i = 0; i < 10; i++ ){pthread_mutex_lock(&m_Locker);int iRet = pthread_create( pthreadId + i, NULL, threadFunc, &i ); if ( iRet == 0 ){printf( "@@@ create No.%d pthread success; pthreadId = %u !\n", i, pthreadId + i );}pthread_cond_wait(&m_Cond, &m_Locker);pthread_mutex_unlock(&m_Locker);}while(1){}return 0;}上面代码运行结果:@@@ create No.0 pthread success; pthreadId = 147902472 !### No.0 pthread start exec; pthread id = 3087563664 !@@@ create No.1 pthread success; pthreadId = 147902476 !### No.1 pthread start exec; pthread id = 3077073808 !@@@ create No.2 pthread success; pthreadId = 147902480 !### No.2 pthread start exec; pthread id = 3066583952 !@@@ create No.3 pthread success; pthreadId = 147902484 !### No.3 pthread start exec; pthread id = 3056094096 !@@@ create No.4 pthread success; pthreadId = 147902488 !### No.4 pthread start exec; pthread id = 3045604240 !@@@ create No.5 pthread success; pthreadId = 147902492 !### No.5 pthread start exec; pthread id = 3035114384 !@@@ create No.6 pthread success; pthreadId = 147902496 !### No.6 pthread start exec; pthread id = 3024624528 !@@@ create No.7 pthread success; pthreadId = 147902500 !### No.7 pthread start exec; pthread id = 3014134672 !@@@ create No.8 pthread success; pthreadId = 147902504 !### No.8 pthread start exec; pthread id = 3003644816 !@@@ create No.9 pthread success; pthreadId = 147902508 ! ### No.9 pthread start exec; pthread id = 2993154960 ! $$$ NO.10 pthread cirle exec !$$$ NO.10 pthread cirle exec !$$$ NO.10 pthread cirle exec !$$$ NO.10 pthread cirle exec !$$$ NO.10 pthread cirle exec !$$$ NO.10 pthread cirle exec !$$$ NO.10 pthread cirle exec !$$$ NO.10 pthread cirle exec !$$$ NO.10 pthread cirle exec !$$$ NO.10 pthread cirle exec !$$$ NO.10 pthread cirle exec !$$$ NO.10 pthread cirle exec !$$$ NO.10 pthread cirle exec !。