在下面的代码中,我创建了一个具有循环的线程,当我调用条件时将执行完整的迭代。但是,如果我调用它 1000 次,则该值invoking_thread
不同于exec_thread
. main
发生了什么,我该如何解决这个问题?我不想退出正在运行的线程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。然后打印结果。
现在,添加一些块,大多数时候我都会遇到分段错误。其余时间我没有得到想要的价值。
#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 次调用。依此类推以获得所需的结果。希望我能够解释我的情况。