小编ski*_*ath的帖子

使用pthread_create()时将结构作为参数传递

我尝试将结构作为第4个参数传递,同时使用pthread_create()类似这样的东西:

pthread_create(&tid1, NULL, calca, &t); //t is the struct
Run Code Online (Sandbox Code Playgroud)

现在每当我尝试访问结构中的变量-ta,tb或tc时,我都会收到错误 - 请求成员不是结构或联合.

我可以使用什么替代方法将结构传递给线程?

c multithreading struct pthreads

17
推荐指数
1
解决办法
3万
查看次数

wait的意思((int*)0)

使用这样的等待函数的一个这样的程序是这样的:

#include<stdio.h> 
#include<stdlib.h> 
int main() 
{ 
    int pid,fd[2]; int n; char line[20];        
    if(pipe(fd)<0) { 
        printf("Error creating pipe"); 
    } else { 
        pid=fork(); 
        if(pid<0) { 
            printf("Error while forking"); 
        } else { 
            if(pid>0) { 
                close(fd[0]); 
                write(fd[1],"Hello\n",6); 
                while(wait((int *)0)!=pid);
            } else { 
                close(fd[1]); 
                n=read(fd[0],line,20); 
                if(n<0) 
                printf("Error reading a file"); 
                write(1,line,n); 
            } 
        } 
    } 
    return 0; 
}   
Run Code Online (Sandbox Code Playgroud)

c fork pipe

7
推荐指数
2
解决办法
4049
查看次数

标签 统计

c ×2

fork ×1

multithreading ×1

pipe ×1

pthreads ×1

struct ×1