FILE *fp;
pthread_mutex_t demoMutex;
unsigned short globalThreadIndex = 0;
struct serverInfo
{
unsigned int serverId;
pthread_t threadId;
std :: vector <pthread_t> queue;
};
std :: vector <serverInfo> serverInfoVector;
void * printHello (void* threadId)
{
pthread_t *my_tid = (pthread_t *)&threadId;
printf ("\nIn `printHello ()`: thread id %ld\n", pthread_self ());
***pthread_mutex_lock (&demoMutex);***
unsigned int i = 0;
char found = false;
if (serverInfoVector.size () > 0)
{
// The following code should be executed when and only when the vector isn't empty.
***pthread_cond_wait (&demoConditionVar, &demoMutex);***
while ((i <= serverInfoVector.size ()) && (found == false))
{
if (*my_tid == serverInfoVector [i].threadId)
{
found = true;
break;
}
else
i++;
}
if (found == true)
{
pthread_t writeToFile = pthread_self ();
unsigned short iterate;
for (iterate = 0; iterate < 10000; iterate++)
{
fprintf (fp, " %d %d", iterate, 4);
fprintf (fp, " %lu %lu", writeToFile, sizeof (pthread_t));
if (!serverInfoVector [i].queue.empty ())
{
fprintf (fp, " %c %u", 'A', 1);
fprintf (fp, " %lu %lu", serverInfoVector [i].queue.front (), sizeof (pthread_t));
serverInfoVector [i].queue.pop_back ();
}
fprintf (fp, "\n %lu %u", writeToFile, 1);
}
}
***pthread_mutex_unlock (&demoMutex);***
}
***pthread_exit (NULL);***
}
void checkServerExists (unsigned int serverNumber, std :: string message)
{
unsigned int i = 0;
char found = false;
if (serverInfoVector.size () > 0)
{
while ((i <= serverInfoVector.size ()) && (found == false))
{
if (serverNumber == serverInfoVector [i].threadId)
{
found = true;
break;
}
else
i++;
}
}
if (found == false)
{
pthread_t newThread [2];
int returnValue;
if ((returnValue = pthread_create (&newThread [globalThreadIndex], NULL, printHello, (void*) &newThread [globalThreadIndex])) != 0)
{
printf ("\nerror: pthread_create failed with error number %d", returnValue);
}
printf ("\nIn checkServerExists ()`: thread id %ld\n", newThread [globalThreadIndex]);
serverInfo obj;
obj.serverId = serverNumber;
obj.threadId = newThread [globalThreadIndex];
obj.queue.push_back (newThread [globalThreadIndex]);
serverInfoVector.push_back (obj);
// Now, since something has been pushed in the vector, it makes sense to wake up the sleeping thread.
***pthread_mutex_lock (&demoMutex)***;
// Now, since something has been pushed in the vector, it makes sense to wake up the sleeping thread.
if (serverInfoVector.size () > 0)
***pthread_cond_signal (&demoConditionVar);***
***pthread_mutex_unlock(&demoMutex);***
pthread_join (newThread [globalThreadIndex], NULL);
}
else
{
}
}
int main ()
{
fp = fopen ("xyz", "w");
***pthread_mutex_init (&demoMutex, NULL);
pthread_cond_t demoConditionVar = PTHREAD_COND_INITIALIZER;***
checkServerExists (1, "anisha");
globalThreadIndex++;
checkServerExists (2, "anisha");
return 0;
}
此代码已得到改进,问题仍然存在(程序挂起,第二个线程未显示)。
checkServerExists
函数(在当前情况下)导致创建一个新线程,并将其存储在数组newThread
中。
checkServerExists
函数启动一个新线程,
当创建线程时,它将立即调用其函数printHello
并在条件变量上被阻塞。
checkServerExists
函数然后在全局结构的队列中输入值,设置线程唤醒的信号。
现在,我错过了什么意思?
FILE *fp;
pthread_mutex_t demoMutex;
unsigned short globalThreadIndex = 0;
struct serverInfo
{
unsigned int serverId;
pthread_t threadId;
std :: vector <pthread_t> queue;
};
std :: vector <serverInfo> serverInfoVector;
void * printHello (void* threadId)
{
pthread_t *my_tid = (pthread_t *)&threadId;
printf ("\nIn `printHello ()`: thread id %ld\n", pthread_self ());
***pthread_mutex_lock (&demoMutex);***
unsigned int i = 0;
char found = false;
if (serverInfoVector.size () > 0)
{
// The following code should be executed when and only when the vector isn't empty.
***pthread_cond_wait (&demoConditionVar, &demoMutex);***
while ((i <= serverInfoVector.size ()) && (found == false))
{
if (*my_tid == serverInfoVector [i].threadId)
{
found = true;
break;
}
else
i++;
}
if (found == true)
{
pthread_t writeToFile = pthread_self ();
unsigned short iterate;
for (iterate = 0; iterate < 10000; iterate++)
{
fprintf (fp, " %d %d", iterate, 4);
fprintf (fp, " %lu %lu", writeToFile, sizeof (pthread_t));
if (!serverInfoVector [i].queue.empty ())
{
fprintf (fp, " %c %u", 'A', 1);
fprintf (fp, " %lu %lu", serverInfoVector [i].queue.front (), sizeof (pthread_t));
serverInfoVector [i].queue.pop_back ();
}
fprintf (fp, "\n %lu %u", writeToFile, 1);
}
}
***pthread_mutex_unlock (&demoMutex);***
}
***pthread_exit (NULL);***
}
void checkServerExists (unsigned int serverNumber, std :: string message)
{
unsigned int i = 0;
char found = false;
***pthread_mutex_lock (&demoMutex);***
if (serverInfoVector.size () > 0)
{
while ((i <= serverInfoVector.size ()) && (found == false))
{
if (serverNumber == serverInfoVector [i].threadId)
{
found = true;
break;
}
else
i++;
}
}
if (found == false)
{
pthread_t newThread [2];
int returnValue;
if ((returnValue = pthread_create (&newThread [globalThreadIndex], NULL, printHello, (void*) &newThread [globalThreadIndex])) != 0)
{
printf ("\nerror: pthread_create failed with error number %d", returnValue);
}
printf ("\nIn checkServerExists ()`: thread id %ld\n", newThread [globalThreadIndex]);
serverInfo obj;
obj.serverId = serverNumber;
obj.threadId = newThread [globalThreadIndex];
obj.queue.push_back (newThread [globalThreadIndex]);
serverInfoVector.push_back (obj);
// Now, since something has been pushed in the vector, it makes sense to wake up the sleeping thread.
// Now, since something has been pushed in the vector, it makes sense to wake up the sleeping thread.
if (serverInfoVector.size () > 0)
***pthread_cond_signal (&demoConditionVar);***
pthread_join (newThread [globalThreadIndex], NULL);
}
else
{
}
***pthread_mutex_unlock(&demoMutex);***
}
int main ()
{
fp = fopen ("xyz", "w");
***pthread_mutex_init (&demoMutex, NULL);
pthread_cond_t demoConditionVar = PTHREAD_COND_INITIALIZER;***
checkServerExists (1, "anisha");
globalThreadIndex++;
checkServerExists (2, "anisha");
return 0;
}
在此编辑中,我将锁定放在checkServerExists
函数的顶部(此函数处理全局结构serverInfoVector
)
还挂着。 :DOH:
答案 0 :(得分:5)
此代码已损坏。
首先,您要修改checkServerExists
内的变量而不锁定互斥锁。这是未定义的行为。
如果你解决了这个问题,那么你也没有在printHello
函数之外发信号通知你的条件变量。因此,一旦线程在pthread_cond_wait
调用中被阻塞,它将仅由于虚假唤醒而被唤醒,并且当另一个printHello
线程发出信号时它将被唤醒。您应该在设置pthread_cond_signal
标记的位置调用condition
,而不是printHello
。
条件变量只是一种通知机制。您需要将谓词与它相关联,这是等待的条件(在您的情况下,condition!=0
)。您必须确保在设置和测试条件时访问的变量受互斥锁保护,并且此互斥锁是传递给pthread_cond_wait
的互斥锁,以避免潜在的竞争条件。当您设置变量以指示睡眠线程应该唤醒时,您调用pthread_cond_signal
。
我已稍微修改了您的代码以使其正常工作。特别是,我将循环置于pthread_cond_wait
调用之前,并在调用pthread_join
之前解锁了互斥锁,以便printHello
线程可以获取互斥锁并继续。永远不应该在线程连接中持有互斥锁。这段代码仍然可以大大改进 - 除此之外,它不是例外安全。
#include <pthread.h>
#include <stdio.h>
#include <vector>
#include <string>
FILE *fp;
pthread_mutex_t demoMutex;
pthread_cond_t demoConditionVar;
unsigned short globalThreadIndex = 0;
struct serverInfo
{
unsigned int serverId;
pthread_t threadId;
std :: vector <pthread_t> queue;
};
std :: vector <serverInfo> serverInfoVector;
void * printHello (void* threadId)
{
pthread_t *my_tid = (pthread_t *)threadId;
printf ("\nIn `printHello ()`: thread id %ld\n", pthread_self ());
pthread_mutex_lock (&demoMutex);
unsigned int i = 0;
bool found = false;
while (serverInfoVector.empty())
pthread_cond_wait (&demoConditionVar, &demoMutex);
while ((i < serverInfoVector.size ()) && !found)
{
if (*my_tid == serverInfoVector [i].threadId)
{
found = true;
break;
}
else
i++;
}
if (found)
{
pthread_t writeToFile = pthread_self ();
unsigned short iterate;
for (iterate = 0; iterate < 10000; iterate++)
{
fprintf (fp, " %d %d", iterate, 4);
fprintf (fp, " %lu %lu", writeToFile, sizeof (pthread_t));
if (!serverInfoVector [i].queue.empty ())
{
fprintf (fp, " %c %u", 'A', 1);
fprintf (fp, " %lu %lu", serverInfoVector [i].queue.front (), sizeof (pthread_t));
serverInfoVector [i].queue.pop_back ();
}
fprintf (fp, "\n %lu %u", writeToFile, 1);
}
}
pthread_mutex_unlock (&demoMutex);
pthread_exit (NULL);
}
void checkServerExists (unsigned int serverNumber, std :: string message)
{
unsigned int i = 0;
bool found = false;
pthread_mutex_lock (&demoMutex);
if (serverInfoVector.size () > 0)
{
while ((i <= serverInfoVector.size ()) && (found == false))
{
if (serverNumber == serverInfoVector [i].threadId)
{
found = true;
break;
}
else
i++;
}
}
if (!found)
{
pthread_t newThread [2];
int returnValue;
if ((returnValue = pthread_create (&newThread [globalThreadIndex], NULL, printHello, (void*) &newThread [globalThreadIndex])) != 0)
{
printf ("\nerror: pthread_create failed with error number %d", returnValue);
}
printf ("\nIn checkServerExists ()`: thread id %ld\n", newThread [globalThreadIndex]);
serverInfo obj;
obj.serverId = serverNumber;
obj.threadId = newThread [globalThreadIndex];
obj.queue.push_back (newThread [globalThreadIndex]);
serverInfoVector.push_back (obj);
// Now, since something has been pushed in the vector, it makes sense to wake up the sleeping thread.
// Now, since something has been pushed in the vector, it makes sense to wake up the sleeping thread.
pthread_cond_signal (&demoConditionVar);
pthread_mutex_unlock(&demoMutex);
pthread_join (newThread [globalThreadIndex], NULL);
}
else
{
pthread_mutex_unlock(&demoMutex);
}
}
int main ()
{
fp = fopen ("xyz", "w");
pthread_mutex_init (&demoMutex, NULL);
pthread_cond_init (&demoConditionVar, NULL);
checkServerExists (1, "anisha");
globalThreadIndex++;
checkServerExists (2, "anisha");
return 0;
}
答案 1 :(得分:0)
我猜第二个帖子没有从等待中醒来。第一个线程尝试发信号通知条件变量,但秒尚未启动。我想知道为什么它甚至超过了第一个线程中的等待,但它可能永远不会等待因为线程在条件已经为1之后开始执行。