在下面的代码中,我创建了一个具有循环的线程,当我调用条件时,该循环将执行完整迭代。但是,如果我调用它 1000 次,则 invoking_thread
值与 main
末尾的 exec_thread
不同。发生了什么事,我该如何解决这个问题?我不想退出运行 threadfunc
的线程,因为我可能需要使用它进行进一步的操作。
#include < pthread.h>
#include < stdio.h>
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int invoking_thread = 0, exec_thread = 0;
pthread_t pth;
void *threadfunc(void *parm)
{
int x;
for (;;) {
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond, &mutex);
//printf("Inside the thread %d\n", ins);
exec_thread++;
pthread_mutex_unlock(&mutex);
}
return NULL;
}
void create_thread () {
pthread_create(&pth,NULL,threadfunc,"foo");
}
int main(int argc, char **argv)
{
create_thread();
int y = 0;
while (1) {
if (y == 1000) {
break;
}
y++;
invoking_thread++;
printf("Count: Invoked %d and Inside : %d\n", invoking_thread, exec_thread);
pthread_cond_signal( &cond );
pthread_mutex_lock( &mutex );
pthread_mutex_unlock( &mutex );
}
printf("Count: Invoked %d and Inside : %d\n", invoking_thread, exec_thread);
printf("Main completed\n");
return 0;
}
进一步说明的详细信息: 我可以告诉你我的整个情况:一个 1000 大小的数组被初始化为 0 值。 1 个线程以无限循环启动。对于 1000 次迭代,我将信号传递给线程以递增数组每个元素的值。传递信号后,这些值在循环中乘以 2。在下一步中,再次将 1000 个信号传递给线程以递增数组每个元素的值。然后,和之前一样,循环将所有元素值乘以 2。然后打印结果。
现在,添加一些 block ,大多数时候我都会遇到段错误。其余时间我没有得到想要的值(value)。
#include < pthread.h>
#include < stdio.h>
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int invoking_thread = 0, exec_thread = 0, signal_count = 0;
pthread_t pth;
int res[1000];
void *threadfunc(void *parm) {
for (;;) {
pthread_mutex_lock(&mutex);
while (signal_count == 0)
pthread_cond_wait(&cond, &mutex);
signal_count--; // consume a signal
//printf("Inside the thread res[%d]++\n", exec_thread);
exec_thread++;
res[exec_thread]++;
pthread_mutex_unlock(&mutex);
}
return NULL;
}
void create_thread () {
pthread_create(&pth,NULL,threadfunc,"foo");
}
int main(int argc, char **argv)
{
create_thread();
int y;
for (y = 0;y<1000;y++) {
res[y] = 0;
}
y = 0;
while (1) {
if (y == 1000) {
break;
}
y++;
invoking_thread++;
//printf("Count: Invoked %d and Inside : %d\n", invoking_thread, exec_thread);
pthread_mutex_lock( &mutex );
signal_count++;
pthread_mutex_unlock( &mutex );
pthread_cond_signal( &cond );
}
printf("Count: Invoked %d and Inside : %d\n", invoking_thread, exec_thread);
for (y = 0;y<1000;y++) {
res[y] = res[y]*2;
}
exec_thread = 0;
y = 0;
while (1) {
if (y == 1000) {
break;
}
y++;
invoking_thread++;
//printf("Count: Invoked %d and Inside : %d\n", invoking_thread, exec_thread);
pthread_mutex_lock( &mutex );
signal_count++;
pthread_mutex_unlock( &mutex );
pthread_cond_signal( &cond );
}
printf("Count: Invoked %d and Inside : %d\n", invoking_thread, exec_thread);
for (y = 0;y<1000;y++) {
res[y] = res[y]*2;
}
// result
for (y = 0;y<1000;y++) {
printf("%d result for %d\n",res[y], y);
}
printf("Main completed\n");
return 0;
}
因此我的问题是,线程调用应该必须在前 1000 个信号之后等待,而它不会等待,然后让代码进行计算,然后应该允许它进行进一步的 1000 个调用。等等以获得想要的结果。希望我能解释一下我的情况。
最佳答案
如果有线程在等待,pthread_cond_signal()
将唤醒任何在该条件下等待的线程。如果没有,那么该调用将不会执行任何操作。这很可能就是您的情况发生的情况。随后有多次对 pthread_cond_signal()
的调用,而工作线程实际上并未休眠。
为了确保一次调用一个工作线程通过,您必须使用两个 pthread_cond_t
- 一个用于指示工作线程的启动,第二个用于通知工作已结束。另一种是忙着等待,直到 1000 个工作完成。这是修改后的代码:
int main(int argc, char **argv)
{
create_thread();
int y = 0;
int executed = 0;
while (executed <1000) {
invoking_thread++;
printf("Count: Invoked %d and Inside : %d\n", invoking_thread, exec_thread);
pthread_mutex_lock( &mutex );
executed = exec_thread; // read inside mutex to ensure variable visibility
pthread_cond_signal( &cond );
pthread_mutex_unlock( &mutex );
}
printf("Count: Invoked %d and Inside : %d\n", invoking_thread, exec_thread);
printf("Main completed\n");
return 0;
}
关于c - 为什么C程序中调用线程数比执行线程数多?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18330269/