免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3005 | 回复: 2
打印 上一主题 下一主题

Segmentation fault [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-10-09 20:57 |只看该作者 |倒序浏览
本帖最后由 o_unix 于 2012-10-09 22:22 编辑

大家好:我封装一个类,用来管理db2上下文的。代码如下:

  1. #include <iostream>
  2. using namespace std;

  3. #include <sqlca.h>
  4. #include <string.h>
  5. #include <pthread.h>
  6. #include <sql.h>
  7. // 用于保存上下文并标识当前上下文是否空闲, free 为 1时空闲,0 时在使用。
  8. struct ctxlist
  9. {
  10.     void *ctx;
  11.     int free;
  12. };

  13. class test
  14. {
  15.     /*用于管理上下文访问*/
  16.     pthread_mutex_t mutex;
  17.     pthread_cond_t cond;
  18.     struct ctxlist *list; //保存上下文的起始地址

  19.     int use; // 没有被使用的上下文
  20.     int total; // 一共创建了多少个上下文
  21.     public:
  22.     test( int i, int ctxtype);
  23.     ~test();
  24.     int get(); //获取一个空闲的上下文
  25.     int unget( int i); // 释放使用完的上下文
  26.       
  27. };

  28. test::test( int i, int ctxtype)
  29. {
  30.     total = i;
  31.     pthread_mutex_init( &mutex, NULL);
  32.     pthread_cond_init( &cond, NULL);
  33.     list = new struct ctxlist[i];
  34.     sqleSetTypeCtx( ctxtype);
  35.     int l;
  36.     struct sqlca sqlca;
  37.     for( l = 0; l < total; l++)
  38.     {
  39.         sqleBeginCtx( &list[l].ctx, SQL_CTX_CREATE_ONLY, NULL, &sqlca);
  40.         list[l].free = 1;
  41.     }
  42.     use = total;
  43. }
  44. test::~test()
  45. {
  46.     int i;
  47.     struct sqlca sqlca;
  48.     for( i = 0; i < total; i++)
  49.     {
  50.         sqleEndCtx( &list[i].ctx,  SQL_CTX_FREE_ONLY, NULL, &sqlca);
  51.     }
  52.     pthread_mutex_destroy( &mutex);
  53.     pthread_cond_destroy( &cond);
  54.     delete(list);
  55. }



  56. int test::get()
  57. {
  58.     pthread_mutex_lock( &mutex);
  59.     while( 0 == use)
  60.         pthread_cond_wait( &cond, &mutex);
  61.     int i;
  62.     struct sqlca sqlca;
  63.     for( i = 0; i < total; i++)
  64.     {
  65.         if( 1 == list[i].free)
  66.                 break;
  67.     }
  68.     list[i].free = 0;
  69.     pthread_mutex_unlock( &mutex);
  70.     return i;
  71. }

  72. int test::unget( int i)
  73. {
  74.     pthread_mutex_lock( &mutex);
  75.     list[i].free = 1;
  76.     pthread_mutex_unlock( &mutex);
  77.     pthread_cond_signal( &cond);
  78.     return i;
  79. }
  80. void *pthread_func( void *arg);

  81. test tt(5, SQL_CTX_MULTI_MANUAL);

  82. int main( void )
  83. {
  84.    

  85.     pthread_t *tids = new pthread_t[10];
  86.     int i;
  87.     for( i = 0; i < 10; i++)
  88.                                 pthread_create( tids+i, NULL, pthread_func, NULL);

  89.     for( i = 0; i < 10; i++)
  90.     {
  91.                                 pthread_join( *(tids+i), NULL);
  92.     }
  93.     cout<<"main finishi"<<endl;
  94.     return 0;
  95. }

  96. void *pthread_func( void *arg)
  97. {
  98.     cout<<"pthread id: "<<pthread_self()<<endl;
  99. }

复制代码
但是运行的时候在 sqleEndCtx( &list.ctx,  SQL_CTX_FREE_ONLY, NULL, &sqlca); 这句就发生错误了。查看发现没有无效的地址。请大家帮忙看看,我那里没有考虑清楚?谢谢。

论坛徽章:
0
2 [报告]
发表于 2012-10-09 22:26 |只看该作者
现在上面的版本是正确的了。谢谢大家的关注。

论坛徽章:
0
3 [报告]
发表于 2012-11-05 09:21 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP