linux 下常用的创建多线程函数pthread_create(pthread_t * thread , pthread_attr_t * attr , void *(*start_routine)(void*) , void *args);

其中第一个参数用来保存线程信息,第二个参数指新线程的运行属性,可以设置为NULL,第三个参数为自定义的线程函数,第四个参数就是线程函数需要用到的参数,一般如果要传递多个参数,可以设置为结构体(struct)类型,这里我们使用int类型的变量。

下面我着重讨论一个用for结构来创建多个线程时参数传递的问题:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h> #define th_pop 20 pthread_mutex_t mutex; pthread_t a_thread[th_pop]; void * thread_func(void *args)
{
pthread_mutex_lock(&mutex);
int t_id = *(int*)args;
printf("the id of this thread is %d\n",t_id);
pthread_mutex_unlock(&mutex);
return (void*)NULL;
} void init()
{
pthread_mutex_init(&mutex, NULL);
int i;
for( i=0; i<th_pop; i++)
{
pthread_create(&a_thread[i] , NULL , thread_func , &i);
}
//wait the end of the threads;
for( i=0; i<th_pop; i++)
{
int res = pthread_join(a_thread[i] , NULL);
if(res != 0)
printf("the thread id: %d ends fail \n",i);
}
pthread_mutex_destroy(&mutex); } int main()
{
init();
return 0;
}

运行结果:

huangcheng@ubuntu:~$ ./a.out
the id of this thread is 2
the id of this thread is 8
the id of this thread is 9
the id of this thread is 9
the id of this thread is 20
the id of this thread is 20
the id of this thread is 20
the id of this thread is 20
the id of this thread is 20
the id of this thread is 20
the id of this thread is 20
the id of this thread is 20
the id of this thread is 20
the id of this thread is 20
the id of this thread is 20
the id of this thread is 20
the id of this thread is 20
the id of this thread is 20
the id of this thread is 20
the id of this thread is 20

看到这个结果有没有感觉到有什么不对呢?可能你会感觉到很纳闷,怎么出现了那么多的id=0的结果呢?其实这个认真分析一下并不难理解:

首先pthread_create函数传递的是一个指针型的参数,即传递的是一个地址而已,这样在执行for结构时:

for(int i=0; i<th_pop; i++)
{
pthread_create(&a_thread[i] , NULL , thread_func , &i);
}

该for循环快速执行完成,并且将i置为20,故而传递的地址指向的内容为20,同时其它的线程还没来得及执行: int t_id = *(int*)args;

这样就使得多个线程都指向同一个地址,内容为20,解决该问题的一个办法为中for结构中加入sleep(1),这样当sleep时间大于线程函数执行时间,就可以得到一个正确的结果,不过这种办法剥掉了并发性,并不可取,下面我们采用另一种方法。

我们只修改init()函数

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h> #define th_pop 20 // pthread_mutex_t mutex; pthread_t a_thread[th_pop]; void * thread_func(void *args)
{
pthread_mutex_lock(&mutex);
int t_id = *(int*)args;
printf("the id of this thread is %d\n",t_id);
pthread_mutex_unlock(&mutex);
return (void*)NULL;
} void init()
{
pthread_mutex_init(&mutex, NULL);
int thread_id[th_pop];
int i;
for( i=0; i<th_pop; i++)
thread_id[i] = i;
for( i=0; i<th_pop; i++)
{
int *t = thread_id +i;
pthread_create(&a_thread[i] , NULL , thread_func , (void*)t);
}
//wait the end of the threads;
for( i=0; i<th_pop; i++)
{
int res = pthread_join(a_thread[i] , NULL);
if(res != 0)
printf("the thread id: %d ends fail \n",i);
}
pthread_mutex_destroy(&mutex); } int main()
{
init();
return 0;
}

运行结果:

huangcheng@ubuntu:~$ ./a.out
the id of this thread is 3
the id of this thread is 2
the id of this thread is 1
the id of this thread is 4
the id of this thread is 5
the id of this thread is 6
the id of this thread is 7
the id of this thread is 8
the id of this thread is 9
the id of this thread is 10
the id of this thread is 11
the id of this thread is 12
the id of this thread is 13
the id of this thread is 14
the id of this thread is 15
the id of this thread is 16
the id of this thread is 17
the id of this thread is 18
the id of this thread is 19
the id of this thread is 0

从这个例子中我们应该明白,要避免直接在传递的参数中传递发生改变的量,否则会导致结果不可测。

解决这类问题的办法:

(1)复制一份到堆里面。
(2)加锁。(一般做法)
(3)用结构体数组。(推荐这个)
(4)sleep。(不推荐)

