473,473 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

how to cast?

i have a problem with threads and casts,
I created a thread
pthread_create(&grab_thread, NULL, grab_image, (void*)&camera);

and my function is like this:
void *grab_image(void *camera)
{..}

originally the type of camera is "struct Camera" but pthreads only wants
voids. now I need to cast the "void *camera" to "struct Camera *camera"
in the functions body. but how? or is the pthread_create wrong?
Jul 23 '05 #1
3 1820

void *grab_image(void *parm)
{
Camera *camera = static_cast<Camera *>(parm);
// use camera
pthread_exit(0); // or whatever return value you might want
}

pthread_create(&thread_id, NULL, grab_image, (void *)(&camera));

HTH
Srini
Andreas Müller wrote:
i have a problem with threads and casts,
I created a thread
pthread_create(&grab_thread, NULL, grab_image, (void*)&camera);

and my function is like this:
void *grab_image(void *camera)
{..}

originally the type of camera is "struct Camera" but pthreads only wants
voids. now I need to cast the "void *camera" to "struct Camera *camera"
in the functions body. but how? or is the pthread_create wrong?


Jul 23 '05 #2

"Andreas Müller" wrote:
i have a problem with threads and casts,
I created a thread
pthread_create(&grab_thread, NULL, grab_image, (void*)&camera);

and my function is like this:
void *grab_image(void *camera)
{..}

originally the type of camera is "struct Camera" but pthreads only wants
voids. now I need to cast the "void *camera" to "struct Camera *camera" in
the functions body. but how? or is the pthread_create wrong?


void * grab_image(void *arg) /* Note: renamed parameter)
{
Camera *my_camera = (Camera*)arg; /* Or appropriate C++-style cast */
}

Note, make sure you pass a pointer to an object of type Camera when you
create thread and make sure that pointer is still valid for the life-time of
the thread. And, btw, pthreads are not part of the C++ standard so next time
I suggest making an example program exhibiting your problem using standard
constructs only.

/ Eric
Jul 23 '05 #3
Andreas Müller wrote:
i have a problem with threads and casts,
I created a thread
pthread_create(&grab_thread, NULL, grab_image, (void*)&camera);

and my function is like this:
void *grab_image(void *camera)
{..}

originally the type of camera is "struct Camera" but pthreads only wants
voids. now I need to cast the "void *camera" to "struct Camera *camera"
in the functions body. but how? or is the pthread_create wrong?


You don't need the void* cast above.
To cast it back:
void* v_camera = ...;

Camera* camera = static_cast<Camera*>(v_camera);
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Aaron W. West | last post by:
Fun with CAST! (Optimized SQLServerCentral script posts) I found some interesting "tricks" to convert binary to hexadecimal and back, which allow doing 4 or 8 at a time. Test code first: --...
3
by: Mike | last post by:
I am using MS-Access as a front end for my MS-SQL DB. I have a sql view that uses the following: SELECT TOP 100 PERCENT RECID, PATNUMBER AS , SVCCODE AS , QTY, PROF_CHRGS AS , AMOUNT,...
4
by: Ray | last post by:
When a single-bit bitfield that was formed from an enum is promoted/cast into an integer, does ANSI C say anything about whether that integer should be signed or unsigned? SGI IRIX cc thinks it is...
17
by: Hazz | last post by:
In this sample code of ownerdraw drawmode, why does the '(ComboBox) sender' line of code need to be there in this event handler? Isn't cboFont passed via the managed heap, not the stack, into this...
3
by: mra | last post by:
I want to cast an object that I have created from a typename to the corresponding type. Can anycone tell me how to do this? Example: //Here, Create the object of type "MyClass" object...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
6
by: Lore Leunoeg | last post by:
Hello I derived a class MyControl from the Control class. Public Class MyControl Inherits Control Sub New() MyBase.New() End Sub End Class
9
by: Frederick Gotham | last post by:
Let's assume that we're working on the following system: CHAR_BIT == 8 sizeof( char* ) == 4 (i.e. 32-Bit) Furthermore, lets assume that the memory addresses are distributed as follows: ...
5
by: Frederick Gotham | last post by:
Before I begin, here's a list of assumptions for this particular example: (1) unsigned int has no padding bits, and therefore no invalid bit- patterns or trap representations. (2) All types have...
7
by: * Tong * | last post by:
Hi, I couldn't figure out how to properly type cast in this case: $ cat -n type_cast.c 1 #include <stdio.h> 2 3 typedef unsigned char Byte; 4 typedef signed char Small_Int; 5
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.