在下面的代码中,我创建了一个具有循环的线程,当我调用条件时它将执行完整的迭代。但是,如果我调用1000次,则invoking_thread
值与main
末尾的exec_thread
不同。发生了什么,我该如何解决这个问题?我不想退出运行threadfunc
的线程,因为我可能需要将其用于进一步操作。进一步说明为什么调用线程数比在C程序中执行线程数多?
#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所调用。等等以获得期望的结果。希望我能够解释我的情况。
我刚刚编译了程序并确认这是问题所在。主线程像一个门口的公牛一样运行 - 发送1000个信号,但另一个线程在主线程退出之前从来没有机会收到它们。如果你在主线程中进行睡眠,那么另一个线程将接收信号。 –
感谢您的关注。但是加入睡眠和延迟并不会帮助我达到这个速度。有什么方法可以让我获得信号,让所有1000人完成。这将帮助我在此之后添加检查 –
@Vishwadeep最简单的事情就是在主服务器中忙于等待(并保持信号),但线程实际上已完成1000次工作。 – Dariusz