Boost logo

Boost Users :

Subject: [Boost-users] How to pass a shared_ptr to a thread.
From: Jason Cipriani (jason.cipriani_at_[hidden])
Date: 2008-12-03 22:29:57


Hi, I'm new to boost. I'm using it for the shared_ptr implementation.
I have a usage question. On Windows, a thread function takes a void*
parameter. If I have a shared_ptr to an object, what's the correct way
to pass that to a new thread? E.g.:

DWORD WINAPI threadproc (LPVOID something) {
  // how to get a shared_ptr through "something"?
  ...
}

void create5threads () {
  ...
  shared_ptr p(new Object);
  // what should "something" be if i want to pass p to the new
threads, incrementing its use count?
  CreateThread(..., &threadproc, (LPVOID)something, ...);
  CreateThread(..., &threadproc, (LPVOID)something, ...);
  CreateThread(..., &threadproc, (LPVOID)something, ...);
  CreateThread(..., &threadproc, (LPVOID)something, ...);
  CreateThread(..., &threadproc, (LPVOID)something, ...);
}

Sorry if this is a stupid question, but the only thing I could think
of was "(LPVOID)new shared_ptr(p)", but that means each thread must
explicitly delete the new shared_ptr and that kind of defeats the
purpose!

Thanks,
Jason


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net