开发者

What happens when i copy pthread_rwlock_t?

pthread_rwlock t1;
pthread_rwlock_wrlock(&t1);

pthread_rwlock t2 = t1;

what happend? is t2 locked or n开发者_如何学编程ot?


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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