2013年Visual Studio中的pthread支持

时间:2015-11-22 10:14:55

标签: c++ visual-studio-2013 pthreads

我从http://sourceforge.net/projects/pthreads4w/files/latest/download?source=typ_redirect下载了预构建库。

我把它解压缩到以下目录: C:\ Users \用户维纳\文件\并行线程\并行线程-w32-2-9-1释放

我设置了以下内容: 在项目中 - >右键单击 - >属性 - >配置属性 - > C / C ++ - >一般 - >其他包括目录 - > “C:\用户\维纳\文件\并行线程\并行线程-w32-2-9-1释放\预built.2 \包括”

在项目中 - >右键单击 - >属性 - >配置属性 - >链接器 - >一般 - >其他图书馆目录 - > “C:\用户\维纳\文件\并行线程\并行线程-w32-2-9-1释放\预built.2 \ lib中\ X86”

在项目中 - >右键单击 - >属性 - >配置属性 - >链接器 - >输入 - >附加依赖项 - > pthreadVC2.lib pthreadVCE2.lib pthreadVSE2.lib

在项目中 - >右键单击 - >属性 - >配置属性 - >链接器 - >所有选项 - >其他选项 - > -lpthread

这是我的代码:

#include "stdafx.h"

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

struct Node {
    int data;
    struct Node* next;
};

struct Node*head;
pthread_rwlock_t rwlock;
int numThreads;

int i, j;
float mInsert, mDelete, mMember;
int data[1000];
int test[10000];
long numberOfTotalOperations = 10000;
long numberOfInsertOperations;
long numberOfDeleteOperations;
long numberOfMemberOperations;

void *Process(void* rank);
int Member(int val);
int Insert(int val);
int Delete(int val);
long getCurrentTime(void);

int main(int argc, char* argv[]) {

    if (argc != 5) {
        printf("<member op fraction> <insert op fraction> <delete op fraction>");
        exit(0);
    }
    mMember = strtod(argv[1], NULL);
    mInsert = strtod(argv[2], NULL);
    mDelete = strtod(argv[3], NULL);
    numThreads = strtod(argv[4], NULL);

    numberOfInsertOperations = mInsert * numberOfTotalOperations;
    numberOfDeleteOperations = mDelete * numberOfTotalOperations;
    numberOfMemberOperations = mMember * numberOfTotalOperations;

    pthread_rwlock_init(&rwlock, NULL);

    //data set : create with non-repeated random number
    for (i = 0; i < 1000; i++) {
        while (1) {
            int temp = rand() % 65536;
            int found = 0;
            for (j = 0; j < i; j++) {
                if (data[j] == temp) { found = 1; break; }
            }
            if (found == 0) { data[i] = temp; break; }
        }
    }
    //test set
    for (i = 0; i < 10000; i++) {
        while (1) {
            int temp = rand() % 65536;
            int found = 0;
            for (j = 0; j < i; j++) {
                if (test[j] == temp) { found = 1; break; }
            }
            if (found == 0) { test[i] = temp; break; }
        }
    }

    //----------------------------------------- Insert,Delete,Member
    long thread;
    pthread_t* thread_handles;
    thread_handles = malloc(numThreads * sizeof(pthread_t));

    for (thread = 0; thread < numThreads; thread++)
        pthread_create(&thread_handles[thread], NULL, Process, (void*)thread);

    for (thread = 0; thread < numThreads; thread++)
        pthread_join(thread_handles[thread], NULL);


    //--------
    free(thread_handles);
    return 0;
}

void *Process(void* rank) {

    long my_rank = (long)rank;
    int i, offset = (numberOfTotalOperations * my_rank) / numThreads;
    int my_last_i = offset + (numberOfTotalOperations / numThreads);
    long insert_op = numberOfInsertOperations / numThreads;
    long delete_op = numberOfDeleteOperations / numThreads;
    long member_op = numberOfMemberOperations / numThreads;

    for (i = offset; i < my_last_i; i++) {
        if (i < offset + insert_op) {   //insert
            pthread_rwlock_wrlock(&rwlock);
            Insert(test[i]);
            pthread_rwlock_unlock(&rwlock);
        }
        else if (i < offset + insert_op + delete_op) {  //delete
            pthread_rwlock_wrlock(&rwlock);
            Delete(test[i]);
            pthread_rwlock_unlock(&rwlock);
        }
        else {
            pthread_rwlock_rdlock(&rwlock);
            Member(test[i]);
            pthread_rwlock_unlock(&rwlock);
        }
    }
    return NULL;
}

int Insert(int value)
{
    struct Node*curr_p = head;
    struct Node*pred_p = NULL;
    struct Node*temp_p;

    while (curr_p != NULL && curr_p->data < value)
    {
        pred_p = curr_p;
        curr_p = curr_p->next;
    }

    if (curr_p == NULL || curr_p->data > value)
    {
        temp_p = malloc(sizeof(struct Node));
        temp_p->data = value;
        temp_p->next = curr_p;

        if (pred_p == NULL) /** New first node */
        {
            head = temp_p;
        }
        else
        {
            pred_p->next = temp_p;
        }
        return 1;
    }
    else /* value already in list*/
    {
        return 0;
    }
}

int Member(int value)
{
    struct Node*curr_p = head;

    while (curr_p != NULL && curr_p->data < value)
    {
        curr_p = curr_p->next;
    }
    if (curr_p == NULL || curr_p->data > value)
    {
        return 0;
    }
    else
    {
        return 1;
    }
}

int Delete(int value)
{
    struct Node*curr_p = head;
    struct Node*pred_p = NULL;

    while (curr_p != NULL && curr_p->data < value)
    {
        pred_p = curr_p;
        curr_p = curr_p->next;
    }
    if (curr_p != NULL && curr_p->data == value)
    {
        if (pred_p == NULL) /** deleting first node in list */
        {
            head = curr_p->next;
            free(curr_p);
        }
        else
        {
            pred_p->next = curr_p->next;
            free(curr_p);
        }
        return 1;
    }
    else /* Value isn't in list */
    {
        return 0;
    }
}

我收到了以下错误,其中包含pthreads: 1.智能感知:类型“void *”的值不能分配给“pthread_t *”类型的实体 2. IntelliSense:“void *”类型的值不能分配给“Node *”类型的实体 3.无法从'void *'转换为'pthread_t *' 4.无法从'void *'转换为'Node *'

我的设置有什么问题?

2 个答案:

答案 0 :(得分:0)

  

在项目中 - &gt;右键单击 - &gt;属性 - &gt;配置属性 - &gt;链接器 - &gt;所有选项 - &gt;其他选项 - &gt; -lpthread

这是错的。你为什么这样做?

  

在项目中 - &gt;右键单击 - &gt;属性 - &gt;配置属性 - &gt;链接器 - &gt;输入 - &gt;附加依赖项 - &gt; pthreadVC2.lib pthreadVCE2.lib pthreadVSE2.lib

您应该只链接一个* .lib文件。自述文件说明:

In general:
    pthread[VG]{SE,CE,C}[c].dll
    pthread[VG]{SE,CE,C}[c].lib

where:
    [VG] indicates the compiler
    V   - MS VC, or
    G   - GNU C

    {SE,CE,C} indicates the exception handling scheme
    SE  - Structured EH, or
    CE  - C++ EH, or
    C   - no exceptions - uses setjmp/longjmp

    c   - DLL compatibility number indicating ABI and API
          compatibility with applications built against
          a snapshot with the same compatibility number.
          See 'Version numbering' below.

答案 1 :(得分:0)

所以,我只是试图编译你的代码并得到同样的错误。这是因为你应该从malloc返回显式转换void *指针。

//thread_handles = malloc(numThreads * sizeof(pthread_t));
thread_handles = (pthread_t*)malloc(numThreads * sizeof(pthread_t)); // right one

//temp_p = malloc(sizeof(struct Node));
temp_p = (struct Node*)malloc(sizeof(struct Node)); // right one