|
Boost Users : |
Subject: Re: [Boost-users] How to terminate a specific thread among other threads
From: Master (master.huricane_at_[hidden])
Date: 2012-04-15 09:17:56
none worked :(
neither the boost::this_thread::get_id() is useable in a switch statement ,
nor the suggested use of boost::thread* or boost::container::vector<thread>
.
before i give the faulty source code of mine , i need to answer what
exactly im after .
see some of my friends are trying to learn boost , specially to work with
its threading capabilities to use it in a project of theirs . they have no
idea what boost is , or how these stuff work , so its on me , because
unlike them i had a little experience with boost in a long past .
so one of them asked me a question on a scenario where there is a function
, which is reapeately creating new threads , and each thread execute a
function with a unique request message ,
in the aforementioned function ( dispatcher lets say ) , he wants to see if
he as a ability to distinguish between the coming threads , and altering
them at any given moment , be it terminating them , or simply getting its
Id for further management , again by management i mean , in other functions
( or objects methods ) the thread can be specified by its ID , for being
recognized for being surveyed , or again being terminated or moved .
and i also have another question , how do i send an argument to afunction
with is goingto be used as packaged_task? it seems , i can only make
packaged_task objects out of strucs! or classes! seems i cant even use
functions at all! is it right ? i would appreciate an explanation on this
matter too please .
and now here is the source :
i am on windows 7 sp1 - 32bit - Visual studio 2010 .
> #include <boost/signal.hpp>
> #include <iostream>
> #include <boost/thread.hpp>
> using namespace std;
>
> boost::thread* threadarray[3];
>
> int t(int x)
> {
> switch(boost::this_thread::get_id())
> {
> case threadarray[0].get_id():
> cout<<"thread 1";
> break;
> case threadarray[1].get_id():
> //DISPOSE THIS THREAD or CHANGE OWNERSHITP, OR GET ANOTHER JOB TO
> IT , OR CHECK IF IT WAS SUCCESSFUL OR NOT
> cout<<"thread 2";
> break;
> case threadarray[3].get_id():
> cout<<"thread 3";
> break;
> default:
> cout<<"default";
> }
> return 0;
> }
>
> int main()
> {
> boost::thread t1(t,5), t2(t,6),t3(t,7);
>
> threadarray[0] = t1.move();
> threadarray[1] = t2.move();
> threadarray[2] = t3.move();
>
> for(int i = 0; i <3;i++)
> {
> threadarray[i].join();
> }
> system("pause");
> }
and these are the errors:
> Error 1 error C2450: switch expression of type 'boost::thread::id'
> is illegal c:\users\master\documents\visual studio 2010\projects\boost
> example\boost example\mb.cpp 61 1 Boost Example
> Error 2 error C2228: left of '.get_id' must have
> class/struct/union c:\users\master\documents\visual studio
> 2010\projects\boost example\boost example\mb.cpp 63 1 Boost Example
> Error 3 error C2046: illegal case
> c:\users\master\documents\visual studio 2010\projects\boost example\boost
> example\mb.cpp 63 1 Boost Example
> Error 4 error C2228: left of '.get_id' must have
> class/struct/union c:\users\master\documents\visual studio
> 2010\projects\boost example\boost example\mb.cpp 66 1 Boost Example
> Error 5 error C2046: illegal case
> c:\users\master\documents\visual studio 2010\projects\boost example\boost
> example\mb.cpp 66 1 Boost Example
> Error 6 error C2228: left of '.get_id' must have
> class/struct/union c:\users\master\documents\visual studio
> 2010\projects\boost example\boost example\mb.cpp 70 1 Boost Example
> Error 7 error C2046: illegal case
> c:\users\master\documents\visual studio 2010\projects\boost example\boost
> example\mb.cpp 70 1 Boost Example
> Warning 8 warning C4065: switch statement contains 'default' but no
> 'case' labels c:\users\master\documents\visual studio
> 2010\projects\boost example\boost example\mb.cpp 75 1 Boost Example
> Error 9 error C2440: '=' : cannot convert from 'boost::thread' to
> 'boost::thread *' c:\users\master\documents\visual studio
> 2010\projects\boost example\boost example\mb.cpp 83 1 Boost Example
> Error 10 error C2440: '=' : cannot convert from 'boost::thread' to
> 'boost::thread *' c:\users\master\documents\visual studio
> 2010\projects\boost example\boost example\mb.cpp 84 1 Boost Example
> Error 11 error C2440: '=' : cannot convert from 'boost::thread' to
> 'boost::thread *' c:\users\master\documents\visual studio
> 2010\projects\boost example\boost example\mb.cpp 85 1 Boost Example
> Error 12 error C2228: left of '.join' must have class/struct/union
> c:\users\master\documents\visual studio 2010\projects\boost example\boost
> example\mb.cpp 89 1 Boost Example
> 13 IntelliSense: expression must have integral or enum type
> c:\users\master\documents\visual studio 2010\projects\boost example\boost
> example\mb.cpp 11 9 Boost Example
> 14 IntelliSense: expression must have class type
> c:\users\master\documents\visual studio 2010\projects\boost example\boost
> example\mb.cpp 13 7 Boost Example
> 15 IntelliSense: expression must have class type
> c:\users\master\documents\visual studio 2010\projects\boost example\boost
> example\mb.cpp 16 8 Boost Example
> 16 IntelliSense: expression must have class type
> c:\users\master\documents\visual studio 2010\projects\boost example\boost
> example\mb.cpp 20 8 Boost Example
> 17 IntelliSense: no suitable conversion function from
> "boost::thread" to "boost::thread *" exists
> c:\users\master\documents\visual studio 2010\projects\boost example\boost
> example\mb.cpp 33 19 Boost Example
> 18 IntelliSense: no suitable conversion function from
> "boost::thread" to "boost::thread *" exists
> c:\users\master\documents\visual studio 2010\projects\boost example\boost
> example\mb.cpp 34 19 Boost Example
> 19 IntelliSense: no suitable conversion function from
> "boost::thread" to "boost::thread *" exists
> c:\users\master\documents\visual studio 2010\projects\boost example\boost
> example\mb.cpp 35 19 Boost Example
> 20 IntelliSense: expression must have class type
> c:\users\master\documents\visual studio 2010\projects\boost example\boost
> example\mb.cpp 39 3 Boost Example
>
>
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