Hi,

I am just trying the boost threads example from the link.

#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/tss.hpp>
#include <iostream>

boost::mutex io_mutex;
boost::thread_specific_ptr<int> ptr;

struct count
{
count(int id) : id(id) { }

void operator()()
{
if (ptr.get() == 0)
ptr.reset(new int(0));

for (int i = 0; i < 10; ++i)
{
(*ptr)++;
boost::mutex::scoped_lock
lock(io_mutex);
std::cout << id << ": "
<< *ptr << std::endl;
}
}

int id;
};

int main(int argc, char* argv[])
{
boost::thread thrd1(count(1));
boost::thread thrd2(count(2));
thrd1.join();
thrd2.join();
return 0;
}
// Output
$ ./testme
10591400: 1
10591400: 2
10591400: 3
10591400: 4
10591400: 5
10591400: 6
10591400: 7
10591400: 8
10591400: 9
10591400: 10
Segmentation fault

I have a few questions.
1)  Why is id copied incorrectly in operator()?
2)  Why does the count object get copied so many times?  (i tried to write a copy constructor into count to see the id value)
3)  I tried to add setstacksize in boost code and recompile.  Is it a better way to do it?

Thanks in advance.
Gabriel

----- Original Message ----
From: Moshe Matitya <Moshe.Matitya@Kayote.com>
To: boost-users@lists.boost.org
Sent: Tuesday, December 12, 2006 3:40:03 AM
Subject: Re: [Boost-users] HowTo Pass arguments to boost threads?

Forever Kid wrote:
>
> What is the method boost prefers to pass arguments to boost
> threads?  (such as a pointer to some class
> object)
>
> I saw the class thread_specific_ptr.  
> Is thread_specific_ptr designed for this purpose?
>
> Is there a recommended site with boost examples to check out?
>  Boost has good documentation, although I prefer to learn by
> example and use strict documentation as I become more experienced.
>
> Thanks much,
> Graham


Take a look this introductory article on Boost.Threads by Bill Kempf:
http://www.ddj.com/184401518
It addresses the question you're asking in a very clear, straightforward
manner, with examples.

Moshe
_______________________________________________
Boost-users mailing list
Boost-users@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users



Everyone is raving about the all-new Yahoo! Mail beta.