tags:

views:

42

answers:

2
pthread_rwlock t1;
pthread_rwlock_wrlock(&t1);

pthread_rwlock t2 = t1;

what happend? is t2 locked or not?

+3  A: 

Nothing special happens. pthread_rwlock_t (not pthread_rwlock, AFAIK) is an opaque C struct. Copying the variable simply copies the struct, byte for byte.

At the Pthreads level, copying a pthread_rwlock_t results in undefined behaviour. Don't do it.

Marcelo Cantos
sorry, i mean: aftert2 = t1, is t2 locked or not?
Raymond
i make a test, t2 is not locked, it seems different your answer: copy byte by byte.
Raymond
@Raymond: Add your first comment to your question, since it clarifies things quite a bit. What happens at the Pthreads level when you copy these data types is undefined; you should not copy these structs. But there is no question: the struct is copied byte by byte.
Marcelo Cantos
A: 

A new copy is created. The below example may clear things

#include<stdio.h>
typedef struct
{
char a[8];
}example;
int main()
{
example s1, s2;
strcpy(s1.a,"Hi");
s2=s1;
printf("%s %s \n",s1.a,s2.a);
strcpy(s2.a,"There");
printf("%s %s \n",s1.a,s2.a);
return 0;
}

This will output:

Hi Hi
Hi There
Praveen S