请教一个线程参数传递的问题

时间:2020-11-30 18:54:30
#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 );
        ...
}

11 个解决方案

#1


其实我的目标是把条件变量和互斥体变量传递到线程函数中,但是总是编译不过。

我也尝试着用结构来传递多个参数过去,也是不行。

p3.c: In function `child_pthread_2':
p3.c:46: error: cannot convert to a pointer type
p3.c:46: warning: passing arg 1 of `pthread_mutex_lock' from incompatible pointer type
p3.c:51: error: incompatible type for argument 1 of `pthread_cond_signal'
p3.c:57: warning: passing arg 1 of `pthread_mutex_unlock' from incompatible pointer type

#2


请各位大虾指教一二,谢谢

#3


你把完整的程序贴上来吧, 要根据你的程序再写,好麻烦。

#4


引用 3 楼 pottichu 的回复:
你把完整的程序贴上来吧, 要根据你的程序再写,好麻烦。

好,不过上面的已经是基本骨架了,完整代码有很多垃圾的。

#5


#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/msg.h>

typedef union {
    pthread_mutex_t     pthread_mut;
    pthread_cond_t      pthread_cond;
} pthread_buffer_fd;

int create_msq()
{
    int id;

        id = msgget( (key_t) 0x9999,(IPC_CREAT|0666));
    if(id == -1)
    {
                printf(" connect message queue failed!\n");
                return(-1);
        }
        return id;
}

int recv_msg( int msg_id)
{
        int ret = 0;
        char buffer[8192];
        printf("msgrcv starting...\n");                                                                       
        ret = msgrcv(msg_id, buffer, 8192,0,0);                                                               
        printf("msgrcv ending...\n");                                                                         
        return ret;                                                                                           
}                                                                                                             
                                                                                                              
void * child_pthread_2( pthread_buffer_fd * m2 )                                                              
{                                                                                                             
        int ret = 0;                                                                                          
        pthread_t p_id_2;                                                                                     
        pid_t pid3 = 0;                                                                                       
                                                                                                              
        printf("pthread child 2 is created!\n");                                                              
        printf("read_msg  locking....\n");                                                                    
        //加锁                                                                                                
        pthread_mutex_lock((pthread_buffer_fd *)m2->pthread_mut);                                             
        ret = create_msq();                                                                                   
        recv_msg(ret);                                                                                        
                                                                                                              
        printf("条件变量翻转,信号发出 !\n");                                                                  
        ret = pthread_cond_signal(m2->pthread_cond);                                                          
        if ( ret == 0) printf("cond signal sending...\n");                                                    
        else printf("cond signal sending failed!\n");                                                         
                                                                                                              
        p_id_2 = pthread_self();                                                                              
        printf("pthread_id = %u\n", p_id_2);                                                                  
        pthread_mutex_unlock(m2);                                                                             
        printf("pthread child 2 will exit!\n");                                                               
        pthread_exit(NULL);                                                                                   
}                                                                                                             
                                                                                                              

#6


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_cond_t cond_timeout = PTHREAD_COND_INITIALIZER;                                               
        pthread_cond_t cond_read_msg = PTHREAD_COND_INITIALIZER;                                              
        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 )                            
        { printf(" init cond_read_msg error!\n"); return; }                                                   
        else                                                                                                  
        { printf("initial cond_read_msg successfully!\n"); }                                                  
                                                                                                              
        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);                       
        if (ret == 0)                                                                                         
        { printf("create child_child process successfully! p_id_2 = %u\n", pthread_self()); }                 
        else { printf("create child_child_process failed!\n"); }                                              
                                                                                                              
        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 );            
        if ( ret != 0) printf(" wait setting error!\n");                                                      
        else                                                                                                  
        { printf(" waiting timeout ok!\n");                                                                   
          if ( ret == ETIMEDOUT) printf(" timeout is detected!\n");                                           
        }                                                                                                     
                                                                                                              
        ret = pthread_join(p_id_2, NULL);                                                                     
                                                                                                              
        printf("pthread child 2 will exit! ret of thread2 = %d\n", ret);                                      
        pthread_exit(NULL);                                                                                   
}                                                                                                             
                                                                                                              
int main()                                                                                                    
{                                                                                                             
        int ret = 0;                                                                                          
        pthread_t p_id;                                                                                       
        pthread_attr_t p_attr;                                                                                
                                                                                                              
        ret = create_msq();                                                                                   
        if ( ret < 0)                                                                                         
        { printf("create msq failed!\n"); }                                                                   
        else                                                                                                  
        { printf("create msq successfully!\n"); }                                                             
                                                                                                              
        ret = pthread_create( &p_id, NULL, ( void * ) child_pthread_1, NULL );                                
        if (ret == 0)                                                                                         
        { printf("pthread created!\n"); }                                                                     
        else                                                                                                  
        { printf("pthread failed!\n"); }                                                                      
                                                                                                              
        ret = pthread_join(p_id, NULL);                                                                       
        if ( ret == 0)                                                                                        
        { printf("the thread is closed!\n"); }                                                                
                                                                                                              
}                                                                                                             
  

