CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Oct 2001
    Posts
    745

    Thread Exiting Problem

    I have an application which have to be run under Windows as well as Linux.It is a multithreaded application also.

    Iam having problems(Run Time Errror) Exiting with the threads in Linux when it comes to the pthread_join statement.
    The Windows side runs well.I have implemented it under windows & Linux as follows.
    Could anyone tell me what should be done under Linux Side for safely exiting the threads

    Thanks in advance....


    For Windows:
    ------------

    thread_write->Start();
    thread_read->Start();
    // Getting Thread Handles.
    HANDLE hHandles[2] = {thread_write->GetHandle(),thread_read->GetHandle()};
    int NumThreads = 2;
    do
    {
    switch(WaitForMultipleObjects(NumThreads,hHandles,false,INFINITE))
    {
    case WAIT_OBJECT_0 +0:
    if(hHandles[0]==thread_write->GetHandle())
    m_Readaccess->SetExitFlag();// Write thread terminated - so allow reader to exit gracefully

    // Put last handle in where this one completed and
    // reduce number of handles in array
    hHandles[0]= hHandles[--NumThreads];
    break;

    default:
    hHandles[0]= hHandles[--NumThreads];
    break;
    }

    }
    while(NumThreads)






    For Linux:
    ----------

    typedef WriterThread<LinuxThread> WriterThread;
    typedef ReaderThread<LinuxThread> ReaderThread;
    m_pCycleBuffer = new T_BUFFERTYPE(BUF_SIZE);
    m_BasePtr = new LinuxThread();
    m_Readaccess = new C_BufferReadAccess(*m_pCycleBuffer,* m_BasePtr);
    WriterThread *thread_write = new WriterThread(*m_pCycleBuffer,*m_pdma,* m_RdiFilename,*m_pmta);
    ReaderThread *thread_read = new ReaderThread(*m_pCycleBuffer,*m_pida,*m_Readaccess);
    thread_write->Start();
    thread_read->Start();
    void *status = NULL;
    pthread_join(thread_write->GetHandle(),&status);
    pthread_join(thread_read->GetHandle(),&status);

  2. #2
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,020
    Doesn't look like anything wrong with the code: are they compiled for multi-threaded operation?

    If you want to force termination, you could always use _exit but that is just like a power cut: nothing exits cleanly.
    Succinct is verbose for terse

  3. #3
    Join Date
    Oct 2001
    Posts
    745
    Yes,They are compiled for Multithreaded operation.


    What shold be given as the 2nd paramter to pthread_join.Iam giving it as follows now.(Null).Could that be the problem..
    I want to make the writer thread exit first & then the reader Thread.
    void *status = NULL;
    pthread_join(thread_write->GetHandle(),&status);

  4. #4
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,020
    It doesn't have to be anything as it is overwritten when the thread exits. The threads have to be created as void* (void*).

    Does the following example work? I don't have the pthread libraries on my Linux system at the moment so I cannot try it out.
    Code:
    #include <pthread.h>
    #include <iostream>
    #include <stdlib.h>
    using namespace std;
    bool running = true;
    void* One (void*)
    {
        while (running)
            cout << "One" << endl;
    
        return (void*) 0;
    }
    
    void* Two (void*)
    {
        while (running)
            cout << "Two" << endl;
    
        return (void*) 0;
    }
    
    main ()
    {
        pthread_t one, two;
        void* status;
        pthread_create (&one, 0, One, (void*) 0);
        pthread_create (&two, 0, Two, (void*) 0);
    
        sleep (60);
        running = false;
        pthread_join (one, &status);
        pthread_join (two, &status);
        return 0;
    }
    Another possibility is to use pthread_exit ((void*) 0) instead of return (void*) 0.
    Succinct is verbose for terse

  5. #5
    Join Date
    Oct 2001
    Posts
    745
    I made a project with ur file.I produced the .o object.No problem.I commented out the sleep(), call as it was showing undeclared identifier.
    I got an *.exe under linux.
    Iam new to Linux.I dont know how to run the exe from konsole

  6. #6
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,020
    Sorry - I forgot: sleep is in unistd.h. If you add unistd.h to the bunch of includes, it should work with sleep in. Just type the program name with a ./ in front. For instance, if it is called a.out

    ./a.out

    Minor note: if you are eventually going to transfer this to Solaris, sleep might not work as both pthreads and sleep use SIGALRM. If you are staying with Linux, it is not a problem.
    Succinct is verbose for terse

  7. #7
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    This kind of indirectly answers your question. If you used a thread
    library that already supports multiple platforms, you have one
    client-code base. In addition, if you use one that's already out
    there, it'll probably have underwent a lot of testing.

    So, you should check out the boost threading stuff. I believe they
    implement all the thread stuf natively, but it's all abstracted so
    that you don't HAVE to know the details.

    It's just a suggestion.

    --Paul

  8. #8
    Join Date
    Oct 2001
    Posts
    745
    When Iam Debugging I have another problem ,Iam getting a Deadlock

    I have implemented the LinuxMutex class as follows:

    LinMutex::LinMutex()
    {
    pthread_mutex_init(&m_hMutex,(pthread_mutexattr_t*)NULL);

    }
    void LinMutex::AcquireLock()
    {
    pthread_mutex_lock(&m_hMutex);
    }

    void LinMutex::ReleaseLock()
    {
    pthread_mutex_unlock(&m_hMutex);
    }

    LinMutex::~LinMutex()
    {
    pthread_mutex_destroy(&m_hMutex);

    }

    Is it alright.I heard from a colleuge,that If a thread which already owns a Mutex,Try to access,it again there will be a deadlock under Linux.

    In windows,I have implemented the lock() as follows to remove the DeadLock as said in Msdn:
    // from Msdn
    The thread that owns a mutex can specify the same mutex in repeated wait function calls without blocking its execution. Typically, you would not wait repeatedly for the same mutex, but this mechanism prevents a thread from deadlocking itself while waiting for a mutex that it already owns. However, to release its ownership, the thread must call ReleaseMutex once for each time that the mutex satisfied a wait.

    void WinMutex::AcquireLock()
    {
    while (WaitForSingleObject(m_hMutex,1000L)!= WAIT_OBJECT_0);
    }


    What should I do to remove the Deadlock.What should be done in The AcquireLock() of LinMutex.
    Could u help me on this regard.....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured