我正在尝试为每个传入的请求创建一个线程。这是我正在制作的一个简短示例。问题是我可以将第一个传入请求写入列表,但是当我尝试发送第二个请求时,它会在需要向我的函数“request_loop”发送信号时停止工作。
所以它工作完美,直到那里。有谁知道如何解决这个问题?该程序不想比这更进一步。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include "tcpsocket.h"
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <pthread.h>
#define NUM_HANDLER_THREADS 3 /* number of threads used to service requests */
pthread_mutex_t request_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t got_request = PTHREAD_COND_INITIALIZER;
unsigned char buffer[BUFSIZE];
unsigned char collect_buffers[2000];
struct timespec timeout;
struct timeval tv;
int num_requests = 0;
int num;
int rc = 0;
struct request {
int number;
struct request* next;
};
struct request* requests = NULL;
struct request* last_request = NULL;
这是我添加请求的功能。当我想将第二个线程写入我的列表时,它停止工作......
void add_request(int request_num,pthread_mutex_t* p_mutex,pthread_cond_t* p_cond_var)
{
int rc;
struct request* a_request;
a_request = (struct request*)malloc(sizeof(struct request));
if (!a_request) { /* malloc failed?? */
fprintf(stderr, "add_request: out of memory\n");
exit(1);
}
a_request->number = request_num;
a_request->next = NULL;
rc = pthread_mutex_lock(p_mutex);
printf("NUMBER OF REQUESTS: %d\n",num_requests);
if (num_requests == 0) { /* special case - list is empty */
printf("list is empty\n");
requests = a_request;
last_request = a_request;
printf("REQUEST NUMBER:%d",a_request->number);
}else{
printf("created list\n");
last_request->next = a_request;
last_request = a_request;
}
num_requests++;
rc = pthread_mutex_unlock(p_mutex);
printf("send signal");
rc = pthread_cond_signal(p_cond_var);
printf("signal sended");
}
这是我想将数据写入列表的函数。之后,我使用以下命令删除请求:free(request);
struct request* get_request(pthread_mutex_t* p_mutex){
printf("NUMBER REQUESTS (get request): %d\n",num_requests);
int rc;
struct request* a_request;
rc = pthread_mutex_lock(p_mutex);
if (num_requests > 0) {
a_request = requests;
requests = a_request->next;
printf("REQUEST NUMBER:%d\n",a_request->number);
if (requests == NULL) { /* this was the last request on the list */
last_request = NULL;
printf("last request is NULL\n\n");
}
num_requests--; //remove REQUEST
}else { /* requests list is empty */
printf("list is empty\n");
a_request = NULL;
}
//remove request
free(a_request);
rc = pthread_mutex_unlock(p_mutex);
/* return the request to the caller. */
return a_request;
}
这是我处理请求直到不再有请求的循环
void* handle_requests_loop(void* data){
//rc = pthread_mutex_lock(&request_mutex);
printf("NUMBER REQUESTS (loop): %d\n",num_requests);
int rc;
struct request* a_request;
int thread_id = *((int*)data);
while(num_requests > 0){
printf("THREAD ID : %d\n",thread_id);
a_request = get_request(&request_mutex);
if(num_requests == 0){
printf("NO REQUESTS\n\n");
}else{
printf("THERE ARE REQUESTS\n\n");
}
}
while(num_requests == 0){
printf("wait");
rc = pthread_cond_wait(&got_request, &request_mutex);
}
}
我为此编写了一个生成 2 个线程的测试函数。第一个工作正常,另一个在需要向线程循环发送新信号时停止工作。
int main(int argc, char* argv[]){
printf("StartUp\n");
//---------------------------Create Threads--------------------------------
for (i=0; i<NUM_HANDLER_THREADS; i++) {
thr_id[i] = i;
pthread_create(&p_threads[i], NULL, handle_requests_loop,(void*)&thr_id[i]);
}
//--------------------------INTERRUPT--------------------------------------
int j;
for(j=1;j<3;j++){
printf("ADD REQUEST\n");
add_request(j, &request_mutex, &got_request);
delay.tv_sec = 5;
delay.tv_nsec = 10;
nanosleep(&delay,NULL);
}
return 0;
}
有谁知道如何解决这个问题?非常感谢!!