LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-07-2008, 11:26 AM   #1
Asuralm
LQ Newbie
 
Registered: Nov 2007
Posts: 26

Rep: Reputation: 15
Multithread in C++


Hi all:

I have a function

void FuncA(obj o, arg1 a1, arg2 a2);



Because I have to run the FuncA for many times for different objects, I am thinking it's probably better to run multithread so that each thread contains each object. I have just read a small tutorial in http://www.yolinux.com/TUTORIALS/Lin...ixThreads.html

However, I am still not sure how to start a thread if the arguments of FuncA is multiple.

Also, the pthread_t looks quite out-of-date and it's in C although it works in C++.

Could anyone give me any idea how to do this please?

Thanks
 
Old 11-07-2008, 12:28 PM   #2
jf.argentino
Member
 
Registered: Apr 2008
Location: Toulon (France)
Distribution: FEDORA CORE
Posts: 493

Rep: Reputation: 50
Quote:
However, I am still not sure how to start a thread if the arguments of FuncA is multiple.
You can't you have to design a structure to embed all the arguments you want into one. For example:
Code:
struct obj;
struct arg1;
struct arg2;

struct ThreadArgs {
   obj* o;
   arg1* a1;
   arg2* a2;
};

void* start_FuncA (void* arg) {
   struct ThreadArgs my_args = (struct ThreadArgs*) arg;
   FuncA (*my_args->o, *my_args->a1, *my_args->a2);
   return NULL;
}

int main (void) {
   struct obj o;
   struct arg1 a1;
   struct arg2 a2;
   struct ThreadArgs args;
   pthread_t thread;
   
   args.o = &o;
   args.a1 = &a1;
   args.a2 = &a2;
   pthread_create (&thread, NULL, start_FuncA, &args);
   /* ... */
   return 0;
}
Quote:
Also, the pthread_t looks quite out-of-date
pthread stand for Posix Thread, it's an interface definition to interact with a POSIX compliant system.... So the implementation you're using can be outdated if you're running an old system, but the interface isn't, or if it's the case it means that POSIX itself is outdated, and as far as I know, it's not the case...

Quote:
it's in C although it works in C++.
pthread implementation is wrotten in C, as all system relative tasks: read, write, ioctl, socket... because C _IS_ the UNIX native language... If you want something that is more "C++" look_and-feel, you have to find a wrapper, but if you take a look inside the wrapper code, you'll see call to the same C functions. But it isn't a silly question, if you want to use a wrapper, choose a good library, many are portable, that means that you will be able to compile the same code under POSIX (all UNICES) and non-POSIX (windows and ???), because the library replace the pthread_XXX functions with the one for non-POSIX system.
 
Old 11-07-2008, 01:48 PM   #3
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,126
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
I think c++ boost library has a threading class (that's probably a wrapper for pthread) http://www.boost.org and its available in most distributions directly just need to install it.
 
Old 11-08-2008, 10:55 PM   #4
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Define your objects to be the thread. Basically, you need to define your objects to be functor-objects; then you can use Boost or a variation of such (perhaps a creation of your own?).

For example, with Boost:
PHP Code:
#include <boost/thread/thread.hpp>
...

class 
Object1
{
  public:
    
// constructor
    
Object1(arg1 a1arg2 a2)
      : 
m_a1(a1),
        
m_a2(a2)
    {
    }

    
// thread process
    
void operator()(void)
    {
       
// do something with m_a1 and m_a2
    
}

  private:
    
arg1 m_a1;
    
arg2 m_a2;
};

// repeat for other Object classes (e.g. Object2, Object3, ..., ObjectN)

int main()
{
  
arg1 a1;
  
arg2 a2;

  
Object1 obj1(a1a2);
  
Object2 obj2(a1a2);
  ...
  
ObjectN objN(a1a2);

  
boost::thread_group threads;
  
threads.add_thread(new boost::thread(obj1));
  
threads.add_thread(new boost::thread(obj2));
  ...
  
threads.add_thread(new boost::thread(objN));

  
threads.join_all();

  return 
0;

Of course, in the example above, all Objects are using the same arg1 and arg2 objects. This probably will not be the case for your application.
 
Old 11-10-2008, 10:01 AM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940
Multithreading does not multiply the computer's time and capabilities, but rather, divides it.

My instincts tell me that, if your task is highly resource-intensive, it is very unlikely that multithreading will help matters. Finding a more-optimized algorithm is much more likely to bring success.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
why program is hang up with out using multithread boatcao Programming 1 05-12-2007 12:43 AM
gdbserver problem - multithread tashtego Programming 2 03-26-2007 07:27 AM
Multithread programming linuxmandrake Programming 3 04-07-2006 05:49 PM
multithread program gecoool Programming 7 01-31-2006 12:10 PM
how to debug multithread using GDB? ryanux Programming 1 05-11-2004 12:58 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration