Create a C++ object as a thread with Pthread

Create a C++ object as a thread with Pthread

Post by Richard Wan » Sat, 22 May 1999 04:00:00



Hi,

Does anybody do pthread and C++ knows how to create a C++ object as a thread
with pthread?

 
 
 

Create a C++ object as a thread with Pthread

Post by Rx Vide » Sat, 22 May 1999 04:00:00


Hello,

I don't know what exactly are you trying to do. If you want to create an object
within a thread, then you just create it.
If you want to have an object which 'impersonates' a thread (has a function
which runs in a separate thread), then you have to do one of the two things:

Since class member functions (or pointers to them) differ from the regular
functions in that they have to contain a pointer to the actual object plus the
pointer to the member function, you cannot pass such a function to the
pthread_create.

You can declare one of the member functions as static, or you can declare an
external function (not a member, just a regular f.) and make it a friend of your
class. In both cases you have to pass the pointer to the actual object as an
argument to your function in pthread_call.

class theClass;

void* theFunction(void* param)
{
    theClass* ptr=static_cast<theClass*>(param);

    ptr->doSomething();
    ptr->var=0;

Quote:}

class theClass
{
    friend void* theFunction(void* param);

    int var;
    void doSomething();

Quote:};

HTH,

Martin


> Hi,

> Does anybody do pthread and C++ knows how to create a C++ object as a thread
> with pthread?


 
 
 

Create a C++ object as a thread with Pthread

Post by Juergen Hein » Sat, 22 May 1999 04:00:00



>Hi,

>Does anybody do pthread and C++ knows how to create a C++ object as a thread
>with pthread?

See comp.programming.threads ... this questions shows up often as does
the wrong answer of using a static member function to pass it to
pthread_create(), so read the group, it is very informative and helpful.

Cheers,
Juergen

--
\ Real name     : Jrgen Heinzl                 \       no flames      /

 
 
 

1. rpc and pthreads on IRIX 6.5 - extra threads being created?

Hi

I'm using rpc on IRIX 6.5, where I have written my own svc_run() loop
that runs in a separate thread to the main UI thread (I've included
the code below).
The problem is, even though I only create the one thread, and I have
confirmed that svc_getreqset() is always called from the same thread,
when I track the current thread in the dispatch function, it is not
always the same - that is, calling svc_getreqset() is somehow causing
the underlying rpc library to create an extra thread!  I can't find
anything in any documentation suggesting this should happen, but that
is without doubt what is occurring.
Any help about this would be extremely appreciated!

Thanks

Dylan Nicholson

/// Rpc thread code

void RpcThread(SVCXPRT* xprt)
{
        fd_set fds;
        FD_ZERO(&fds);
        FD_SET((unsigned)xprt->xp_sock, &fds);

        int msck = xprt->xp_sock;
        while (!terminating)
        {
                fd_set sfds = fds;
                switch (select(msck + 1, &sfds, 0, 0, 0))
                {
                case -1:
                        break;
                case 0:
                        continue;
                default:
                        if (FD_ISSET(xprt->xp_sock, &sfds))
                        {
                                int sck = accept(xprt->xp_sock, 0, 0);
                                svcfd_create(sck, 0, 0);
                                FD_SET((unsigned)sck, &fds);
                                if (sck > msck)
                                        msck = sck;
                        }
                        else
                                svc_getreqset(&sfds);
                }
        }
        svc_destroy(xprt);

2. AIX v5 ???

3. How to create objects in shared memory using C++?

4. localhost does not load in apache

5. creating new shared object from existing shared objects ??

6. Newbie with simple (!) CGI-Perl script problem

7. Forte 6.2 C++ object good, lib from object has problems for STL

8. Why can't my X program find my Xlibs?

9. does linux JDK 1.1.x use posix threads (pthreads) or green threads?

10. creating a thread in c++ in unix

11. Problems creating threads using Roguewave Threads.h++

12. linux threads server dies after 1024 threads created

13. For the AIX C++ gurus: creating a C interface to a C++ shard lib