Re: C vs. C++ in pthreads...

From: Gianni Mariani (gi2nospam_at_mariani.ws)
Date: 01/23/04


Date: 23 Jan 2004 02:05:31 EST

John Ratliff wrote:
> Are you sure it works?
>
> I've tested this code on Linux and Solaris. Two different machines,
> compilers, operating systems, and processors. Seems odd they would both
> yield the same bad results while you claim it works fine.
>
> Does it print the "getting up" line in the C++ version? I never see it
> do that.
>
> I'm not saying they don't compile. They just don't run properly.
>
> The lock thing shouldn't matter. Yes, it's a problem. But I noticed this
> after I posted it, and it shouldn't affect sleep and wakeup since it
> clearly reaches the pthread_cond_wait call. If you take that out, the
> program just runs continually...

Nope - I'm wrong - it does not work. (I was looking at the strace and it
looked ok - my bad).

The problem is that the ThreadTest class is being copy constructed and
the thread that is initialized in the real constructor is pointed to the
  original object.

static void *thread_run(void *arg) {
     cout << "starting thread_run..." << endl;

     ThreadTest obj = *(ThreadTest*)arg;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - this is your problem

     obj.report_for_work();

     cout << "ending thread_run..." << endl;

     return NULL;
}

Change the function to this ....

static void *thread_run(void *arg) {
     cout << "starting thread_run..." << endl;

     ThreadTest & obj = * static_cast<ThreadTest*>(arg);

     obj.report_for_work();

     cout << "ending thread_run..." << endl;

     return NULL;
}

Also, add a non default copy constructor ...

     ThreadTest( const ThreadTest & );

AND DON'T DEFINE IT. That way if anyone else does this - the program
won't link. You probably don't want an assignment operator either.
Also - the destructor MUST wait for the thread to exit before returning
otherwise all other kinds of hell happen.



Relevant Pages

  • Re: Embedded Programming vs OS Programming
    ... Now on the personal side, I love Operating Systems, Compilers etc. ... future I wan to create an OS. ... I'm sure experience of embedded stuff -- getting machines to work with ...
    (comp.programming)
  • Re: Article in Scientific American
    ... I.e., without an algorithm to ... proof-checker produces is not merely proof of the validity ... The point was only that using different operating systems or different ... compilers will tend to mitigate the possibility of compiler or operating ...
    (sci.math)
  • Re: C++ in embedded systems
    ... > generated by C+ compilers for various constructs. ... You should know how to write exception safe code before using exceptions in ... But so does every error handling strategy. ... They make it far easier to establish invarians in constructor and ensure ...
    (comp.os.linux.embedded)
  • Re: Global Variables : Where are they stored ?
    ... computers work, how operating systems typically work, and the ... So experiences with endianess and alignment are also ... We don't want fancy language features so that we can ... I am not sure if any of the compilers we use are ...
    (comp.lang.c)
  • Re: Another computer algebra system : smib
    ... a list of problems caused by failures of compilers, or programmers to abide by some consistent specification. ... Certainly sometimes a bug is discovered only after a program is ported to a new environment -- a bug that existed in the old environment too, but just had not been exercised. ... Yes, principally bugs in compilers, operating systems, and programmers' understanding of compilers, operating systems, parallelism, etc. ... In the mid 1980s there were a host of multiple-processor machines, typically motorola 68020s with a shared memory bus, but also processors from MIPS. ...
    (sci.math.symbolic)