多线程传参,分为两种方式。一种是设置 ==全局变量==;另一种是通过int pthread_create((pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
函数的最后一项参数传参,通过pthread_create 参数传参要注意,其参数类型为(void *)。
设置全局变量时,应该注意加上同步机制,例如如p v操作或者锁机制。避免多线程访问同一资源时发生竞争。如果线程个数可以确定,线程间是同步进行的,则线程间的私有资源(线程间使用的buffer,变量)不需要加锁,这种情况一般是设置全局标志来使线程同步。
函数传参时,避免主程序因为等待线程而阻塞,使用
pthread_detach(pid)
函数使线程函数自运行。则该线程运行结束后会自动释放所有资源。使用函数传参时,注意以下几个易错点:
1. 传递的参数类型是个(void型)指针。
2. 参数传递中,不能使用局部变量int a
, 以&a
的方式传参。可采用
int a = 5;//局部变量
pthread_create(pid, NULL, thread_func, (void *)a);
........
void *thread_func(void *argc)
{
......
b = (int )argc;//实现局部变量的对线程函数的传参
......
}
函数传参例子
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
void *thread1_func(void *argc)
{
int ret;
int iSocket =(int )argc;
char heartSendBuf[14] = {'\0'};
while(1) {
ret = send(iSocket, heartSendBuf, 14, 0);
if (ret < 0){
perror("thread1_func:");
close(iSocket);
pthread_exit(NULL);
}
printf("the heart send to client ok!\n");
}
}
void *thread2_func(void *argc)
{
int ret;
int iSocket =(int )argc;
char heartRecvBuf[14] = {'1'};
sleep(4);
while(1) {
ret = recv(iSocket, heartRecvBuf, 14, 0);
if (ret < 0){
perror("thread2_func:");
// close(iSocket);
pthread_exit(NULL);
}
printf("the heart recv from client ok!\n");
}
}
#define SERVER_IP "127.0.0.1"
static unsigned short tmp_port = 8803;
int start_server(void)
{
int iSocket;
int nZero = 0;
struct timeval t = {20, 0};
struct sockaddr_in myserver_addr;
struct sockaddr_in client_addr;
socklen_t addrlen = sizeof(myserver_addr);
memset(&myserver_addr, 0, sizeof(myserver_addr));
myserver_addr.sin_family = AF_INET;
myserver_addr.sin_port = htons(tmp_port);
myserver_addr.sin_addr.s_addr = inet_addr("192.168.0.18");
memset(&client_addr, 0, sizeof(client_addr));
client_addr.sin_family = AF_INET;
client_addr.sin_port = htons(8802);
client_addr.sin_addr.s_addr = inet_addr("192.168.0.20");
if ((iSocket = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ){
perror("Create Socket failed: ");
exit(1);
}
if (bind(iSocket, (struct sockaddr *)&myserver_addr, sizeof(myserver_addr))== -1){
perror("bind error");
}else {
printf("bind ok\n");
}
if (connect(iSocket, (struct sockaddr *)&client_addr, addrlen) != 0){
perror("connect error");
return -1;
}else {
printf("connect ok\n");
}
if (setsockopt(iSocket, SOL_SOCKET, SO_SNDBUF, (char *)&nZero, sizeof(nZero)) < 0){
perror("thread_send:setsockopt heartSendBuf error");
return -1;
}
if (setsockopt(iSocket, SOL_SOCKET, SO_RCVTIMEO, &t, sizeof(t)) < 0){
perror("thread_recv:setsockopt error");
return -1;
}
return iSocket;
}
int main(int argc, char **argv)
{
int ret;
int iSocket;
pthread_t pid1,pid2;
//create client
iSocket = start_server();
if (iSocket < 0){
perror("create client server error");
return -1;
}
ret = pthread_create(&pid1, NULL, thread1_func, (void *)iSocket);
if (ret < 0){
perror("create thread_caller error");
return -1;
}else {
printf(" create heart send thread ok\n");
}
pthread_detach(pid1);
ret = pthread_create(&pid2, NULL, thread2_func, (void *)iSocket);
if (ret < 0){
perror("create thread_caller error");
return -1;
}else {
printf(" create heart send thread ok\n");
}
pthread_detach(pid2);
while(1);
}
总结
多线程传参,传参的值是(void *)
类型,属于地址传参。在传入多个值时应当使用结构体封装。当使局部变量时,注意提前释放局部变量资源,生命周期应该等待线程结束。