博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
pthread线程使用小结
阅读量:4166 次
发布时间:2019-05-26

本文共 2272 字,大约阅读时间需要 7 分钟。

1.奇怪的线程参数初始化

for( i=0; i<n; i++)

{

//会有什么问题?

pthread_create(&tid,NULL, &thread_client_function, (void*)&i );

}

上面代码应该很容易明白,创建多个线程,传入序列号作为线程id。基实这里存在一个大bug, 传递的参数会不成功!!

示例代码:

#include 
#include
#include
#include
#include
void* thread_client_function( void* param ){int client_id = *(int*)param;printf("client id %d/n", client_id);}int main( int argc, char * argv[] ){int n = atol( argv[1] );int i=0;pthread_t tid;for( i=0; i

输出:

gcc -o test_thread test_thread.c -lpthread

./test_thread 3

client id 3

client id 3

client id 3

 

为什么呢?注意其实传递时i是局部变量,而线程的创建是有延时的,所以当多次调用时,线程函数还没执行。但是这里i已经为3了。当线程函数开始执行,读入的参数自然都为3了,这个小细节真可谓令我大伤脑筋呀:)

稍作修改即可:

...

int * a = (int*)malloc( n* sizeof(int) );

for( i=0; i<n; i++)

{

a[i] = i;

pthread_create(&tid,NULL, &thread_client_function, (void*)&a[i] );

}

pthread_join( tid, NULL );

...

这样就可以保存参数不变了。

 

2. pthread_mutex_t / pthread_cond_t初始化/释放

作为一个全局变量时可以使用:

pthread_mutex_t g_mtx = PTHREAD_MUTEX_INITIALIZER;

pthread_cond_t g_cond = PTHREAD_COND_INITIALIZER;

 

如果有多个可使用:

pthread_mutex_init( &g_mtx , NULL);

pthread_cond_init( &g_cond , NULL);

 

释放:

pthread_mutex_destroy( &g_mtx );

pthread_mutex_destroy( &g_mtx );

 

3. 同步条件pthread_cond_t使用

1)需要配合mutex使用

pthread_mutex_lock( &g_mtx );

pthread_cond_wait( &g_cond , &g_mtx );

pthread_mutex_unlock( &g_mtx );

使用pthread_cond_wait 需要在 lock/unlock 之间,以防止在进入wait状态前有signal。需要先lock, 一旦进行wait状态,会释放 mutexlock。而一旦有收到signal信号就会自动重新获到mutexlock。而且condlock是原子操作。

在需要的地方进行 pthread_cond_signal( g_cond ), 之前的wait 位置就可以执行,达到多个线程同步。

 

2)使用超时条件

struct timespec tv;

tv.tv_sec = time(0) + 1;

tv.tv_nsec = 0;

ret = pthread_cond_timedwait( &g_cond , &g_mtx ,&tv );

指定超时结构体timespec,注意超时时间是当前时间,所以要设定为time(0) + N秒。timespec 可精确到纳秒。

 

3)多个线程串行执行

只需要在全局设定一个处理序列号,这样每个线程在执行前判断是否为自己要处理的序号,否则继续wait, 处理框架如下:

void* thread_client_function( void* param )

{

int client_id = *(int*)param;

...

do

{

pthread_cond_wait( &g_conds[ i + 1 ] , &g_mtxs[i] );  //等待取得执行信号

}

while ( g_do[client_id][i] != g_idx ); //判断是否为当前处理序列,否则继续等待

...

}

 

void* thread_server_function( void* param )

{

...

for(i=0;i<n; i++ )

{

printf("Server signal %d/n", i + 1 );

pthread_cond_signal( &g_conds[ i + 1 ] ); //唤醒多个处理线程

}

...

}

上面使用了多个cond的处理,也可以直接使用一个cond, 使用pthread_cond_broadcast响醒所有等待的线程。

转载地址:http://rogxi.baihongyu.com/

你可能感兴趣的文章
java String于常量池中的介绍
查看>>
java Text 错误: 找不到或无法加载主类 Text
查看>>
XShell连接ubantu:给ubantu安装ssh
查看>>
c语言的null和0
查看>>
二进制详解:世界上有10种人,一种懂二进制,一种不懂。
查看>>
c语言一个字符变量存储多个字符
查看>>
java接口中方法的默认访问修饰符为public
查看>>
java多线程之并发synchronized
查看>>
java多线程之并发Lock
查看>>
微信公众平台基础配置
查看>>
jpa 和 hibernate 的联系
查看>>
SpringBoot之@SpringBootApplication注解
查看>>
ajax 传JSON 写法
查看>>
SpringBoot之web发展史
查看>>
SpringBoot之开发web页面
查看>>
SpringBoot之快速部署
查看>>
springBoot之jar包在后台(运行:编写start、stop脚本)
查看>>
redis学习
查看>>
SpringBoot之application.properties文件能配置的属性
查看>>
javaWeb监听器、过滤器、拦截器
查看>>