Multithreading with the boost C++ libraries

Hello,
I posted awhile back about multithreading and was directed to boost.org. What I've read has been good, and I've been trying to get it work for awhile now. I'm using VC++2008 Express (9.0) and haven't been able to link (under: Options > Projects and Solutions > VC++ Directories) the header or prebuilt libraries. Does anyone know why? I used the installer found here: http://www.boostpro.com/products/free . Any ideas?

enduser000
Personally, I'd download the boost libraries and the boost-jam (bjam) from their website and use that to compile the libraries.

Then you shouldn't have any issues linking them and using the headers.
Okay, I tried that earlier... but it took awhile and I didn't get too far. However, I renamed the obj library's to what VC++ thought they were and it works. Now I'm just trying to learn how to make threads, pass functions to them, inturrupt them, and stuff like that. If you have any tips, I'd appreciate them, but boost.org is helping me too. Thanks again,

enduser000
I found using thread groups to be the best approach;
1
2
3
4
5
6
7
    // Create Threads
    boost::thread_group threads;
    for (int i = 0; i < iNumberOfThreads; ++i)
        threads.create_thread(&CRuntimeController::initMCMCThread);

    // Wait for Threads to finish.
    threads.join_all();
Okay, that looks good but, how could I attach my function (void func()) to a single thread? If I use a groop like you did it's no problem:
1
2
boost::thread_group threads;
threads.create_thread(func);

But I still am at a loss as to where I would pass arguments to this function. I did some more reading but would like a second explination if you don't mind, as it's a bit confusing. Let me know what you think.

enduser
You can add a thread to a group by using the .add_thread() function. Then just create a thread.

e.g (From boost examples)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Copyright (C) 2001-2003
// William E. Kempf
//
//  Distributed under the Boost Software License, Version 1.0. (See accompanying 
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#include <boost/thread/thread.hpp>
#include <boost/thread/xtime.hpp>
#include <iostream>

struct thread_alarm
{
    thread_alarm(int secs) : m_secs(secs) { }
    void operator()()
    {
        boost::xtime xt;
        boost::xtime_get(&xt, boost::TIME_UTC);
        xt.sec += m_secs;

        boost::thread::sleep(xt);

        std::cout << "alarm sounded..." << std::endl;
    }

    int m_secs;
};

int main(int argc, char* argv[])
{
    int secs = 5;
    std::cout << "setting alarm for 5 seconds..." << std::endl;
    thread_alarm alarm(secs);
    boost::thread thrd(alarm);
    thrd.join();
}
You're right at the problem I'm having with a single thread (boost::thread mythr).

You declare a thread, but how do you pass a function to it? I've used something similar to this :
 
thread_group threads
and threads.create_thread(func1) to create a new thread. but it gives me an error if I try to do somthing like this:
1
2
boost::thread mythr;
mythr.create_thread(func1); // error, no create_thread() func for boost::thread 


I've tried using something like join() to append my function to the new thread but to no avail. I'm new to multithreading as I am to the boost libraries, so bear with me. Any ideas? Passing a single function to a single thread...

enduser
Last edited on
When I used it, I just passed the function to the constructor:

boost::thread MyThread(&MyFunction);
Lines 31 and 32 of the sample I posted.
Topic archived. No new replies allowed.