当前位置: 代码迷 >> 综合 >> POSIX 线程编程指南(三)
  详细解决方案

POSIX 线程编程指南(三)

热度:86   发布时间:2024-01-09 03:50:39.0

合并与分离线程

  例程:
  • pthread_join (threadid,status)

    pthread_detach (threadid)

    pthread_attr_setdetachstate (attr,detachstate)

    pthread_attr_getdetachstate (attr,detachstate)

 合并:

  • “Joining” 是完成线程间同步的一种方式. 比如:

  • pthread_join()会阻塞调用线程,直到指定的线程终止.

  • 如果retval不为NULL,pthread_join()会将目标线程的退出状态(即目标线程提供给pthread_exit()的值)copy到retval指向的位置.

  • 对同一线程多次调用join是逻辑错误.

  • 另外两种同步方法, 互斥体和 条件变量将稍候讨论
.

 是否可合并?

  • 当线程被创建后,它的一个属性定义了它是“joinable”还是“detached”。只有被创建为“joinable”的线程才能被合并(join)。如果线程被创建为分离的(detached),那么它永不会被合并

  • POSIX 标准的最后草案指定创建线程时应为可合并的.

  • 需要使用pthread_create() 的参数attr来显式的创建可合并或者分离的线程. 典型的四个步骤:
    1. 以pthread_attr_t 数据类型声明一个pthread属性变量
    2. 使用pthread_attr_init()初始化这个属性变量
    3. 使用pthread_attr_setdetachstate()设置属性的分离状态
    4. 完事后使用pthread_attr_destroy()释放属性使用的库资源
  分离:
  • 使用pthread_detach() 显式的将线程设置为分离的,及时它被创建为可合并的.

  • 无法逆向

 建议:

  • 如果线程需要合并,那就考虑显式的声明它。防止某些实现在创建线程时默认并不是将其设置为可合并的.

  • 如果某个线程根本不需要合并,那么创建时就将其设置为分离的。这样一些系统资源能被及早释放.

Example: Pthread Joining

  • 该示例展示了如何使用join例程"等待"线程完成.
  • 由于一些实现创建线程时可能并不将其置于可合并状态, 该例中的线程都以显式的方式创建为可合并,所以稍后它们都能被合并.
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_THREADS	4void* BusyWork(void *t)
{int i;long tid;double result = 0.0;tid = (long)t;printf("Thread %ld starting...\n", tid);for (i = 0; i < 1000000; i++){result = result + sin(i) * tan(i);}printf("Thread %ld done. Result = %e\n", tid, result);pthread_exit((void *)t);
}int main(int argc, char *argv[])
{pthread_t thread[NUM_THREADS];pthread_attr_t attr;int rc;long t;void *status;/* Initialize and set thread detached attribute */pthread_attr_init(&attr);pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);for (t = 0; t < NUM_THREADS; t++){printf("Main: creating thread %ld\n", t);rc = pthread_create(&thread[t], &attr, BusyWork, (void *)t);if (rc){printf("ERROR; return code from pthread_create() is %d\n", rc);exit(-1);}}/* Free attribute and wait for the other threads */pthread_attr_destroy(&attr);for (t = 0; t < NUM_THREADS; t++){rc = pthread_join(thread[t], &status);if (rc){printf("ERROR; return code from pthread_join() is %d\n", rc);exit(-1);}printf("Main: completed join with thread %ld having a status of %ld\n", t, (long)status);}printf("Main: program completed. Exiting.\n");pthread_exit(NULL);
}

输出:

Main: creating thread 0
Main: creating thread 1
Thread 0 starting...
Main: creating thread 2
Thread 1 starting...
Main: creating thread 3
Thread 2 starting...
Thread 3 starting...
Thread 1 done. Result = -3.153838e+06
Thread 0 done. Result = -3.153838e+06
Main: completed join with thread 0 having a status of 0
Main: completed join with thread 1 having a status of 1
Thread 3 done. Result = -3.153838e+06
Thread 2 done. Result = -3.153838e+06
Main: completed join with thread 2 having a status of 2
Main: completed join with thread 3 having a status of 3
Main: program completed. Exiting.

