Linux Visual Studio 2010 中的 pthread(POSIX 线程)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8499074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:38:19  来源:igfitidea点击:

pthread ( POSIX Threads) in visual studio 2010

cwindowslinuxpthreadsposix

提问by user642564

I have found a Pthread program from internet and i want to run it in visual studio 2010 but I dont know how can use pthread in visual studio. the following is the program which I found:

我从互联网上找到了一个 Pthread 程序,我想在 Visual Studio 2010 中运行它,但我不知道如何在 Visual Studio 中使用 pthread。以下是我找到的程序:

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

#define MAX_SIZE 4096
#define NO_PROCESS 8

typedef double matrix[MAX_SIZE][MAX_SIZE];

int N;          /* matrix size      */
int maxnum;     /* max number of element*/
char *Init;     /* matrix init type */
int PRINT;      /* print switch     */
matrix A;       /* matrix A     */
double b[MAX_SIZE];    /* vector b */
double y[MAX_SIZE];    /* vector y */
pthread_barrier_t barrier;

/* forward declarations */
void work(void*);
void Init_Matrix(void);
void Print_Matrix(void);
void Init_Default(void);
int Read_Options(int, char **);

int main(int argc, char **argv)
{
    pthread_t threads[NO_PROCESS];
    int timestart, timeend, iter;
    long i;

    Init_Default();     /* Init default values  */
    Read_Options(argc, argv);    /* Read arguments   */
    Init_Matrix();      /* Init the matrix  */

    pthread_barrier_init(&barrier, NULL, NO_PROCESS);

    for (i = 0; i < NO_PROCESS; i++)
        pthread_create (&threads[i], NULL, (void *) &work, (void *) i);

    for (i = 0; i < NO_PROCESS; i++)
        pthread_join(threads[i], NULL);

    pthread_barrier_destroy(&barrier);

    if (PRINT == 1)
        Print_Matrix();
}

void work(void *pId)
{
    int i, j, k;
    long thread_id = (long)pId;

    /* Gaussian elimination algorithm */

    for (k = 0; k < N; k++)
    { /* Outer loop */

        if (thread_id == (k % NO_PROCESS))
        {
            for (j = k + 1;(j < N); j++)
                A[k][j] = A[k][j] / A[k][k]; /* Division step */

            y[k] = b[k] / A[k][k];

            A[k][k] = 1.0;
        }

        pthread_barrier_wait(&barrier); /* wait for other threads finishing this round */

        for (i = k + 1;(i < N); i++)
        {
            if (thread_id == (i % NO_PROCESS))
            {
                for (j = k + 1;(j < N); j++)
                    A[i][j] = A[i][j] - A[i][k] * A[k][j]; /* Elimination step */

                b[i] = b[i] - A[i][k] * y[k];

                A[i][k] = 0.0;
            }
        }

        pthread_barrier_wait(&barrier); /* wait for other threads finishing this round */

    }
}

void Init_Matrix()
{
    int i, j;

    printf("\nsize      = %dx%d ", N, N);
    printf("\nmaxnum    = %d \n", maxnum);
    printf("Init      = %s \n", Init);
    printf("Initializing matrix...");

    if (strcmp(Init, "rand") == 0)
    {
        for (i = 0; i < N; i++)
        {
            for (j = 0; j < N; j++)
            {
                if (i == j) /* diagonal dominance */
                    A[i][j] = (double)(rand() % maxnum) + 5.0;
                else
                    A[i][j] = (double)(rand() % maxnum) + 1.0;
            }
        }
    }

    if (strcmp(Init, "fast") == 0)
    {
        for (i = 0; i < N; i++)
        {
            for (j = 0; j < N; j++)
            {
                if (i == j) /* diagonal dominance */
                    A[i][j] = 5.0;
                else
                    A[i][j] = 2.0;
            }
        }
    }

    /* Initialize vectors b and y */
    for (i = 0; i < N; i++)
    {
        b[i] = 2.0;
        y[i] = 1.0;
    }

    printf("done \n\n");

    if (PRINT == 1)
        Print_Matrix();
}

