- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我的程序在 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。但问题似乎是在第二次和无限期的等待中。
我已经搜索过类似的问题,但似乎没有任何问题是相同的。比较相似的有:
pthread_cond_wait: random segmentation fault
Cancelling pthread_cond_wait() hangs with PRIO_INHERIT mutex
如果有人能帮助我,我将不胜感激。
最佳答案
首先销毁线程正在等待的互斥量和条件,然后执行线程取消。如果你销毁第一个线程,那么这种问题就会出现在 Windows 中。看看this .
关于在 MinGW 下取消 pthread_cond_wait 中的线程会导致访问冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30898193/
很久没看APUE,今天一位朋友问道关于一个mutex的问题,又翻到了以前讨论过的东西,为了不让自己忘记,把曾经的东西总结一下。 先大体看下网上很多地方都有的关于pthread_cond_wait()
为什么在调用 pthread_cond_wait 之前需要锁定互斥锁? 此外,在调用 pthread_cond_signal 之前是否需要锁定(在同一个互斥体上)? ? 谢谢你的帮助。 最佳答案 Wh
从这里:https://computing.llnl.gov/tutorials/pthreads/#ConVarSignal Note that the pthread_cond_wait rout
我正在编写各种代码片段,看看会发生什么。下面的代码旨在延迟所有线程,直到所有线程都到达代码中的某个点,然后使每个线程打印一个独特的数字。由于线程都这样做,因此数字应该以随机顺序出现。 我当前的问题是我
这个问题是关于 llnl 中的 pthread 教程。 。假设有三个线程。 主题 1: pthread_mutex_lock(&mutex) do_something... if condition
因此,pthread_cond_wait()的想法是,它将解锁互斥体并等待条件。 假设您首先手动解锁互斥体,然后等待条件出现。在该时间范围内,在这两个操作之间,您必须假设可能会发生一些不好的事情,另一
我有一个线程 A,其函数在循环中运行,执行某些操作并唤醒另一个线程 B。然后它释放互斥体并继续下一次迭代。线程 B 等待直到收到信号,然后执行某些操作。我的问题是,是否保证 B 在收到信号后会获取互斥
我正在尝试解决 Dining philosophers problem使用 C++。 代码是用 g++ -lpthread 编译的。 整个解决方案在 philosophers github 上.存储库
首先,我的问题是不同的。 在我的场景中,有一个等待线程,它等待条件变量。信号线程信号条件变量。 我的代码是 //Wating thread //Lock the mutex_ //mutex_ is
我正在尝试以我认为应该使用的方式使用 pthread_cond_wait。我在一个等待改变的方法中使用它,当它改变时,它会调用另一个方法来获取数据。如下所示: void waitForSomethin
我的堆栈中有一段代码正在运行,等待 pthread_cond_wait。信号被发送到同一进程,但信号处理程序中没有“pthread_cond_signal”代码来通知 pthread_cond_wai
如果线程调用 pthread_cond_wait(cond_ptr,mutex_ptr) 将返回一个null cond_ptr,是否保证不会睡着? 根据 http://pubs.opengroup.o
关于下面的代码,我的理解是线程1抢锁,检查条件,解锁时钟,将自己置为休眠状态。之后,线程 2 申请相同的锁和增量计数,然后唤醒休眠线程。我的问题是现在的条件还是false,但是唤醒了休眠的线程,会发生
我正在使用 pthread_cond_wait() 但我仍然不确定它是如何工作的。我有更多线程,可以完成这项工作: pthread_mutex_lock(&mutex); while(count()
这个问题是关于 llnl 中的 pthread 教程的.假设有三个线程。 线程 1: pthread_mutex_lock(&mutex) do_something... if condition
bool flag=false; pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER; pthread_cond_t cond=PTHREAD_COND_I
长话短说 我有一个作业需要实现线程同步。在实现过程中,我担心 pthread_cond_wait() 是否也锁定了互斥锁,如果它被虚假唤醒,而不仅仅是成功唤醒。 任务 该任务是一个工作人员/交付问题,
pthread_cond_wait 是否将调用线程置于等待状态以被 pthread_cond_signal/pthread_cond_broadcast 唤醒,这样它就不会轮询和搅动 CPU? 另外,
我目前正在尝试分析第三方源代码中的一个问题,其中线程(对应于 THREAD-T1 的代码片段)处于无限等待状态。怀疑是线程卡在了pthread_cond_wait。以下是相同的详细信息。 代码说明 T
我有以下代码: typedef struct { ... volatile int i_lines_completed; pthread_mutex_t mutex; q265
我是一名优秀的程序员,十分优秀!