#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
문제는 단지 하나의 스레드가 만들어지고 있다는 것이다! 함수를 메인()에서 4 번, 다른 serverID에서 2 번 호출하여 두 개의 스레드를 생성해야합니까?
무엇이 누락 되었습니까? 이 동작에 기여하는 경우
C 헤더가 아닌 C++ 헤더를 사용하십시오. 'stdio.h '가 아니라'cstdio'입니다. –
또한 도구 모음에서 제공하는 경우'boost :: thread' 또는'std :: thread'를 살펴볼 수도 있습니다. – ereOn
@ereOn 현재 pthreads에 대해서만 배우고 있으므로, 그들에게만 집중하고 싶습니다. 감사합니다. –