Chinaunix首页 | 论坛 | 博客
  • 博客访问: 146099
  • 博文数量: 40
  • 博客积分: 1131
  • 博客等级: 少尉
  • 技术积分: 459
  • 用 户 组: 普通用户
  • 注册时间: 2011-07-16 11:50
文章分类
文章存档

2012年(2)

2011年(38)

我的朋友

分类: 嵌入式

2011-07-30 21:29:05

pthread, POSIX线程。

线程间的切换的话,不用像进程间那样需要完整的上下文切换,从而节省开销。

__clone函数调用

__clone的目的是为了更容易实现pthread库。
Linux私有函数__clone是fork的替代函数,她能够更多控制父进程和子进程之间共享那些进程资源。

#include
int __clone(int (*fn)(void *fnarg), void *child_stack, int flags, void *arg);

pthread接口

pthread = c函数调用 数据结构 libpthread.so

pthread_create

#include
int pthread_create(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *arg);

pthread_exit

void pthread_exit(void *retval);

pthread_join

挂起当前线程,直到th指定的线程终止运行为止。

int pthread_join(pthread_t th, void **thread_return);
int pthread_detach(pthread_t th);

一个线程所使用的内存资源在对该线程应用pthread_join之前不会被重新分配,所以对于可切入的线程必须调用一次join函数。

pthread_cancel

int pthread_setcancelstate(int state, it *oldstate);
int pthread_cancel(pthread_t thread);

code sample for pthread_create

 

  1. /*
  2.  * thrdcreat.c - Illustate creating a thread
  3.  */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <pthread.h>

  7. void err_quit(char *msg);

  8. void task1(int *counter);
  9. void task2(int *counter);
  10. void cleanup(int counter1, int counter2);

  11. int g1 = 0;
  12. int g2 = 0;

  13. int main(int argc, char *argv[]) {
  14.   pthread_t thrd1, thrd2;
  15.   int ret;

  16.   ret = pthread_create(&thrd1, NULL, (void *)task1, (void *)&g1);
  17.   if (ret) err_quit("pthred_create: task1");
  18.   ret = pthread_create(&thrd2, NULL, (void *)task2, (void *)&g2);
  19.   if (ret) err_quit("pthred_create: task2");

  20.   pthread_join(thrd1, NULL);
  21.   pthread_join(thrd2, NULL);

  22.   cleanup(g1, g2);
  23.   exit(EXIT_SUCCESS);
  24. }

  25. void task1(int *counter) {
  26.   while(*counter < 5) {
  27.     printf("task1 count: %d\n", *counter);
  28.     (*counter) ;
  29.     sleep(1);
  30.   }
  31. }

  32. void task2(int *counter) {
  33.   while(*counter < 5) {
  34.     printf("task2 count: %d\n", *counter);
  35.     (*counter) ;
  36.   }
  37. }


  38. void cleanup(int counter1, int counter2) {
  39.   printf("total :%d\n", counter1 counter2);
  40. }

  41. void err_quit(char *msg) {
  42.   perror(msg);
  43.   exit(EXIT_FAILURE);
  44. }
pthread条件

用于挂起当前线程知道满足条件为止。

#include
int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr);
int pthread_cond_signal(pthread_cond_t *cond);
int pthread_cond_broadcast(pthread_cond_t *cond);
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
int pthread_cond_timewait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime);
int pthread_cond_destroy(pthread_cond_t *cond);

singal & broadcast用于重启等待条件的线程。
wait & timewait 解开mutex指出的一个mutex, 然后等待变量cond上的信号。


互斥

mutex = mutual exclusion, 是一种锁或者信号灯。

一个互斥锁只有两种状态: locked , unlocked.

int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t * mutexatrr);
int pthread_mutex_lock(pthread_mutex_t * mutex);
int pthread_mutex_trylock(pthrad_mutex_t *mutex);
int pthread_mutex_unlock(pthread_mutex_t *mutex);
int pthread_mutex_destroy(pthread_mutex_t *mutex);

code sample for mutex:

 

  1. /*
  2.  * mutex.c - using mutex
  3.  */
  4. #include <pthread.h>
  5. #include <unistd.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <errno.h>

  9. #define INDEX 10000000
  10. long int ticks;
  11. time_t end_time;
  12. pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

  13. void err_quit(char *msg);
  14. void idx_th(void *arg);
  15. void mon_th(void *arg);

  16. int main(int argc, char *argv[]) {
  17.   pthread_t idx_th_id;
  18.   pthread_t mon_th_id;
  19.   int ret;

  20.   end_time = time(NULL) 30; /* 30s */

  21.   ret = pthread_create(&idx_th_id, NULL, (void*)idx_th, NULL);
  22.   if (ret) err_quit("pthread_create; idx_th");
  23.   ret = pthread_create(&mon_th_id, NULL, (void*)mon_th, NULL);
  24.   if (ret) err_quit("pthread_create; mon_th");

  25.   pthread_join(idx_th_id, NULL);
  26.   pthread_join(mon_th_id, NULL);

  27.   exit(EXIT_SUCCESS);
  28. }

  29. /* increase the ticks */
  30. void idx_th(void *arg) {
  31.   long l;
  32.   int ret;

  33.   while (time(NULL) < end_time) {
  34.     ret = pthread_mutex_lock(&mutex);
  35.     if (ret) err_quit("pthread_mutex_lock");

  36.     for (l = 0l; l < INDEX; l ) {
  37.       ticks ;
  38.     }

  39.     ret = pthread_mutex_unlock(&mutex);
  40.     if (ret) err_quit("pthread_mutex_unlock");
  41.     sleep(1);
  42.   }
  43. }

  44. /* monitor the ticks */
  45. void mon_th(void *arg) {
  46.   int nolock = 0;
  47.   int ret;

  48.   while (time(NULL) < end_time) {
  49.     sleep(3);

  50.     ret = pthread_mutex_trylock(&mutex);
  51.     if (ret != EBUSY) {
  52.       if (ret) err_quit("pthread_mutex_trylock");
  53.       printf("mon_th: got lock at %ld ticks\n", ticks);
  54.       ret = pthread_mutex_unlock(&mutex);
  55.       if (ret) err_quit("pthread_mutex_unlock");
  56.     } else {
  57.       nolock ;
  58.     }
  59.   }
  60.   printf("mon_th missed lock %d times\n", nolock);
  61. }

  62. void err_quit(char *msg) {
  63.   perror(msg);
  64.   exit(EXIT_FAILURE);
  65. }
阅读(1239) | 评论(0) | 转发(0) |
0

上一篇:内存管理

下一篇:泛型技术和Boost库

给主人留下些什么吧!~~