void Print_Matrix()
{
    int i, j;

    printf("Matrix A:\n");

    for (i = 0; i < N; i++)
    {
        printf("[");

        for (j = 0; j < N; j++)
            printf(" %5.2f,", A[i][j]);

        printf("]\n");
    }

    printf("Vector b:\n[");

    for (j = 0; j < N; j++)
        printf(" %5.2f,", b[j]);

    printf("]\n");

    printf("Vector y:\n[");

    for (j = 0; j < N; j++)
        printf(" %5.2f,", y[j]);

    printf("]\n");

    printf("\n\n");
}

void Init_Default()
{
    N = 2048;
    Init = "rand";
    maxnum = 15.0;
    PRINT = 0;
}

int Read_Options(int argc, char **argv)
{
    char *prog;
    prog = *argv;

    while (++argv, --argc > 0)
        if (**argv == '-')
            switch ( *++*argv )
            {

                    case 'n':
                    --argc;
                    N = atoi(*++argv);
                    break;

                    case 'h':
                    printf("\nHELP: try sor -u \n\n");
                    exit(0);
                    break;

                    case 'u':
                    printf("\nUsage: sor [-n problemsize]\n");
                    printf("           [-D] show default values \n");
                    printf("           [-h] help \n");
                    printf("           [-I init_type] fast/rand \n");
                    printf("           [-m maxnum] max random no \n");
                    printf("           [-P print_switch] 0/1 \n");
                    exit(0);
                    break;

                    case 'D':
                    printf("\nDefault:  n         = %d ", N);
                    printf("\n          Init      = rand" );
                    printf("\n          maxnum    = 5 ");
                    printf("\n          P         = 0 \n\n");
                    exit(0);
                    break;

                    case 'I':
                    --argc;
                    Init = *++argv;
                    break;

                    case 'm':
                    --argc;
                    maxnum = atoi(*++argv);
                    break;

                    case 'P':
                    --argc;
                    PRINT = atoi(*++argv);
                    break;

                    default:
                    printf("%s: ignored option: -%s\n", prog, *argv);
                    printf("HELP: try %s -u \n\n", prog);
                    break;
            }
}

Can anybody tell me how to run it in visual studio. I know there should be included some header, but i dont know how to do it.

谁能告诉我如何在visual studio中运行它。我知道应该包含一些标题,但我不知道该怎么做。

Please let me know from start until end step by step. I am a beginner in programming please tell me step by step...

请让我从头到尾一步一步地知道。我是编程初学者请告诉我一步一步...

回答by Jeremiah Gowdy

http://sourceware.org/pthreads-win32/

http://sourceware.org/pthreads-win32/

The above link is for a pthreads partial implementation on Win32. It's a little old, but it should do the job.

上面的链接是针对 Win32 上的 pthreads 部分实现的。它有点旧,但它应该可以胜任。

回答by markpizz

Now that you've got your program compiled, you need to locate the specific dll (pthreadVC2.dll) in one of two places:

现在您已经编译了程序,您需要在以下两个位置之一找到特定的 dll (pthreadVC2.dll):

  • 1) The same directory your .exe file is located in
    OR
  • 2) C:\Windows\system32 (if your OS is a 32bit windows version)
    OR
  • 2) C:\Windows\SysWOW64 (if your OS is a 64bit windows version)
  • 1) 您的 .exe 文件位于
    OR 中的同一目录
  • 2) C:\Windows\system32(如果您的操作系统是 32 位 Windows 版本)
  • 2) C:\Windows\SysWOW64(如果你的操作系统是64位windows版本)

Really, the "C:\Windows" in the above examples should be the actual path your windows is installed to, sometimes this is spelled out as %windir%\system32 or %windir%\SysWOW64

实际上,上面示例中的“C:\Windows”应该是您的 Windows 安装到的实际路径,有时拼写为 %windir%\system32 或 %windir%\SysWOW64

回答by Elod

A short and easy description can be found here(for all the files: *.h, *.lib, *.dll).

可以在此处找到简短的描述 (对于所有文件:*.h、*.lib、*.dll)。