#7


1. 你应该弄清脆 联合体 和结构体的区别。
2. int pthread_mutex_lock(pthread_mutex_t *mutex); 需要用指针。
void * child_pthread_2( pthread_buffer_fd * m2 )    
{    
        int ret = 0;    
        pthread_t p_id_2;    
        pid_t pid3 = 0;    
    
        printf("pthread child 2 is created!\n");    
        printf("read_msg  locking....\n");    

        pthread_buffer_fd * _m2 = (pthread_buffer_fd * ) m2;
        pthread_mutex_t  *mut= &(_m2->pthread_mut);
//        pthread_mutex_lock(&((pthread_buffer_fd *)m2->pthread_mut));                                           
        pthread_mutex_lock(mut);
        ret = create_msq();    
        recv_msg(ret);    
    
        printf("条件变量翻转,信号发出 !\n");    
        //ret = pthread_cond_signal(m2->pthread_cond);                                                         
        if ( ret == 0) printf("cond signal sending...\n");    
        else printf("cond signal sending failed!\n");    
    
        p_id_2 = pthread_self();    
        printf("pthread_id = %u\n", p_id_2);    
        pthread_mutex_unlock(mut);    
        printf("pthread child 2 will exit!\n");    
        pthread_exit(NULL);    
}    

#8


void thread_fun(void *arg);
pthread_t thread;

int data[10];

pthread_create(&thread,NULL,thread_fun,(void *)data);

这样data就传给arg指针了,之后,你只需要拷贝arg指向的数据,参数就传递成功了啊

#9


引用 8 楼 hairetz 的回复:
void thread_fun(void *arg); 
pthread_t thread; 

int data[10]; 

pthread_create(&thread,NULL,thread_fun,(void *)data); 

这样data就传给arg指针了,之后,你只需要拷贝arg指向的数据,参数就传递成功了啊


我要把互斥体变量和条件变量传递给线程函数:
typedef union { 
    pthread_mutex_t    pthread_mut; 
    pthread_cond_t      pthread_cond; 
} pthread_buffer_fd; 

字符数组好像不行。

#10


程序写的好乱

#11


没有人知道?

#1


其实我的目标是把条件变量和互斥体变量传递到线程函数中,但是总是编译不过。

我也尝试着用结构来传递多个参数过去,也是不行。

p3.c: In function `child_pthread_2':
p3.c:46: error: cannot convert to a pointer type
p3.c:46: warning: passing arg 1 of `pthread_mutex_lock' from incompatible pointer type
p3.c:51: error: incompatible type for argument 1 of `pthread_cond_signal'
p3.c:57: warning: passing arg 1 of `pthread_mutex_unlock' from incompatible pointer type

#2


请各位大虾指教一二,谢谢

#3


你把完整的程序贴上来吧, 要根据你的程序再写,好麻烦。

#4


引用 3 楼 pottichu 的回复:
你把完整的程序贴上来吧, 要根据你的程序再写,好麻烦。

好,不过上面的已经是基本骨架了,完整代码有很多垃圾的。

#5


#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/msg.h>

typedef union {
    pthread_mutex_t     pthread_mut;
    pthread_cond_t      pthread_cond;
} pthread_buffer_fd;

int create_msq()
{
    int id;

        id = msgget( (key_t) 0x9999,(IPC_CREAT|0666));
    if(id == -1)
    {
                printf(" connect message queue failed!\n");
                return(-1);
        }
        return id;
}

int recv_msg( int msg_id)
{
        int ret = 0;
        char buffer[8192];
        printf("msgrcv starting...\n");                                                                       
        ret = msgrcv(msg_id, buffer, 8192,0,0);                                                               
        printf("msgrcv ending...\n");                                                                         
        return ret;                                                                                           
}                                                                                                             
                                                                                                              
void * child_pthread_2( pthread_buffer_fd * m2 )                                                              
{                                                                                                             
        int ret = 0;                                                                                          
        pthread_t p_id_2;                                                                                     
        pid_t pid3 = 0;                                                                                       
                                                                                                              
        printf("pthread child 2 is created!\n");                                                              
        printf("read_msg  locking....\n");                                                                    
        //加锁                                                                                                
        pthread_mutex_lock((pthread_buffer_fd *)m2->pthread_mut);                                             
        ret = create_msq();                                                                                   
        recv_msg(ret);                                                                                        
                                                                                                              
        printf("条件变量翻转,信号发出 !\n");                                                                  
        ret = pthread_cond_signal(m2->pthread_cond);                                                          
        if ( ret == 0) printf("cond signal sending...\n");                                                    
        else printf("cond signal sending failed!\n");                                                         
                                                                                                              
        p_id_2 = pthread_self();                                                                              
        printf("pthread_id = %u\n", p_id_2);                                                                  
        pthread_mutex_unlock(m2);                                                                             
        printf("pthread child 2 will exit!\n");                                                               
        pthread_exit(NULL);                                                                                   
}                                                                                                             
                                                                                                              

