亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

Chinaunix

標(biāo)題: Segmentation fault [打印本頁(yè)]

作者: o_unix    時(shí)間: 2012-10-09 20:57
標(biāo)題: Segmentation fault
本帖最后由 o_unix 于 2012-10-09 22:22 編輯

大家好:我封裝一個(gè)類(lèi),用來(lái)管理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. // 用于保存上下文并標(biāo)識(shí)當(dāng)前上下文是否空閑, free 為 1時(shí)空閑,0 時(shí)在使用。
  8. struct ctxlist
  9. {
  10.     void *ctx;
  11.     int free;
  12. };

  13. class test
  14. {
  15.     /*用于管理上下文訪問(wèn)*/
  16.     pthread_mutex_t mutex;
  17.     pthread_cond_t cond;
  18.     struct ctxlist *list; //保存上下文的起始地址

  19.     int use; // 沒(méi)有被使用的上下文
  20.     int total; // 一共創(chuàng)建了多少個(gè)上下文
  21.     public:
  22.     test( int i, int ctxtype);
  23.     ~test();
  24.     int get(); //獲取一個(gè)空閑的上下文
  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. }

復(fù)制代碼
但是運(yùn)行的時(shí)候在 sqleEndCtx( &list.ctx,  SQL_CTX_FREE_ONLY, NULL, &sqlca); 這句就發(fā)生錯(cuò)誤了。查看發(fā)現(xiàn)沒(méi)有無(wú)效的地址。請(qǐng)大家?guī)兔纯矗夷抢餂](méi)有考慮清楚?謝謝。
作者: o_unix    時(shí)間: 2012-10-09 22:26
現(xiàn)在上面的版本是正確的了。謝謝大家的關(guān)注。
作者: lloydm    時(shí)間: 2012-11-05 09:21
提示: 作者被禁止或刪除 內(nèi)容自動(dòng)屏蔽




歡迎光臨 Chinaunix (http://www.72891.cn/) Powered by Discuz! X3.2