UNIX环境高级编程——pthread_create的问题的更多相关文章

  1. (九) 一起学 Unix 环境高级编程 (APUE) 之 线程

    . . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO (三) 一起学 Unix 环境高级编 ...

  2. (十) 一起学 Unix 环境高级编程 (APUE) 之 线程控制

    . . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO (三) 一起学 Unix 环境高级编 ...

  3. UNIX环境高级编程——线程属性

    pthread_attr_t 的缺省属性值 属性 值 结果 scope PTHREAD_SCOPE_PROCESS 新线程与进程中的其他线程发生竞争. detachstate PTHREAD_CREA ...

  4. 【UNIX环境高级编程】线程同步

    当多个线程共享相同的内存时,需要确保每个线程看到一致的数据视图.如果每个线程使用的变量都是其他线程不会读取和修改的,那么就不存在一致性问题.同样,如果变量是只读的也不会有一致性问题.但是,当一个线程可 ...

  5. (十三) [终篇] 一起学 Unix 环境高级编程 (APUE) 之 网络 IPC:套接字

    . . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO (三) 一起学 Unix 环境高级编 ...

  6. multiple definition of `err_sys' 《UNIX环境高级编程》

    本文地址:http://www.cnblogs.com/yhLinux/p/4079930.html 问题描述: [点击此处直接看解决方案] 在练习<UNIX环境高级编程>APUE程序清单 ...

  7. unix环境高级编程基础知识之第二篇(3)

    看了unix环境高级编程第三章,把代码也都自己敲了一遍,另主要讲解了一些IO函数,read/write/fseek/fcntl:这里主要是c函数,比较容易,看多了就熟悉了.对fcntl函数讲解比较到位 ...

  8. (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO

    . . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO (三) 一起学 Unix 环境高级编 ...

  9. (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO

    . . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO (三) 一起学 Unix 环境高级编 ...

随机推荐

  1. 传统方法过渡到ES6去优雅地实现JavaScript的继承

    众所周知,面向对象编程有三个重要的概念: 封装.继承.多态.而JS作为面向对象的弱类型语言,应该说是基于对象的语言,正如常说的,JS的世界里,万物皆对象.虽然JS本身不是面向对象的语言,我们可以通过模 ...

  2. js变量的一点认识

    js中变量包含两种不同数据类型的值,基本类型值(简单的数据段)和引用类型值(可能由多个值组成的对象).那么他们在保存方式和复制变量值是上有什么不同呢? 一.保存 只能给引用类型的值动态添加属性,不能给 ...

  3. LintCode题解之最长单词

    这些一次遍历搞定的,套路无非都是在遍历的时候就记录数据的状态,然后根据遍历到的当前的数据的状态来修改最终结果,当遍历完了的时候结果也就确定了. public class Solution { /* * ...

  4. How To determine DDIC Check Table, Domain and Get Table Field Text Data For Value?

     How To determineDDIC Check Table, Domain and Get Table Field Text Data For Value? 1.Get Table Fie ...

  5. django+uwsgi+nginx+postgresql备忘

    安装pg创建数据库xxx设置用户密码111111 apt-get install postgresql su - postgres psql create database xxx; alter us ...

  6. python转lua最容易掉进去的坑--作用域

    你以为会依次打印2,4,8吗? 错. 2,2,2 value = 1 for i=1,3 do local value = value*2 print(value) end 你以为打印1吗?,错,输出 ...

  7. Eclipse调试(2)——各种类型断点设置

    本文是Eclipse调试(1)--基础篇 的提高篇.分两个部分: 1) Debug视图下的3个小窗口视图:变量视图.断点视图和表达式视图 2) 设置各种类型的断点 变量视图.断点视图和表达式视图 1. ...

  8. Django项目实践4 - Django站点管理(后台管理员)

    http://blog.csdn.net/pipisorry/article/details/45079751 上篇:Django项目实践3 - Django模型 Introduction 对于某一类 ...

  9. 用Codility测试你的编码能力

    没有宏观的架构设计,没有特定的框架语言.在Codility提出的一些小问题上,用最纯粹的方式测试你最基本的编码能力. Codility第一课:算法复杂度 各种算法书的开篇大多是算法分析,而复杂度(co ...

  10. Linux 高性能服务器编程——TCP协议详解

    问题聚焦:     本节从如下四个方面讨论TCP协议:     TCP头部信息:指定通信的源端端口号.目的端端口号.管理TCP连接,控制两个方向的数据流     TCP状态转移过程:TCP连接的任意一 ...