当前位置: 技术问答>linux和unix
请教一个线程参数传递的问题
来源: 互联网 发布时间:2016-06-11
本文导语: #include ... typedef union { pthread_mutex_t pthread_mut; pthread_cond_t pthread_cond; } pthread_buffer_fd; ... void * child_pthread_2( pthread_buffer_fd * m2 ) { pthread_mutex_lock((pthread_buffer_fd *)m2->pthread_mut)...
#include
...
typedef union {
pthread_mutex_t pthread_mut;
pthread_cond_t pthread_cond;
} pthread_buffer_fd;
...
void * child_pthread_2( pthread_buffer_fd * m2 )
{
pthread_mutex_lock((pthread_buffer_fd *)m2->pthread_mut);
这一行报错:error: cannot convert to a pointer type warning: passing arg 1 of `pthread_mutex_lock' from incompatible pointer type
....
ret = pthread_cond_signal(m2->pthread_cond);
这一行报错:error: incompatible type for argument 1 of `pthread_cond_signal'
....
pthread_mutex_unlock(m2->pthread_mut);
这一样报错:warning: passing arg 1 of `pthread_mutex_unlock' from incompatible pointer type
....
pthread_exit(NULL);
}
void * child_pthread_1( void )
{
pthread_t p_id_3;
pthread_t p_id_2;
pid_t pid1 = 0;
int ret = 0;
pthread_buffer_fd ctl_ptr[1 + 1];
struct timeval now;
struct timespec timeout;
pthread_mutex_t mut_read_msg;
pthread_mutex_t mut_timeout;
pthread_condattr_t cond_read_msg_attr;
pthread_condattr_t cond_timeout_attr;
...
if ( (ret = pthread_cond_init(&cond_read_msg, &cond_read_msg_attr)) != 0 )
...
ctl_ptr[0].pthread_mut = mut_read_msg;
ctl_ptr[0].pthread_cond = cond_read_msg;
...
ret = pthread_create( &p_id_2, NULL, ( void * ) child_pthread_2, &mut_timeout);
...
gettimeofday(&now);
timeout.tv_sec = now.tv_sec + 5;
timeout.tv_nsec = now.tv_usec * 1000;
ret = pthread_cond_timewait(&ctl_ptr[0].pthread_cond, &ctl_ptr[0].pthread_mut, &timeout );
...
}
...
typedef union {
pthread_mutex_t pthread_mut;
pthread_cond_t pthread_cond;
} pthread_buffer_fd;
...
void * child_pthread_2( pthread_buffer_fd * m2 )
{
pthread_mutex_lock((pthread_buffer_fd *)m2->pthread_mut);
这一行报错:error: cannot convert to a pointer type warning: passing arg 1 of `pthread_mutex_lock' from incompatible pointer type
....
ret = pthread_cond_signal(m2->pthread_cond);
这一行报错:error: incompatible type for argument 1 of `pthread_cond_signal'
....
pthread_mutex_unlock(m2->pthread_mut);
这一样报错:warning: passing arg 1 of `pthread_mutex_unlock' from incompatible pointer type
....
pthread_exit(NULL);
}
void * child_pthread_1( void )
{
pthread_t p_id_3;
pthread_t p_id_2;
pid_t pid1 = 0;
int ret = 0;
pthread_buffer_fd ctl_ptr[1 + 1];
struct timeval now;
struct timespec timeout;
pthread_mutex_t mut_read_msg;
pthread_mutex_t mut_timeout;
pthread_condattr_t cond_read_msg_attr;
pthread_condattr_t cond_timeout_attr;
...
if ( (ret = pthread_cond_init(&cond_read_msg, &cond_read_msg_attr)) != 0 )
...
ctl_ptr[0].pthread_mut = mut_read_msg;
ctl_ptr[0].pthread_cond = cond_read_msg;
...
ret = pthread_create( &p_id_2, NULL, ( void * ) child_pthread_2, &mut_timeout);
...
gettimeofday(&now);
timeout.tv_sec = now.tv_sec + 5;
timeout.tv_nsec = now.tv_usec * 1000;
ret = pthread_cond_timewait(&ctl_ptr[0].pthread_cond, &ctl_ptr[0].pthread_mut, &timeout );
...
}
|
void thread_fun(void *arg);
pthread_t thread;
int data[10];
pthread_create(&thread,NULL,thread_fun,(void *)data);
这样data就传给arg指针了,之后,你只需要拷贝arg指向的数据,参数就传递成功了啊
pthread_t thread;
int data[10];
pthread_create(&thread,NULL,thread_fun,(void *)data);
这样data就传给arg指针了,之后,你只需要拷贝arg指向的数据,参数就传递成功了啊