Solved

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

Posted on 2012-04-04
4
1,766 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
countX 22 70
mapShare challenge 13 68
How to print into std::string like sprintf using C++11? 12 39
Adjust the position 3 13
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
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.

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now