I want to create a concurrent server to work on multiple client request. So I created a thread function to handle multiple request . My problem is I have a hash table , it is loaded with contents of file initially whenever server start and I have socket descriptor, file descriptor too. So how can I pass to thread function .Is it required structure to store the arguments and pass to thread ?
我想创建一个并发服务器来处理多个客户端请求。所以我创建了一个线程函数来处理多个请求。我的问题是我有一个哈希表,每当服务器启动它时都会加载文件的内容,我也有套接字描述符,文件描述符。那么我怎样才能传递给线程函数。是否需要结构来存储参数并传递给线程?
my code is like this :
我的代码是这样的:
struct UserData
{
char *username;
char *password;
};
struct HashTable
{
int size;
struct UserData *table
};
int main()
{
struct HashTable *htable;
//socket sd to open socket in server
and a Fd file descriptor to write to file
// hash table loaded with contents and it is a structure
/*create thread using pthread*/
pthread_create(...,fun,..);
}
void * fun(void *arg)
{
.............
}
how I declare a structure for passing to thread function including the arguments like socket descriptor (sd) ,file descriptor(fd) and hash table pointer ? will I use mutex to lock when I writing to file (fd) ?
我如何声明一个传递给线程函数的结构,包括socket descriptor(sd),file descriptor(fd)和hash table pointer这样的参数?我写文件(fd)时会使用互斥锁吗?
1 个解决方案
#1
pthread_create()
takes a void *
as its final argument, which gets passed to your thread entry function, fun()
in your case. So you would just need to define a struct that contains all the fields you want to pass:
pthread_create()接受一个void *作为它的最后一个参数,它将被传递给你的线程入口函数fun()。所以你只需要定义一个包含你想要传递的所有字段的结构:
struct ThreadArg {
int sd; /* socket descriptor */
int fd; /* file descriptor */
struct HashTable *ht;
};
Then in your main()
you fill it in and pass it to pthread_create()
:
然后在main()中填写并传递给pthread_create():
...
struct ThreadArg *arg = malloc(sizeof(struct ThreadArg)); /* you should check for NULL */
arg->sd = sd;
arg->fd = fd;
arg->ht = htable;
pthread_create(..., fun, (void *)arg);
...
And in fun()
you cast it back:
在fun()中,你把它丢回来:
void *fun(void *arg) {
struct ThreadArg *thArg = (struct ThreadArg *)arg;
/* do whatever with thArg->sd, thArg->fd, etc. */
...
/* free the memory when done */
free(arg);
}
#1
pthread_create()
takes a void *
as its final argument, which gets passed to your thread entry function, fun()
in your case. So you would just need to define a struct that contains all the fields you want to pass:
pthread_create()接受一个void *作为它的最后一个参数,它将被传递给你的线程入口函数fun()。所以你只需要定义一个包含你想要传递的所有字段的结构:
struct ThreadArg {
int sd; /* socket descriptor */
int fd; /* file descriptor */
struct HashTable *ht;
};
Then in your main()
you fill it in and pass it to pthread_create()
:
然后在main()中填写并传递给pthread_create():
...
struct ThreadArg *arg = malloc(sizeof(struct ThreadArg)); /* you should check for NULL */
arg->sd = sd;
arg->fd = fd;
arg->ht = htable;
pthread_create(..., fun, (void *)arg);
...
And in fun()
you cast it back:
在fun()中,你把它丢回来:
void *fun(void *arg) {
struct ThreadArg *thArg = (struct ThreadArg *)arg;
/* do whatever with thArg->sd, thArg->fd, etc. */
...
/* free the memory when done */
free(arg);
}