简体   繁体   中英

Why does pthread_create() returns 0 but the thread never starts

I am coming from a Java background and am comfortable using Java's synchronization for multi threaded programming. Recently, I started to dabble in C++ and have had to work on some multi-threaded code. Following is the smallest working example of the issue that I am facing.

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;

}

The thread is launched successfully but I never see one of the prints from the functions executed in the thread itself ie thread(). If I remove the join call then the program just terminates after printing the statements inside the main method.

Two things:

You appear to be missing a call to pthread_mutex_init in your constructors. That might be part of the problem.

The other issue is that your fprintf statements need to have a newline.

That is, this line:

fprintf(stderr,"Inside thread");

Updated to this:

fprintf(stderr,"Inside thread\n");

The \\n char will flush the output buffers such that your messages actually appear on the screen.

Apply similar treatment for the other print statements.

Also, you appear to be using a different lock per variable. Consider just having one single mutex instance. And you may not need a lock in TxStatus either....

Pthreads is a C library. Its data types are dumb C types. If you just place them in a class or struct, they won't get auto-initialized. You need to initialize them. The easiest way to do it is with PTHREAD_MUTEX_INITIALIZER (alternatively with pthread_mutex_init ) which in C++, you can do right next to the member declaration.

This doesn't hang:

#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;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM