error C2678 and C2440

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

error C2678 and C2440

satish-13
Hi,
I am porting my multi threading(using pthread) Linux project to
windows. The compiler is MS VC++ 2005.
I am using pthread-win32, downloaded from
"http://sourceware.org/pthreads-win32/".
For version, here is a line from pthread.h
"#define PTW32_VERSION 2,7,0,0"

The project compilation is almost done but I am  getting few errors
related to pthread.

A.:- Have a look on following code,
----------------------------------------------------------------
#define MAXTHREADS 1000
typedef struct {
        pthread_t id;
        pthread_mutex_t lock;
        pthread_cond_t cond;
} THREADLIST;

THREADLIST threads[MAXTHREADS];

1281 int thread_find() {
1282 int thread = -1;
1283 pthread_t self = pthread_self();
1284 pthread_mutex_lock(&threadlistlock);
1285 for (int i = 0; i < MAXTHREADS; i++) {
1286 if (threads[i].flags != THREAD_UNUSED && threads[i].id ==
self) 1287 thread = i;
1288 break;
1289 }
1290 }
1291 pthread_mutex_unlock(&threadlistlock);
1292 return thread;
1293 }
----------------------------------------------------------------

The error, I am getting is,

1>main.cpp(1286) : error C2678: binary '==' : no operator found which
takes a left-hand operand of type 'pthread_t' (or there is no
acceptable conversion)
1> c:\program files\microsoft visual studio
8\vc\platformsdk\include\guiddef.h(192): could be 'int operator
==(const GUID &,const GUID &)'
1> while trying to match the argument list '(pthread_t, pthread_t)'


B.:- Another piece of code on which I am getting error is,
----------------------------------------------------------------
50 unsigned long MY_pthreads_thread_id(void)
51 {
52    int ret;
53 ret = pthread_self();
54 return (ret);
55 }
----------------------------------------------------------------
Here, it gives error like,

1>main.cpp(53) : error C2440: '=' : cannot convert from 'pthread_t' to 'int'

Can you please tell me the reason behind these errors?
I will be very thankful to you.

Regards
Satish
Reply | Threaded
Open this post in threaded view
|

RE: error C2678 and C2440

Burkhardt, Glenn
Look at the declaration for 'pthread_t' in pthread.h.  It's a structure,
not a pointer or an integer:

/*
 * Generic handle type - intended to extend uniqueness beyond
 * that available with a simple pointer. It should scale for either
 * IA-32 or IA-64.
 */
typedef struct {
    void * p;                   /* Pointer to actual object */
    unsigned int x;             /* Extra information - reuse count etc
*/
} ptw32_handle_t;

typedef ptw32_handle_t pthread_t;

I can't say I like this either.  All other implementations I've seen use
a pointer for pthread_t.  It must be a Windows thing.

> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of satish
> Sent: Friday, December 14, 2007 4:11 AM
> To: [hidden email]
> Subject: error C2678 and C2440
>
> Hi,
> I am porting my multi threading(using pthread) Linux project
> to windows. The compiler is MS VC++ 2005.
> I am using pthread-win32, downloaded from
> "http://sourceware.org/pthreads-win32/".
> For version, here is a line from pthread.h "#define
> PTW32_VERSION 2,7,0,0"
>
> The project compilation is almost done but I am  getting few
> errors related to pthread.
>
> A.:- Have a look on following code,
> ----------------------------------------------------------------
> #define MAXTHREADS 1000
> typedef struct {
> pthread_t id;
> pthread_mutex_t lock;
> pthread_cond_t cond;
> } THREADLIST;
>
> THREADLIST threads[MAXTHREADS];
>
> 1281 int thread_find() {
> 1282 int thread = -1;
> 1283 pthread_t self = pthread_self();
> 1284 pthread_mutex_lock(&threadlistlock);
> 1285 for (int i = 0; i < MAXTHREADS; i++) {
> 1286 if (threads[i].flags != THREAD_UNUSED &&
> threads[i].id ==
> self) 1287
> thread = i;
> 1288 break;
> 1289 }
> 1290 }
> 1291 pthread_mutex_unlock(&threadlistlock);
> 1292 return thread;
> 1293 }
> ----------------------------------------------------------------
>
> The error, I am getting is,
>
> 1>main.cpp(1286) : error C2678: binary '==' : no operator found which
> takes a left-hand operand of type 'pthread_t' (or there is no
> acceptable conversion)
> 1> c:\program files\microsoft visual studio
> 8\vc\platformsdk\include\guiddef.h(192): could be 'int
> operator ==(const GUID &,const GUID &)'
> 1> while trying to match the argument list '(pthread_t, pthread_t)'
>
>
> B.:- Another piece of code on which I am getting error is,
> ----------------------------------------------------------------
> 50 unsigned long MY_pthreads_thread_id(void)
> 51 {
> 52    int ret;
> 53 ret = pthread_self();
> 54 return (ret);
> 55 }
> ----------------------------------------------------------------
> Here, it gives error like,
>
> 1>main.cpp(53) : error C2440: '=' : cannot convert from
> 'pthread_t' to 'int'
>
> Can you please tell me the reason behind these errors?
> I will be very thankful to you.
>
> Regards
> Satish
>
Reply | Threaded
Open this post in threaded view
|

Re: error C2678 and C2440

Peter Slacik
In reply to this post by satish-13
Hi,

(shortened)

satish wrote:

> Hi,
> I am porting my multi threading(using pthread) Linux project to
> windows. The compiler is MS VC++ 2005.
>
> The project compilation is almost done but I am  getting few errors
> related to pthread.
>
> A.:- Have a look on following code,
> ----------------------------------------------------------------
> typedef struct {
> pthread_t id;
> } THREADLIST;
>
> THREADLIST threads[MAXTHREADS];
>
> 1283 pthread_t self = pthread_self();
> 1286 if (threads[i].flags != THREAD_UNUSED && threads[i].id == self) ----------------------------------------------------------------
>
> The error, I am getting is,
>
> 1>main.cpp(1286) : error C2678: binary '==' : no operator found which
> takes a left-hand operand of type 'pthread_t' (or there is no
> acceptable conversion)
> 1> c:\program files\microsoft visual studio
> 8\vc\platformsdk\include\guiddef.h(192): could be 'int operator
> ==(const GUID &,const GUID &)'
> 1> while trying to match the argument list '(pthread_t, pthread_t)'
>
>  
IIRC you've to use int pthread_equal(pthread_t thread1, pthread_t
thread2) for the comparison. Because pthread_t is defined as:

     typedef struct {
         void * p;                   /* Pointer to actual object */
         unsigned int x;             /* Extra information - reuse count etc */
     } ptw32_handle_t;
     
     typedef ptw32_handle_t pthread_t;


> B.:- Another piece of code on which I am getting error is,
> ----------------------------------------------------------------
> 52    int ret;
> 53 ret = pthread_self();
> ----------------------------------------------------------------
> Here, it gives error like,
>
> 1>main.cpp(53) : error C2440: '=' : cannot convert from 'pthread_t' to 'int'
>
>  
Because of pthread_self()'s return value definition:

    pthread_t pthread_self (void);

Already in your previous example, you've used

    1283 pthread_t self = pthread_self();
     

This is the way to go.

> Can you please tell me the reason behind these errors?
> I will be very thankful to you.
>
> Regards
> Satish
>  

With regards
Peter

PS: I'm resending this, my original email was not plain text, sorry.
Reply | Threaded
Open this post in threaded view
|

Re: error C2678 and C2440

Peter Slacik
In reply to this post by Burkhardt, Glenn
>From "The Open Group Base Specifications Issue 6, IEEE Std 1003.1, 2004
Edition",
http://www.opengroup.org/onlinepubs/009695399/functions/pthread_equal.html:

> *RATIONALE*
>
> Implementations may choose to define a thread ID as a structure. This
> allows additional flexibility and robustness over using an *int*. For
> example, a thread ID could include a sequence number that allows
> detection of "dangling IDs" (copies of a thread ID that has been
> detached). Since the C language does not support comparison on
> structure types, the pthread_equal() function is provided to compare
> thread IDs.
>
>

With regards
Peter

PS: I'm resending this again, my original emails were not plain text, sorry.

Burkhardt, Glenn wrote:

> Look at the declaration for 'pthread_t' in pthread.h.  It's a structure,
> not a pointer or an integer:
>
> /*
>  * Generic handle type - intended to extend uniqueness beyond
>  * that available with a simple pointer. It should scale for either
>  * IA-32 or IA-64.
>  */
> typedef struct {
>     void * p;                   /* Pointer to actual object */
>     unsigned int x;             /* Extra information - reuse count etc
> */
> } ptw32_handle_t;
>
> typedef ptw32_handle_t pthread_t;
>
> I can't say I like this either.  All other implementations I've seen use
> a pointer for pthread_t.  It must be a Windows thing.
>
>  
>> -----Original Message-----
>> From: [hidden email]
>> [mailto:[hidden email]] On Behalf Of satish
>> Sent: Friday, December 14, 2007 4:11 AM
>> To: [hidden email]
>> Subject: error C2678 and C2440
>>
>> Hi,
>> I am porting my multi threading(using pthread) Linux project
>> to windows. The compiler is MS VC++ 2005.
>> I am using pthread-win32, downloaded from
>> "http://sourceware.org/pthreads-win32/".
>> For version, here is a line from pthread.h "#define
>> PTW32_VERSION 2,7,0,0"
>>
>> The project compilation is almost done but I am  getting few
>> errors related to pthread.
>>
>> [...]
>>
>> Can you please tell me the reason behind these errors?
>> I will be very thankful to you.
>>
>> Regards
>> Satish
>>
>>    
Reply | Threaded
Open this post in threaded view
|

RE: error C2678 and C2440

Dennis Foreman
In reply to this post by Burkhardt, Glenn
Seems to me that, ever since people started ballyhooing the "beauties" of C,
because of its "typing", those same people have been finding ways to "get
around" that very same typing, by either "assuming" that something is an int
or by using casting. Let's not blame this structure on "windows". The
beautiful thing about typing is that it allows you to port an application to
ANY platform, without having to worry about the underlying real type.

And what is an "int"? Depends on the architecture (32 vs. 64 bits) and the
compiler that generates the code.

Regards,
DJ Foreman