Overview
Features
Download
Documentation
Community
Add-Ons & Services

Using Threadpool in for loop

Please post support and help requests here.

Using Threadpool in for loop

Postby GATTACA » 02 Jan 2013, 03:48

I have created this example program to teach myself how to use the ThreadPool and Runnable classes in POCO but I'm not having much luck.

Can someone tell me why this doesn't compile?
Thanks in advance for any and all help.
Code: Select all
#include <iostream>
#include <deque>
#include "Poco/Runnable.h"
#include "Poco/ThreadPool.h"

using namespace std;


class Task : public Poco::Runnable {
private:
   int _id;
public:
   Task() {};
   void setId(int x) { _id = x; }

   void run(){
      for (int i= 0; i < 100; i++){
         cout << i << ") Task " << _id << " is working" << std::endl;
      }
   }
};
   
int main(){

   Task *ptr = NULL;
   deque<Task> DT;
   deque<Task>::iterator iter;
   Poco::ThreadPool pool;
   
   for(int i = 0; i < 10; i++) {
      ptr = new Task();
      ptr->setId(i);
      
      DT.push_back(*ptr);
      delete(ptr);
      ptr = NULL;
   }
   
   for(iter = DT.begin(); iter != DT.end(); iter++) pool.start(*iter);

   pool.joinAll();   
   return 0;
}


Here is the compile error I get:
Code: Select all
$ g++ -O2 -Wall -static  -I./poco.lib/include -L./poco.lib/lib -lPocoFoundation learnPoco.cpp -o learnPoco.exe
/tmp/ccKMn21d.o: In function `main':
learnPoco.cpp:(.text+0xe3): undefined reference to `Poco::ThreadPool::ThreadPool(int, int, int, int)'
learnPoco.cpp:(.text+0x143): undefined reference to `Poco::Runnable::Runnable()'
learnPoco.cpp:(.text+0x231): undefined reference to `Poco::ThreadPool::start(Poco::Runnable&)'
learnPoco.cpp:(.text+0x25b): undefined reference to `Poco::ThreadPool::joinAll()'
learnPoco.cpp:(.text+0x263): undefined reference to `Poco::ThreadPool::~ThreadPool()'
learnPoco.cpp:(.text+0x389): undefined reference to `Poco::ThreadPool::~ThreadPool()'
/tmp/ccKMn21d.o: In function `Task::~Task()':
learnPoco.cpp:(.text._ZN4TaskD2Ev[_ZN4TaskD5Ev]+0x8): undefined reference to `Poco::Runnable::~Runnable()'
/tmp/ccKMn21d.o: In function `Task::~Task()':
learnPoco.cpp:(.text._ZN4TaskD0Ev[_ZN4TaskD5Ev]+0xc): undefined reference to `Poco::Runnable::~Runnable()'
/tmp/ccKMn21d.o:(.rodata._ZTI4Task[typeinfo for Task]+0x10): undefined reference to `typeinfo for Poco::Runnable'
collect2: ld returned 1 exit status



I'm sorry if this is a newbie question but I'm pulling my hair out trying to figure this one out.
The tutorials all suggest that I haven't made a mistake (or at least not one I can see).
I seem to have to define a constructor and destructor for both ThreadPool and Runnable??
GATTACA
 
Posts: 4
Joined: 01 Jan 2013, 16:06

Re: Using Threadpool in for loop

Postby alex » 02 Jan 2013, 16:22

There's nothing wrong with the compilation of your code. There are two problems with linking:

1) order in which you specify files on the command line
2) since you are building static, you'll have to link with pthread as well

Try this:
Code: Select all
$ g++ -O2 -Wall -static  -I./poco.lib/include -L./poco.lib/lib learnPoco.cpp -o learnPoco.exe -lPocoFoundation -lpthread
alex
 
Posts: 1113
Joined: 11 Jul 2006, 16:27
Location: United_States

[SOLVED] Re: Using Threadpool in for loop

Postby GATTACA » 02 Jan 2013, 20:16

Thanks alex.

The program works now!!!

I would never have thought that was the issue.
GATTACA
 
Posts: 4
Joined: 01 Jan 2013, 16:06


Return to Support

Who is online

Users browsing this forum: No registered users and 2 guests