views:

238

answers:

3

Hi

Could someone please help with this? I have the following:

// part_1
if (pthread_create(&threadID, NULL, ThreadMain, 
    (void *) clientSocket) != 0) {
    cerr << "Unable to create thread" << endl;
    exit(1);
}

// part_2
void *ThreadMain(void *clientSocket) {

  pthread_detach(pthread_self()); 

  ...

  delete (TCPSocket *) clientSocket;
  return NULL;
}

I would to have part_2 in part_1 ( I mean without calling TreadMain() function )

thanks for your replies

+4  A: 

What if you could do this?

pthread_create() has to create a new stack for the new thread to use. If pthread_create() copied the existing thread's old stack into a separate block of memory to create a new stack, then what would happen to local variables that point to other local variables? The local variables on the new stack would point to the old stack. When the thread using the old stack (the one that called pthread_create()) returns or overwrites them, those variables start pointing to invalid data.

But what about fork()? Why doesn't fork() take a function pointer like pthread_create()? Well, unlike pthread_create(), the new thread of execution created by fork() doesn't have to live in the same address space as the old thread of execution, because they are in separate processes. fork() clones the parent process's virtual address space, stack, heap, and all. The child process's stack variables have the same virtual addresses as the corresponding variables in the parent process. So all of the pointers in the new child process continue to be valid*, no matter what the old parent process does with the memory.

* Nitpicker's corner: excluding pointers that were invalid to begin with, as well as memory that is deliberately shared

bk1e
thanks for your reply. I already programmed thinks with pthreads and in addition fork() is supported on windows. what about declaring a function inside another , is this possible?thanks
make
Some C or C++ compilers might support nested functions as a compiler-specific extension. In C++, you can get something similar by defining a local class, but it can't access any variables from the enclosing function's scope (i.e. it's not a closure). Are lambda functions in C++0x more like what you're looking for?
bk1e
Can you please give me a link? thanks again for your help ...
make
http://en.wikipedia.org/wiki/Nested_functions http://en.wikipedia.org/wiki/C%2B%2B0x#Lambda_functions_and_expressions
bk1e
"Local" functions and lambdas don't do anything regular functions don't. They get called and return just the same. You're much better off working out threading semantics with more familiar syntax.
Potatoswatter
It's irrelevant anyway, since lambdas and functors in general aren't function pointers, and can't be passed as the parameter of pthread_create. You could use the Boost.Threads library instead.
Steve Jessop
@PotatoSwatter: I agree. Nested functions without closures are merely syntactic sugar, and for this use case, lambda functions would just make it easier to pass parameters to the thread.
bk1e
@Steve Jessop: True, you can't directly pass a C++ function object to a C function; you would have to write a proxy function that executes the function object for you, and Boost.Thread already solves that problem. (Also, C++0x is supposed to have threads and futures+promises in the standard library.)
bk1e
+1  A: 

If all you want to do is simply move the function for part2 inside part1, you can create a local class inside of part1, with a static member function...

class LocalFunctor
{
public:
   static void *ThreadFunc(void* clientSocket)
   {
      pthread_detach(pthread_self());
      ...
      delete (TCPSocket *) clientSocket;       
      return NULL;  
    }
};

then call LocalFunctor::THreadFunc within pthread_create

pthread_create(&threadID, NULL, LocalFunctor::THreadFunc,(void *) clientSocket)

If you're going to do this more than once, look at boost::thread or wrap this up inside a template helper class.

-Rick

Rick
A: 

You could get an Apple Macintosh computer with OS 10.6 and start programming with Grand Central Dispatch. Apple added some new C compiler features that do almost exactly what you seem to want. They're called Blocks.

Zan Lynx
Or not..........
0A0D