2

我在同样的情况下被困了大约 2 天,我非常感谢任何帮助。主线程正在调用该initDevice()函数,该函数正在打开文件并创建一个新线程,他将成为该writeToDeviceHandler()函数的“写入”线程。从write2device()main() 调用,应该插入新任务以​​写入(将来)到map<int,Task*>. 问题是,有时应用程序会陷入某种无限循环或死锁,有时它会写入 <(# of tasks) 来写入。任何人都可以查看代码中是否有任何问题?谢谢!

int write2device(char *buffer, int length)
{
    if(is_running)
    {
        pthread_mutex_lock(&tasks_mutex);//LOCK
        int curr_id = getNextAvailableId();
        Task* new_task = new Task(buffer,length, curr_id);
        tasks[curr_id] = new_task;
        pthread_cond_signal(&tasks_cv);
        given_ids.insert(curr_id);
        pthread_mutex_unlock(&tasks_mutex);//UNLOCK
        return curr_id;
    }
    return FAIL;
}

int initdevice(char *filename)
{
    is_running = true;
    pthread_cond_signal(&tasks_cv);
    output_file.open(filename);
    if(!output_file.is_open())
    {
        cerr << "Error opening file" << endl;
        is_running = false;
        return SYSTEM_ERROR;
    }
    int res = pthread_create(&writing_thread, NULL, writeToDeviceHandler, NULL);//Create the writing to file thread.
    if(res != 0)
    {
        cerr <<  "Error creating the writing thread" <<endl;
        exit(FAIL);
    }

    return SUCCESS;
}

void *writeToDeviceHandler(void *arg)
{
    Task* curr_task;
    while(is_running)
    {
        pthread_mutex_lock(&tasks_mutex);
        cout << "IN LOOP - size of db: " << tasks.size() << endl;
        if(tasks.empty())
        {
            pthread_cond_wait(&tasks_cv, &tasks_mutex);
        }
        if(tasks.empty()) cout << "Empty, still finding thread" <<endl;
        curr_task = tasks.begin()->second;
        if(curr_task == NULL)
        {
            pthread_mutex_unlock(&tasks_mutex);
            continue;
        }
        //copy from tasks to file
        output_file.write(curr_task->getBuff(), curr_task->getLength());

        ids.remove(curr_task->getId());
        tasks.erase(curr_task->getId());
        delete curr_task;
        pthread_mutex_unlock(&tasks_mutex);
    }

    pthread_exit(NULL);
    return NULL;
}
4

2 回答 2

1

您的代码不正确,因为它没有围绕pthread_cond_wait调用的循环。呼叫可以在pthread_cond_wait虚假唤醒时返回。您必须在它返回后检查您的唤醒条件。在您的情况下,它看起来应该是这样的:

while (task.empty ())
  pthread_cond_wait(&tasks_cv, &tasks_mutex);

您的代码也缺少错误检查。请检查所有函数的所有返回值是否有错误。

于 2012-05-09T08:07:03.543 回答
0

您的writeToDeviceHandler代码在持有互斥锁的同时完成所有工作,完全违背了拥有线程的意义。如果另一个线程想给这个线程工作,它必须获取tasks_mutex. 为此,它必须完成,直到该线程完成写入并释放互斥锁。那么,为什么要打扰这个线程呢?

当你有一个互斥锁来保护需要完成的工作时,重点是在释放互斥锁的情况下实际完成工作。这样,其他线程就不必在您完成工作时等待。

于 2012-05-09T10:19:40.027 回答