#6


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_cond_t cond_timeout = PTHREAD_COND_INITIALIZER;                                               
        pthread_cond_t cond_read_msg = PTHREAD_COND_INITIALIZER;                                              
        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 )                            
        { printf(" init cond_read_msg error!\n"); return; }                                                   
        else                                                                                                  
        { printf("initial cond_read_msg successfully!\n"); }                                                  
                                                                                                              
        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);                       
        if (ret == 0)                                                                                         
        { printf("create child_child process successfully! p_id_2 = %u\n", pthread_self()); }                 
        else { printf("create child_child_process failed!\n"); }                                              
                                                                                                              
        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 );            
        if ( ret != 0) printf(" wait setting error!\n");                                                      
        else                                                                                                  
        { printf(" waiting timeout ok!\n");                                                                   
          if ( ret == ETIMEDOUT) printf(" timeout is detected!\n");                                           
        }                                                                                                     
                                                                                                              
        ret = pthread_join(p_id_2, NULL);                                                                     
                                                                                                              
        printf("pthread child 2 will exit! ret of thread2 = %d\n", ret);                                      
        pthread_exit(NULL);                                                                                   
}                                                                                                             
                                                                                                              
int main()                                                                                                    
{                                                                                                             
        int ret = 0;                                                                                          
        pthread_t p_id;                                                                                       
        pthread_attr_t p_attr;                                                                                
                                                                                                              
        ret = create_msq();                                                                                   
        if ( ret < 0)                                                                                         
        { printf("create msq failed!\n"); }                                                                   
        else                                                                                                  
        { printf("create msq successfully!\n"); }                                                             
                                                                                                              
        ret = pthread_create( &p_id, NULL, ( void * ) child_pthread_1, NULL );                                
        if (ret == 0)                                                                                         
        { printf("pthread created!\n"); }                                                                     
        else                                                                                                  
        { printf("pthread failed!\n"); }                                                                      
                                                                                                              
        ret = pthread_join(p_id, NULL);                                                                       
        if ( ret == 0)                                                                                        
        { printf("the thread is closed!\n"); }                                                                
                                                                                                              
}                                                                                                             
  

#7


1. 你应该弄清脆 联合体 和结构体的区别。
2. int pthread_mutex_lock(pthread_mutex_t *mutex); 需要用指针。
void * child_pthread_2( pthread_buffer_fd * m2 )    
{    
        int ret = 0;    
        pthread_t p_id_2;    
        pid_t pid3 = 0;    
    
        printf("pthread child 2 is created!\n");    
        printf("read_msg  locking....\n");    

        pthread_buffer_fd * _m2 = (pthread_buffer_fd * ) m2;
        pthread_mutex_t  *mut= &(_m2->pthread_mut);
//        pthread_mutex_lock(&((pthread_buffer_fd *)m2->pthread_mut));                                           
        pthread_mutex_lock(mut);
        ret = create_msq();    
        recv_msg(ret);    
    
        printf("条件变量翻转,信号发出 !\n");    
        //ret = pthread_cond_signal(m2->pthread_cond);                                                         
        if ( ret == 0) printf("cond signal sending...\n");    
        else printf("cond signal sending failed!\n");    
    
        p_id_2 = pthread_self();    
        printf("pthread_id = %u\n", p_id_2);    
        pthread_mutex_unlock(mut);    
        printf("pthread child 2 will exit!\n");    
        pthread_exit(NULL);    
}    

#8


void thread_fun(void *arg);
pthread_t thread;

int data[10];

pthread_create(&thread,NULL,thread_fun,(void *)data);

这样data就传给arg指针了,之后,你只需要拷贝arg指向的数据,参数就传递成功了啊

#9


引用 8 楼 hairetz 的回复:
void thread_fun(void *arg); 
pthread_t thread; 

int data[10]; 

pthread_create(&thread,NULL,thread_fun,(void *)data); 

这样data就传给arg指针了,之后,你只需要拷贝arg指向的数据,参数就传递成功了啊


我要把互斥体变量和条件变量传递给线程函数:
typedef union { 
    pthread_mutex_t    pthread_mut; 
    pthread_cond_t      pthread_cond; 
} pthread_buffer_fd; 

字符数组好像不行。

#10


程序写的好乱

#11


没有人知道?