gpt4 book ai didi

c++ - 尽管尝试创建两个线程,但只创建了一个线程

转载 作者:行者123 更新时间:2023-11-27 23:25:53 26 4
gpt4 key购买 nike

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <vector>
#include <string>
#include <iostream>

FILE* fp;
pthread_mutex_t demoMutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t conditionVariable = PTHREAD_COND_INITIALIZER;
unsigned int condition = 0;

struct serverInfo
{
unsigned int serverId;
pthread_t threadId;
std::vector<std::string> queue;
};
std::vector<serverInfo> serverInfoVector;

void* printHello(void* threadId)
{
pthread_t* my_tid = (pthread_t*)threadId;

pthread_mutex_lock(&demoMutex);
while (condition == 0)
pthread_cond_wait(&conditionVariable, &demoMutex);

unsigned int i = 0;
char found = false;

if (serverInfoVector.size () > 0) {
while ((i <= serverInfoVector.size()) && (found == false)) {
if (*my_tid == serverInfoVector[i].threadId) {
found = true;
break;
}
else
i++;
}
}

while (!serverInfoVector[i].queue.empty()) {
std::cout << "\nThread: " << pthread_self() << ", poped from queue: " << serverInfoVector[i].queue.front();
serverInfoVector[i].queue.pop_back();
}

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].serverId) {
found = true;
break;
}
else
i++;
}
}

if (found == false) {
// This server doesn't exist, so create a thread for it, create a queue for it, push the message in the corresponding queue.
// Push the server number in the serverNumberArray.

// Create a thread for it.
pthread_t newThread;
int returnValue;
if ((returnValue = pthread_create (&newThread, NULL, printHello, (void*) &newThread)) != 0) {
printf("\nerror: pthread_create failed with error number %d", returnValue);
}
printf("\nIn checkServerExists()`: thread id %ld\n", newThread);

// Push the message in its queue.
serverInfo obj;
obj.serverId = serverNumber;
obj.threadId = newThread;
obj.queue.push_back(message);
serverInfoVector.push_back(obj);

condition++;
pthread_cond_signal(&conditionVariable);
pthread_mutex_unlock(&demoMutex);

for (unsigned int i = 0; i < serverInfoVector.size(); i++)
pthread_join(serverInfoVector[i].threadId, NULL);
}
else {
// This server exists, so lookup its thread and queue, push the message in the corresponding queue.
printf("\nIn else ()`: thread id %ld\n", serverInfoVector[i].threadId);
serverInfoVector[i].queue.push_back(message);

condition++;
pthread_cond_signal(&conditionVariable);
pthread_mutex_unlock(&demoMutex);

for (unsigned int i = 0; i < serverInfoVector.size(); i++)
pthread_join(serverInfoVector[i].threadId, NULL);
}
}

int main()
{
fp = fopen("xyz", "w");

checkServerExists(1, "anisha");
checkServerExists(2, "kaul");
checkServerExists(1, "sanjeev");
checkServerExists(2, "sharma");
}

输出:

In checkServerExists ()`: thread id 140233482061584

Thread: 140233482061584, poped from queue: anisha
In checkServerExists ()`: thread id 140233482061584

In else ()`: thread id 140233482061584

In else ()`: thread id 140233482061584

问题是似乎只创建了一个线程!我在 main() 中调用函数 checkServerExists 4 次,使用不同的 serverID 调用 2 次,所以应该创建两个线程?

我错过了什么?

最佳答案

已编辑:真正的问题是线程终止并连接为正如 hmjd 所指出的那样,它们一经创建。我要离开这个,而不是删除,因为下面也是问题。

我在您发布的输出中看到两个新线程的创建:“In
checkServerExists"
仅在创建新线程时输出。我也查看 printf 中的未定义行为:newThread 具有类型pthread_t,它可以是系统想要的任何东西,并且是可能不是 long,这是您传递给 printf 的格式。据我所知,没有办法(可移植地)输出一个 pthread_t(除了它的十六进制转储字节);您显示为线程 ID 的值没有任何意义。另外,你不能使用==比较pthread_t,你需要使用pthread_equal。 (至少在我用过的一个平台上,pthread_t 是一个结构。)

您的代码还有许多其他奇怪的地方。为什么申报例如,found 类型为 char,而不是类型 bool。为什么found == false,而不是 !found。以及为什么 break; 在循环,因为你在循环控制变量中有条件。一个多checkServerExists 开头的更惯用形式是:

for ( std::vector<ServerInfo>::iterator current = serverInfoVector.begin();
current != serverInfoVector.end() && current->serverId != serverNumber;
++ current ) {
}
if ( current == serverInfoVector.end() ) {
// not found...
} else {
// found...
}

假设您没有为查找创建预测对象,而只是使用 std::find

关于c++ - 尽管尝试创建两个线程,但只创建了一个线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9564580/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com