This is the mail archive of the cygwin@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: pthread_create -- no callback?


Yeah, arg problem noted.  Oversight on my part, thanks.  However, the thread
callback still appears to be a problem.  Here's a more simplified example:


#include "pthread.h"
#include <iostream>

extern "C" void* callbackFunction (void*);

void* callbackFunction (void* arg) {
    cout << "--> callbackFunction()\n";
    return NULL;
}

int main (int numArgs, char** args) {

    pthread_t thread;

    pthread_attr_t pta;
    pthread_attr_init(&pta);

    cout << "--> pthread_create()\n";
    pthread_create(&thread, &pta, callbackFunction, NULL);
    cout << "<-- pthread_create()\n";
    
    void* threadExitStatus;
    cout << "--> pthread_join()\n";
    pthread_join(&thread, &threadExitStatus);
    cout << "<-- pthread_join():" << (int)threadExitStatus << "\n";
}




The result of this is as follows (again, compiled using g++ on cygwin 1.3.5):

--> pthread_create()
<-- pthread_create()
--> pthread_join()
<-- pthread_join():1


So, the newly created thread fails during it's execution (exit status 1), and
the callback doesn't ever seem to be invoked.  I've tried this both with and
without a pthread_attr_t argument to the pthread_create call.  It still fails,
but with an error code of 168 w/out the pthread_attr_t argument &
initialization code.  Any ideas?


thanks,
Evan

--- "Lassi A. Tuura" <lassi.tuura@cern.ch> wrote:
> > Is there something I'm doing wrong in the pthread_create call?
> 
> Yes, from a very superficial reading at least.
> 
> > void* execute(void* args) {
> 
> Here, you'll want non-zero `args'...
> 
> >     int status = pthread_create(&_thread, NULL, execute, NULL);
> 
> ... and here you are passing null (the last argument).  Since
> QueueProcessor::processTasks is not virtual, you are probably calling it
> with null 'this'.  Dunno why it doesn't die soon after that, but I
> suppose your while loop is failing there and hence the thread returns
> immediately, and thus joins.
> 
> BTW,
> >         pthread_mutex_lock(&_mutex);
> >         while (_queue->size() == 0 && !_stopped) {
> >             pthread_cond_wait(&_condition, &_mutex);
> >         }
> >         pthread_mutex_unlock(&_mutex);
> > 
> >         if (_queue->size()>0 && !_stopped) {
> >             t=_queue->front();
> >             _queue->pop_front();
> 
> ... is bad.  You'll want to keep the mutex until you've popped the task
> off the list.  Then unlock, then go into the following code section.
> 
> > Ideas?  Any good online pthreads references/faq's that you'd recommend?
> 
> I hear David Butenhof's "Programming with POSIX Threads" is good:
>   http://cseng.aw.com/book/0,,0201633922,00.html
> 
> More links at: http://www.humanfactor.com/pthreads/
> 
> //lat
> -- 
> Nothing more completely baffles one who is full of
> trick and duplicity, than straightforward and simple
> integrity in another.  --Charles Caleb Colton
> 
> --
> Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
> Bug reporting:         http://cygwin.com/bugs.html
> Documentation:         http://cygwin.com/docs.html
> FAQ:                   http://cygwin.com/faq/
> 


__________________________________________________
Do You Yahoo!?
Find the one for you at Yahoo! Personals
http://personals.yahoo.com

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]