gpt4 book ai didi

c++ - 在 main() 中的 pthread_join 之后出现段错误

转载 作者:行者123 更新时间:2023-11-30 04:29:10 25 4
gpt4 key购买 nike

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

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 print_thread_id(pthread_t id)
{
size_t i;
for (i = sizeof(i); i; --i)
printf("%02x", *(((unsigned char*) &id) + i - 1));
}

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);
print_thread_id (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);
}
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);
}
}

int main ()
{
checkServerExists (1, "anisha");
checkServerExists (2, "kaul");
checkServerExists (1, "sanjeev");
checkServerExists (2, "sharma");

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

return 0;
}

输出:

In checkServerExists ()`: thread id 139875161245456
00007f37394c8710
In checkServerExists ()`: thread id 139875152852752
00007f3738cc7710
In else ()`: thread id 139875161245456

In else ()`: thread id 139875152852752

Segmentation fault
  • 问题是 seg 错误,即使我在 main 中加入线程时也会出现这种错误。
  • 在 OpenSuse 上,pthread_t 类型定义为无符号长型,因此我尝试打印它。另外,我尝试调用函数 print_thread_id 如下所示:https://stackoverflow.com/a/1759894/462608

小改进:

In checkServerExists ()`: thread id 139975945303824
00007f4eb07f3710
In checkServerExists ()`: thread id 139975936911120
00007f4eafff2710
In else ()`: thread id 139975945303824

In else ()`: thread id 139975936911120

Thread: 139975936911120, poped from queue: 1kaul1�&��N�&��Na7�Npq`�q`�q`'��N�s`�s`�s`!�q`!�q`�s`1sharma
Segmentation fault

现在正在打印来自第二个线程的 poped 值,但段错误仍然存​​在。

最佳答案

段错误是由于我没有在以下代码中使用pthread_equal 来比较threadId:

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

感谢@David Schwartz 在这里提到它:https://stackoverflow.com/a/9564998/462608和 :doh: 因为我没有仔细听。

关于c++ - 在 main() 中的 pthread_join 之后出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9579031/

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