C++ 为什么pthread_create()返回0,但线程从未启动
我来自Java背景,对使用Java的同步进行多线程编程感到很舒服。最近,我开始涉足C++并不得不处理一些多线程代码。以下是我所面临的问题的最小工作示例C++ 为什么pthread_create()返回0,但线程从未启动,c++,pthreads,C++,Pthreads,我来自Java背景,对使用Java的同步进行多线程编程感到很舒服。最近,我开始涉足C++并不得不处理一些多线程代码。以下是我所面临的问题的最小工作示例 class TxStatus { private: int numOperations; bool commitStatus; pthread_mutex_t statusLock; public: TxStatus() { this->
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;
}
类TxStatus{
私人:
国际核操作;
布尔委员会;
pthread_mutex_t statusLock;
公众:
TxStatus(){
此->commitStatus=false;
此->数值运算=0;
}
void addNewOperation(){
pthread_mutex_lock(&statusLock);
numperations++;
pthread_mutex_unlock(&statusLock);
}
无效操作已完成(){
pthread_mutex_lock(&statusLock);
核操作--;
pthread_mutex_unlock(&statusLock);
}
无效提交(){
此->commitStatus=true;
}
};
类TxManager{
私人:
无符号长-长全球边界;
无符号长-长txId;
pthread_mutex_t gfLock;
pthread_mutex_t txIdLock;
std::map txStatusMap;
公众:
TxManager(){
pthread_mutex_lock(&gfLock);
全球前沿=1;
pthread_mutex_unlock(&gfLock);
pthread_mutex_lock(&txIdLock);
txId=1;
pthread_mutex_unlock(&txIdLock);
}
无符号long-long-beginNewTx(){
pthread_mutex_lock(&txIdLock);
无符号长newId=txId;
txId++;
pthread_mutex_unlock(&txIdLock);
TxStatus状态OBJ;
insert(std::make_pair(newId,statusObj));
返回newId;
}
void addunflushedo操作(无符号长txId){
txStatusMap[txId].addNewOperation();
}
无效标记操作刷新(无符号长txId){
txStatusMap[txId]。操作已完成();
}
void markCommitted(无符号长txId){
txStatusMap[txId].commit();
}
};
void*线程(void*args){
TxManager TxManager;
fprintf(标准“内螺纹”);
unsigned long long newTxId=txManager.beginNewTx();
fprintf(标准,“发送开始:%d”,newTxId);
txManager.addunflushedooperation(newTxId);
pthread_exit(NULL);
}
int main(){
pthread_t tx_线程;
fprintf(标准“内干管”);
int ret=pthread_create(&tx_thread,NULL,thread,NULL);
如果(ret!=0)
{
fprintf(stderr,“错误启动线程”);
}否则{
fprintf(stderr,“线程成功启动”);
}
if(pthread\u join(tx\u thread,NULL)!=0)
{
fprintf(stderr,“加入pthread失败”);
}
返回0;
}
线程已成功启动,但我从未看到线程本身执行的函数(即thread())的打印结果。如果我删除了join调用,那么程序在打印main方法中的语句后就会终止。两件事:
您似乎在构造函数中缺少对的调用。这可能是问题的一部分
另一个问题是fprintf语句需要换行
就是这一行,
fprintf(stderr,"Inside thread");
更新至:
fprintf(stderr,"Inside thread\n");
\n
字符将刷新输出缓冲区,使消息实际显示在屏幕上
对其他打印语句应用类似的处理
此外,您似乎对每个变量使用了不同的锁。考虑只使用一个互斥实例。您可能也不需要TxStatus中的锁….两件事:
您似乎在构造函数中缺少对的调用。这可能是问题的一部分
另一个问题是fprintf语句需要换行
就是这一行,
fprintf(stderr,"Inside thread");
更新至:
fprintf(stderr,"Inside thread\n");
\n
字符将刷新输出缓冲区,使消息实际显示在屏幕上
对其他打印语句应用类似的处理
此外,您似乎对每个变量使用了不同的锁。考虑只使用一个互斥实例。您可能也不需要TxStatus中的锁……Pthreads是一个C库。它的数据类型是哑C类型。如果您只是将它们放在类或结构中,它们将不会自动初始化。您需要初始化它们。 最简单的方法是用<代码> pthRead MutExx初始化器(或者用<代码> pthRead MutExxIn<代码> >,在C++中,您可以在成员声明旁边做。< /P> 这不适用于:
#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;
}
#包括
#包括
#包括
使用名称空间std;
TxStatus类{
私人:
国际核操作;
布尔委员会;
pthread\u mutex\u t statusLock=pthread\u mutex\u初始值设定项;
公众:
TxStatus(){
此->commitStatus=false;
此->数值运算=0;
}
void addNewOperation(){
pthread_mutex_lock(&statusLock);
numperations++;
pthread_mutex_unlock(&statusLock);
}
无效操作已完成(){
pthread_mutex_lock(&statusLock);
核操作--;
pthread_mutex_unlock(&statusLock);
}
无效提交(){
此->commitStatus=true;
}
};
类TxManager{
私人:
无符号长-长全球边界;
无符号长-长txId;
pthread\u mutex\u t gfLock=pthread\u mutex\u初始值设定项;
pthread\u mutex\u t txIdLock=pthread\u mutex\u初始值设定项;
std::map txStatusMap;
公众:
TxManager(){
pthread_mutex_lock(&gfLock);
全球前沿=1;
pthread_mutex_unlock(&gfLock);
pthread_mutex_lock(&txIdLock);
txId=1;
pthread_mutex_unlock(&txIdLock);
}
无符号long-long-beginNewTx(){
pthread_mutex_lock(&txIdLo)