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

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫(kù)
最近訪問(wèn)板塊 發(fā)新帖
查看: 3101 | 回復(fù): 2
打印 上一主題 下一主題

Segmentation fault [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2012-10-09 20:57 |只看該作者 |倒序?yàn)g覽
本帖最后由 o_unix 于 2012-10-09 22:22 編輯

大家好:我封裝一個(gè)類,用來(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)有考慮清楚?謝謝。

論壇徽章:
0
2 [報(bào)告]
發(fā)表于 2012-10-09 22:26 |只看該作者
現(xiàn)在上面的版本是正確的了。謝謝大家的關(guān)注。

論壇徽章:
0
3 [報(bào)告]
發(fā)表于 2012-11-05 09:21 |只看該作者
提示: 作者被禁止或刪除 內(nèi)容自動(dòng)屏蔽
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國(guó)互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過(guò)ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP