此时我正在编写一个测试程序以获取基础,但是当我运行它时,它会将垃圾值输入我的结构中。
如果这有点不完整,我很抱歉,我一直在处理这个问题,在网上搜索了几个小时,我所做的一切似乎都是正确的,但是当我通过 pthread_create 函数传递它们时,我将垃圾插入到一些关键值中.
谢谢你的帮助!
这段代码给了我以下输出:
主函数运行!
gWorkerid 的初始集 = 0
工人 ID = 319534848
你好杜迪!
结束睡眠
gWorkerid 现在是垃圾值 = -946297088
我期待:
主函数运行!
gWorkerid 的初始集 = 0
工人ID = 0
你好杜迪!
结束睡眠
gWorkerid 现在是垃圾值 = 0
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <pthread.h>
#define MAX_CONN 3
#define MAX_LINE 1000
typedef struct worker_t
{
int id;
int connection;
pthread_t thread;
int used;
}WORKER_T;
struct sockaddr_in gServ_addr;
WORKER_T gWorker[MAX_CONN];
char sendBuff[1025];
// Thread function
void * worker_proc(void *arg)
{
WORKER_T *me = (WORKER_T*) arg;
printf("Howdy Doody!\n");
return NULL;
}
int main(int argc, char *argv[])
{
printf("main function running!\n");
pthread_t threadTest;
int i = 0;
gWorker[i].id = i;
printf("initial set of gWorkerid = %d\n", gWorker[i].id);
gWorker[i].connection = i;
gWorker[i].used = 1;
pthread_create(&gWorker[i], NULL, worker_proc, &gWorker[i]);
sleep(1);
printf("end sleep\n");
printf("gWorkerid is now trash value = %d\n", gWorker[i].id);
return 0;
}