Solved

Converting a boost:thread::id  type to std long.

Posted on 2012-04-04
4
1,936 Views
Last Modified: 2012-04-11
I am creating a warpper for the boost::thread class. Many of our users do not like the boost's own types. So I have this function in my classs

long getId();

to give the users in std types.

The getId() simply get the id of the composed boost::thread object within my class. But the problem is boost::thread return their own type boost::thread::id when I call call the get_id() of the boost::thread . I want to oconvert this to long in my getId() functiion. So in essence...

how to convert

boost::thread::id to long?

-prain
0
Comment
Question by:prain
  • 2
  • 2
4 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 37808136
Don't do that, since it is not a thread ID. Depending on which platform your code is running, it is either a encapsulated 'pthread_t*' or a Windows HANDLE (see boost/thread/pthread/thread_data.hpp or boost/thread/win32/thread_data.hpp respectively).
0
 

Author Comment

by:prain
ID: 37812128
In out application we need some kind of an ID returned from the wrapper. therefore I tried doing a reinterpret_cast from boos::thread::id to long. It did not work.
0
 
LVL 86

Accepted Solution

by:
jkr earned 150 total points
ID: 37812194
That won't work since thread::id is a class and not a POD. There however is on operator for ostreams, so the following should work:

#include <sstream>

long thread_id_to_long(const boost::thread::id& tid) {

  long l;
  std::stringstream ss;

  ss << id;

  ss >> l;

  return l;
}

Open in new window

0
 

Author Closing Comment

by:prain
ID: 37833298
Thanks
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Best book to learn C++ 4 84
c++ reading data from file into two dimensional array 3 117
DCT of  2D array using fftw in c++ 9 69
troubleshoot a python script 8 25
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question