我想用互斥锁完成我的线程。第一个线程将不会执行,线程2& 3执行。
有谁知道这个问题可能是什么?有时执行线程1但不执行2或3。我不知道这里有什么问题。
Thread created successfully
Thread created successfully
Thread created successfully
----------------------------------------------------
J:0
NUM_REQUESTS (before function): 0
J:0
----------------------------------------------------
----------------------------------------------------
J:1
Third thread processing done
WRITE DATA TO LIST!
NUM_REQUESTS(function): 1
NUM_REQUESTS (before function): 0
J:1
----------------------------------------------------
----------------------------------------------------
J:2
Second thread processing done
WRITE DATA TO LIST!
NUM_REQUESTS(function): 0
NUM_REQUESTS (before function): 0
J:2
----------------------------------------------------
程序:
#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
pthread_mutex_t request_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t got_request = PTHREAD_COND_INITIALIZER;
pthread_t tid[3];
int thr_id[3];
int ret1,ret2,ret3;
int i = 0;
int err;
int *ptr[2];
int num_requests = 0;
int rc = 0;
这是线程的功能,第一个线程没有被执行!
void* doSomeThing(void *arg)
{
unsigned long i = 0;
pthread_t id = pthread_self();
for(i=0; i<1000000;i++);
if(pthread_equal(id,tid[0]))
{
printf("First thread processing done\n");
printf("WRITE DATA TO LIST!\n");
num_requests--;
printf("NUM_REQUESTS(function): %d\n",num_requests);
rc = pthread_mutex_unlock(&request_mutex);
pthread_exit(&tid[0]);
}else if(pthread_equal(id,tid[1])){
printf("Second thread processing done\n");
num_requests--;
printf("WRITE DATA TO LIST!\n");
printf("NUM_REQUESTS(function): %d\n",num_requests);
rc = pthread_mutex_unlock(&request_mutex);
pthread_exit(&tid[1]);
}else if(pthread_equal(id,tid[2])){
printf("Third thread processing done\n");
printf("WRITE DATA TO LIST!\n");
printf("NUM_REQUESTS(function): %d\n",num_requests);
num_requests--;
rc = pthread_mutex_unlock(&request_mutex);
pthread_exit(&tid[2]);
}
return NULL;
}
这是我创建线程输出的地方
void add_request(int j,pthread_mutex_t* p_mutex,pthread_cond_t* p_cond_var)
{
printf("----------------------------------------------------\n");
printf("J:%d\n",j);
if(num_requests > 3){
printf("WAIT TILL THREADS ARE FREE!\n");
}else{
rc = pthread_mutex_lock(&request_mutex);
printf("NUM_REQUESTS (before function): %d\n",num_requests);
num_requests++;
rc = pthread_mutex_unlock(&request_mutex);
rc = pthread_mutex_lock(&request_mutex);
rc = pthread_cond_signal(p_cond_var);
printf("J:%d\n",j);
printf("----------------------------------------------------\n");
}
}
在main中我只创建线程并使用add_request函数来执行线程
int main(void)
{
//create 3 threads
while(i < 3)
{
thr_id[i] = i;
err = pthread_create(&(tid[i]), NULL, &doSomeThing, (void*)&thr_id[i]);
if (err != 0)
printf("can't create thread :[%s]", strerror(err));
else
printf("Thread created successfully\n");
i++;
}
int j;
for(j=0;j<3;j++){
add_request(j, &request_mutex, &got_request);
}
return 0;
}
答案 0 :(得分:1)
至少有一个问题我认为你解锁互斥锁而不锁定它......
当你创建一个线程时,它会自发地开始(或多或少),所以不能假设在doSomething()之后调用add_request。
您可以锁定然后解锁。如果你想等到线程结束,也可以考虑使用ptheard_join
。
修改强> 这就是你想要做的事https://computing.llnl.gov/tutorials/pthreads/samples/condvar.c - 来自https://computing.llnl.gov/tutorials/pthreads/
祝你好运