gpt4 book ai didi

c++ - 为什么 pthread_create() 返回 0 但线程永远不会启动

转载 作者:行者123 更新时间:2023-11-28 01:31:44 26 4
gpt4 key购买 nike

我有 Java 背景,很乐意使用 Java 的同步进行多线程编程。最近,我开始涉足 C++,不得不处理一些多线程代码。以下是我面临的问题的最小工作示例。

class TxStatus {
private:
int numOperations;
bool commitStatus;
pthread_mutex_t statusLock;

public:
TxStatus() {
this->commitStatus = false;
this->numOperations = 0;
}
void addNewOperation() {
pthread_mutex_lock(&statusLock);
numOperations++;
pthread_mutex_unlock(&statusLock);
}
void operationCompleted() {
pthread_mutex_lock(&statusLock);
numOperations--;
pthread_mutex_unlock(&statusLock);
}
void commit() {
this->commitStatus = true;
}

};

class TxManager {
private:
unsigned long long globalFrontier;
unsigned long long txId;

pthread_mutex_t gfLock;
pthread_mutex_t txIdLock;

std::map<unsigned long long, TxStatus> txStatusMap;

public:
TxManager() {
pthread_mutex_lock(&gfLock);
globalFrontier = 1;
pthread_mutex_unlock(&gfLock);

pthread_mutex_lock(&txIdLock);
txId = 1;
pthread_mutex_unlock(&txIdLock);
}

unsigned long long beginNewTx() {
pthread_mutex_lock(&txIdLock);
unsigned long long newId = txId;
txId++;
pthread_mutex_unlock(&txIdLock);
TxStatus statusObj;
txStatusMap.insert(std::make_pair(newId,statusObj));
return newId;
}

void addUnflushedOperation(unsigned long long txId) {
txStatusMap[txId].addNewOperation();
}
void markOperationAsFlushed(unsigned long long txId) {
txStatusMap[txId].operationCompleted();
}
void markCommitted(unsigned long long txId) {
txStatusMap[txId].commit();
}
};


void * thread( void *args){

TxManager txManager;
fprintf(stderr,"Inside thread");
unsigned long long newTxId = txManager.beginNewTx();

fprintf(stderr,"Tx Started: %d", newTxId );
txManager.addUnflushedOperation(newTxId);
pthread_exit(NULL);

}

int main(){
pthread_t tx_thread;
fprintf(stderr,"Inside main");
int ret = pthread_create(&tx_thread, NULL, thread, NULL);
if (ret != 0)
{
fprintf(stderr,"Error launching thread");
}else{
fprintf(stderr,"Thread launched successfully");
}


if (pthread_join(tx_thread, NULL) != 0)
{
fprintf(stderr,"Join pthread failed");
}

return 0;

}

线程已成功启动,但我从未看到线程本身执行的函数(即 thread())的输出之一。如果我删除 join 调用,那么程序将在 main 方法中打印语句后终止。

最佳答案

Pthreads 是一个 C 库。它的数据类型是哑 C 类型。如果您只是将它们放在一个类或结构中,它们将不会自动初始化。你需要初始化它们。最简单的方法是使用 PTHREAD_MUTEX_INITIALIZER(或者使用 pthread_mutex_init),在 C++ 中,您可以在成员声明旁边执行此操作。

这不会挂起:

#include <pthread.h>
#include <map>
#include <stdio.h>
using namespace std;
class TxStatus {
private:
int numOperations;
bool commitStatus;
pthread_mutex_t statusLock = PTHREAD_MUTEX_INITIALIZER;

public:
TxStatus() {
this->commitStatus = false;
this->numOperations = 0;
}
void addNewOperation() {
pthread_mutex_lock(&statusLock);
numOperations++;
pthread_mutex_unlock(&statusLock);
}
void operationCompleted() {
pthread_mutex_lock(&statusLock);
numOperations--;
pthread_mutex_unlock(&statusLock);
}
void commit() {
this->commitStatus = true;
}

};

class TxManager {
private:
unsigned long long globalFrontier;
unsigned long long txId;

pthread_mutex_t gfLock = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t txIdLock = PTHREAD_MUTEX_INITIALIZER;

std::map<unsigned long long, TxStatus> txStatusMap;

public:
TxManager() {
pthread_mutex_lock(&gfLock);
globalFrontier = 1;
pthread_mutex_unlock(&gfLock);

pthread_mutex_lock(&txIdLock);
txId = 1;
pthread_mutex_unlock(&txIdLock);
}

unsigned long long beginNewTx() {
pthread_mutex_lock(&txIdLock);
unsigned long long newId = txId;
txId++;
pthread_mutex_unlock(&txIdLock);
TxStatus statusObj;
txStatusMap.insert(std::make_pair(newId,statusObj));
return newId;
}

void addUnflushedOperation(unsigned long long txId) {
txStatusMap[txId].addNewOperation();
}
void markOperationAsFlushed(unsigned long long txId) {
txStatusMap[txId].operationCompleted();
}
void markCommitted(unsigned long long txId) {
txStatusMap[txId].commit();
}
};


void * thread( void *args){

TxManager txManager;
fprintf(stderr,"Inside thread\n");
unsigned long long newTxId = txManager.beginNewTx();

fprintf(stderr,"Tx Started: %llu", newTxId );
txManager.addUnflushedOperation(newTxId);
pthread_exit(NULL);

}

int main(){
pthread_t tx_thread;
fprintf(stderr,"Inside main\n");
int ret = pthread_create(&tx_thread, NULL, thread, NULL);
if (ret != 0)
{
fprintf(stderr,"Error launching thread");
}else{
fprintf(stderr,"Thread launched successfully");
}


if (pthread_join(tx_thread, NULL) != 0)
{
fprintf(stderr,"Join pthread failed");
}

return 0;

}

关于c++ - 为什么 pthread_create() 返回 0 但线程永远不会启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51239111/

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