栈控制

  Routines:
  • pthread_attr_getstacksize (attr, stacksize)

    pthread_attr_setstacksize (attr, stacksize)

    pthread_attr_getstackaddr (attr, stackaddr)

    pthread_attr_setstackaddr (attr, stackaddr)

int pthread_attr_setstacksize(pthread_attr_t *attr, size_tstacksize);

intpthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize);

int pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr);
intpthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackaddr);

 防止栈问题:

  • POSIX 标准并没有规定一个线程堆栈的大小。这依赖于实现而异.

  • 超出默认堆栈限制往往是很容易做到,通常的结果︰ 程序终止和/或数据损坏.

  • 安全和可移植的程序并不依赖于默认的堆栈大小,它们会显式的使用pthread_attr_setstacksize()为每个线程分配足够的栈空间.

  • 某些环境要求线程的栈必须处于特定的内存区域,应用可以使用pthread_attr_getstackaddr() 和 pthread_attr_setstackaddr()来完成.

 Some Practical Examples at LC:

  • 线程默认栈大小的差别很大.可获得的最大值也差别很大,而且可能依赖于每个节点的线程数量

  • 下面将过去和当前的架构都列出来,用来展示默认线程栈大小的巨大差别.
    Node
    Architecture
    #CPUs Memory (GB) Default Size
    (bytes)
    Intel Xeon E5-2670 16 32 2,097,152
    Intel Xeon 5660 12 24 2,097,152
    AMD Opteron 8 16 2,097,152
    Intel IA64 4 8 33,554,432
    Intel IA32 2 4 2,097,152
    IBM Power5 8 32 196,608
    IBM Power4 8 16 196,608
    IBM Power3 16 16 98,304

Example: Stack Management

  • 该示例展示了如何查询与设置线程的栈大小.
#include <pthread.h>
#include <stdio.h>
#define NTHREADS 4
#define N 1000
#define MEGEXTRA 1000000pthread_attr_t attr;void* dowork(void *threadid)
{double A[N][N];int i, j;long tid;size_t mystacksize;tid = (long)threadid;pthread_attr_getstacksize(&attr, &mystacksize);printf("Thread %ld: stack size = %li bytes \n", tid, mystacksize);for (i = 0; i < N; i++)for (j = 0; j < N; j++)A[i][j] = ((i * j) / 3.452) + (N - i);pthread_exit(NULL);
}int main(int argc, char *argv[])
{pthread_t threads[NTHREADS];size_t stacksize;int rc;long t;pthread_attr_init(&attr);pthread_attr_getstacksize(&attr, &stacksize);printf("Default stack size = %li\n", stacksize);stacksize = sizeof(double) * N * N + MEGEXTRA;printf("Amount of stack needed per thread = %li\n", stacksize);pthread_attr_setstacksize(&attr, stacksize);printf("Creating threads with stack size = %li bytes\n", stacksize);for (t = 0; t < NTHREADS; t++){rc = pthread_create(&threads[t], &attr, dowork, (void *)t);if (rc){printf("ERROR; return code from pthread_create() is %d\n", rc);exit(-1);}}printf("Created %ld threads.\n", t);pthread_exit(NULL);
}

Miscellaneous Routines

  • pthread_self ()

    pthread_equal (thread1,thread2)

    pthread_t pthread_self(void);

    int pthread_equal(pthread_tt1,pthread_tt2);

  • pthread_self 返回系统赋予调用线程的唯一ID.

  • pthread_equal 比较两个线程ID. 如果相等返回非0,否则返回0.

  • 注意:因为线程ID是隐藏对象(opaque),所以不能使用C/C++语言的逻辑运算符==来判断它们是否相同,也不能将其与其他类型数据比较.

    pthread_once (once_control, init_routine)

    int pthread_once(pthread_once_t * once_control ,void (* init_routine )(void));
    pthread_once_t
once_control = PTHREAD_ONCE_INIT
  • pthread_once 在进程中只执行 init_routine 一次. 被任何线程第一次调用时,它执行给定的 init_routine(没有参数). 后续的调用没有无效

  • init_routine routine 一般为初始化例程.

  • once_control 参数为同步控制结构体,调用前需要初始化. For example:

    pthread_once_t once_control = PTHREAD_ONCE_INIT;