1

Windows (Windows 7 - 32 ビット) でアクセス違反が発生し、プログラムが停止します。MinGW の下で gcc 4.8.1 でコンパイルされた C コードです。pthreads-w32 2.9.1 を使用します。いくつかのスレッドが同時に動作しており、他に明らかな問題はありません。数日間は問題なく動作することもあれば、数時間で失敗することもあります。コードはいくつかの Linux アーキテクチャでコンパイルすることもできますが、問題はありません。デバッガーの下でプログラムを実行することも非常に困難です。

以下は、クラッシュが発生する関数です。

static void *timeout_lector(void *indice)
{
    int e;
    pthread_mutex_t fm;
    pthread_cond_t fc;
    struct timespec ts;
    struct timeval tv;
    struct equipo *eq;

    eq = &estacion.equipos[*((int *)indice)];

    if (pthread_mutex_init(&fm, NULL) || pthread_cond_init(&fc, NULL)) {
        LOG_PRINT("Error creating mutex or cond in timeout_lector.\n");
        exit(1);
    }
    pthread_mutex_lock(&fm);
    gettimeofday(&tv, NULL);
    ts.tv_sec  = tv.tv_sec;
    ts.tv_nsec = tv.tv_usec * 1000;
    siguiente_tmseg(&ts, 150);  /* Increments ts by 150 ms */
    pthread_cleanup_push(thread_cleanup_fc, (void *)(&fc));
    pthread_cleanup_push(thread_cleanup_fm, (void *)(&fm));
    if ((e = pthread_cond_timedwait(&fc, &fm, &ts)) != ETIMEDOUT) {
        LOG_PRINT("Error waiting in timeout_lector: %d.\n", e);
        exit(1);
    }
    pthread_mutex_lock(&eq->mutexto);
    eq->to = 1;
    pthread_mutex_unlock(&eq->mutexto);
    e = pthread_cond_wait(&fc, &fm);  /* wait until we are cancelled */
    LOG_PRINT("Error in timeout_lector: %d.\n", e);
    exit(1);
    /* Next lines are never executed and just for correct syntax */
    pthread_cleanup_pop(1);
    pthread_cleanup_pop(1);
    return(NULL);
}

クリーンアップ関数は次のとおりです。

void thread_cleanup_fc(void *fc)
{
    pthread_cond_destroy(fc);
}

void thread_cleanup_fm(void *fm)
{
    pthread_mutex_unlock(fm);
    pthread_mutex_destroy(fm);
}

Dr.MinGW 0.7.3 で実行しており、レポートは次のとおりです。

estacion.exe caused an Access Violation at location 62489D38 in module pthreadGC2.dll Writing to location 0101FCFC.

Registers:
eax=00000000 ebx=0444febc ecx=ffffffff edx=0101fcfc esi=0444fee0 edi=0444fcf0
eip=62489d38 esp=0444fcd0 ebp=0444fd18 iopl=0         nv up ei pl zr na po nc
cs=001b  ss=0023  ds=0023  es=0023  fs=003b  gs=0000             efl=00010246

AddrPC   Params
62489D38 0444FEE0 00000000 FFFFFFFF  pthreadGC2.dll!pthread_cond_destroy
004194E0 0444FEE0 0444FEBC 0444FD68  estacion.exe!thread_cleanup_fc  [C:/codigo/CeltaDAS/estacion/tiempo.c @ 223]
6248ABB5 00000684 FFFFFFFF 0444FE08  pthreadGC2.dll!ptw32_pop_cleanup.constprop.3
6248BAAF 00000000 00000000 03C63EE0  pthreadGC2.dll!sem_timedwait
6248512D 003E1138 0444FEB0 767DA53A  pthreadGC2.dll!pthread_getspecific
62485D22 003E1138 00000078 00000000  pthreadGC2.dll!ptw32_push_cleanup
62485D22 0444FEE0 0444FEE4 0444FED0  pthreadGC2.dll!ptw32_push_cleanup
0040C0D1 014908EC 014AAD70 00000000  estacion.exe!timeout_lector  [C:/codigo/CeltaDAS/estacion/equipos.c @ 214]
62485BD3 014B1190 0805AA4A 00000000  pthreadGC2.dll!ptw32_threadStart@4
767E1287 0444FF94 773DEE1C 03CF0048  msvcrt.dll!itow_s
767E1328 03CF0048 0444FFD4 777037EB  msvcrt.dll!endthreadex
773DEE1C 03CF0048 319EB544 00000000  kernel32.dll!BaseThreadInitThunk
777037EB 767E12E5 03CF0048 00000000  ntdll.dll!RtlInitializeExceptionChain
777037BE 767E12E5 03CF0048 00000000  ntdll.dll!RtlInitializeExceptionChain

Windows 6.1.7601
DrMingw 0.7.3

Dr.MinGW は、timeout_lector の 214 行目を通過するスタック トレースで報告します。コードの pthread_cond_wait 行に対応します。

このスレッドは継続的に作成され、いくつかは並行して作成されます。キャンセルできるまでしばらく待機し、時間が経過すると変数を変更し、キャンセルされるまで待機します。これは、別のスレッドによってほぼ即座に行われます。待機に使用でき、さまざまなシステムに移植できるキャンセル可能な関数はあまりないため、pthread_cond_timedwait を選択しました。しかし、問題は 2 回目の無期限の待機にあるようです。

同様の問題を検索しましたが、同じ問題はないようです。
pthread_cond_wait: ランダム セグメンテーション エラーpthread_cond_wait() をキャンセルするとPRIO_INHERIT
ミューテックスでハングする

誰かがこれを手伝ってくれたら本当にありがたいです。

4

1 に答える 1

0

まず、スレッドが待機しているミューテックスと条件を破棄してから、スレッドをキャンセルします。最初のスレッドを破棄すると、この種の問題がウィンドウに発生します。これを見てください。

于 2015-08-20T10:48:27.160 に答える