温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

[Linux线程]使用线程的私有数据

发布时间:2020-07-02 18:27:52 来源:网络 阅读:324 作者:银河星君 栏目:编程语言
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#if 0    //预定义
char * str_accumulate(char *s)
{
    static char accu[1024]={0};
    strcat(accu,s);
    return accu;
}
#endif
  static pthread_key_t str_key;     //顶一个键值
  static pthread_once_t str_alloc_key_once = PTHREAD_ONCE_INIT;  //用于解决键冲突
  static void str_alloc_key();  //按键分配函数
  static void str_alloc_destroy_accu(void *accu);  //撤销按键分配函数
  //处理函数
  char * str_accumulate(const char *s)
  {
    char *accu;
    pthread_once(&str_alloc_key_once,str_alloc_key);  //解决按键冲突
    accu = (char *)pthread_getspecific(str_key);      //获取线程的私有数据地址
    if(accu == NULL)
    {
      accu = malloc(1024);   //分配1024的空间
      if(accu == NULL)  //如果accu为NULL则直接返回NULL
      {
        return NULL;
      }
      accu[0] = 0;
      pthread_setspecific(str_key,(void *)accu);   //将accu存放的数据作为键值关联
      printf("Thread %lx : allocating buffer at %p\n",pthread_self(),accu); //打印输出
    }
    strcat (accu,s);  //将accu和s字符串连接到一起
    return accu;
  }
  //这是一个键值分派函数
  static void str_alloc_key()
  {
    pthread_key_create(&str_key,str_alloc_destroy_accu);   //创建键值
    printf("Thread %lx : allocated key %d\n",pthread_self(), str_key);
  }
  //这是撤销键值的函数
  static void str_alloc_destroy_accu(void *accu)
  {
    printf("Thread %lx : freeing buffer at %p\n",pthread_self(),accu);
    free(accu);      //释放空间
  }
  //线程处理函数
  void *threaddeal(void *arg)
  {
    //该函数的主要工作是将arg的字符串和“Result of和thread连接到一起”
    char *str;
    str = str_accumulate("Result of ");
    str = str_accumulate((char *)arg);
    str = str_accumulate(" thread");
    printf("Thread %lx: \"%s\" \n",pthread_self(),str);
    return NULL;
  }
//主函数
int main(int argc, char *argv[])
{
  char *str;
  pthread_t th2,th3;
  str = str_accumulate("Result of ");
  pthread_create(&th2,NULL,threaddeal,(void *)"first");
  pthread_create(&th3,NULL,threaddeal,(void *)"second");  //建立两个线程
  str = str_accumulate("initial thread");
  printf("Thread %lx :\"%s\"\n",pthread_self(),str);
  pthread_join(th2,NULL);
  pthread_join(th3,NULL);   //阻塞线程1和线程2
  return 0;
}


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI