2015-11-22 140 views
0

我從http://sourceforge.net/projects/pthreads4w/files/latest/download?source=typ_redirect下載了預建庫。pthread支持visual studio 2013

我把它提取到以下目錄: C:\用戶\維納\文檔\並行線程\並行線程,w32-2-9-1釋放

我設置以下的事情: 在項目 - >右鍵單擊 - >屬性 - >配置屬性 - > C/C++ - >常規 - >其他包含目錄 - >「C:\ Users \ Veena \ Documents \ pthread \ pthreads-w32-2-9-1-release \ Pre-built .2 \ include「

在項目 - >右鍵單擊 - >屬性 - >配置屬性 - >鏈接器 - >常規 - >其他庫目錄 - >」C:\ Users \ Veena \ Documents \ pthread \ pthreads-w32 -2-9-1-release \ Pre-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; 
    } 
} 

我˚F其中包含以下錯誤,不包括pthreads: 1.智能感知:不能將類型爲「void *」的值分配給類型爲「pthread_t *」的實體 2.智能感知:類型爲「void *」的值不能爲分配給類型「節點*」的實體 3.不能從「無效*」到「的pthread_t *」 4.無法從「無效*」轉換爲「節點*」

這有什麼錯我的設置轉換?

+0

寧願在這種情況下使用MinGw GCC比視覺工作室。 –

回答

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 
0

在項目 - >右鍵 - >屬性 - >配置屬性 - >連接 - >所有選項 - >附加選項 - > -lpthread

這是錯誤的。你爲什麼要這麼做?

在項目 - >右鍵 - >屬性 - >配置屬性 - >連接 - >輸入 - >附加依賴 - > 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. 
+0

當我用C++編寫代碼時,我使用pthreadVCE2.lib,對於C,您應該使用pthreadVC2.lib或pthreadVCE2.lib。 –

+0

我刪除了-lpthread並僅添加了pthreadVCE2.lib。仍然我收到了同樣的錯誤 – Veena