Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macos pthread执行的奇怪结果_Macos_Pthreads - Fatal编程技术网

Macos pthread执行的奇怪结果

Macos pthread执行的奇怪结果,macos,pthreads,Macos,Pthreads,我有这段代码来测试MacOSX中的pthread #include <stdio.h> #include <string.h> #include <pthread.h> // A thread that just exits void* mythread(void *arg) { int tArg; tArg = *(int*)arg; printf("Received %d\n", tArg); return 0

我有这段代码来测试MacOSX中的pthread

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

//    A thread that just exits
void*  mythread(void *arg) { 
    int tArg;

    tArg = *(int*)arg;
    printf("Received %d\n", tArg);
    return 0; 
}

int main (int argc, char *argv[]) {
    int err, count = 0;

    pthread_t thread;

    while (1) {
        err = pthread_create(&thread, 0, mythread, &count);
        if (err != 0) {
            printf("Count: %d Error: %d '%s'\n", count, err, strerror(err));
        }
        count += 500;
        if (count == 5000) break;
    }

    return 0;
}

结果对我来说似乎很奇怪,因为它不是以0开头的,并且有多个重复项和奇怪的最后一个值。可能有什么问题

每次迭代都会覆盖
线程
,这将产生意想不到的后果

您正在将
count
的地址传递给线程(int*),它从
3000开始就不足为奇了;当第一个线程实际执行
*count
处的
int
时,必须将该值递增

其他线程同时从主进程的堆栈中读取相同的值,因此可以得到重复

最后的结果可以解释,因为线程没有正确连接。当最后一个线程读取
count
时,变量不再在堆栈上,因为主函数返回或正在返回

正确的代码应该更像这样:

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

//    A thread that just exits
void*  mythread(void *arg) { 
    int tArg;

    tArg = *(int*)arg;
    printf("Received %d\n", tArg);
    return 0; 
}

int main (int argc, char *argv[]) {
    int err, num = 0, count[10];
    pthread_t thread[10];

    for (num = 0; num < 10; num++) {
        /* this is horrible, these could be statically initialized */
        if (!num) 
            count[num] = 500;
        else count[num] = count[num-1] + 500;

        /* each thread should have it's own pthread_t and int* */
        /* do not change the count[num] of another thread */
        err = pthread_create(&thread[num], 0, mythread, &count[num]);
        if (err != 0) {
            printf("Count: %d Error: %d '%s'\n", count[num], err, strerror(err));
        }
    }

    for (num = 0; num < 10; num++) {
        pthread_join(thread[num], NULL);
    }

    return 0;
}

这是正常的。

您在每次迭代中都会覆盖
线程,这将产生意外的后果

您正在将
count
的地址传递给线程(int*),它从
3000开始就不足为奇了;当第一个线程实际执行
*count
处的
int
时,必须将该值递增

其他线程同时从主进程的堆栈中读取相同的值,因此可以得到重复

最后的结果可以解释,因为线程没有正确连接。当最后一个线程读取
count
时,变量不再在堆栈上,因为主函数返回或正在返回

正确的代码应该更像这样:

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

//    A thread that just exits
void*  mythread(void *arg) { 
    int tArg;

    tArg = *(int*)arg;
    printf("Received %d\n", tArg);
    return 0; 
}

int main (int argc, char *argv[]) {
    int err, num = 0, count[10];
    pthread_t thread[10];

    for (num = 0; num < 10; num++) {
        /* this is horrible, these could be statically initialized */
        if (!num) 
            count[num] = 500;
        else count[num] = count[num-1] + 500;

        /* each thread should have it's own pthread_t and int* */
        /* do not change the count[num] of another thread */
        err = pthread_create(&thread[num], 0, mythread, &count[num]);
        if (err != 0) {
            printf("Count: %d Error: %d '%s'\n", count[num], err, strerror(err));
        }
    }

    for (num = 0; num < 10; num++) {
        pthread_join(thread[num], NULL);
    }

    return 0;
}
这是正常的

Received 1000
Received 2000
Received 2500
Received 1500
Received 500
Received 3000
Received 3500
Received 4000
Received 4500
Received 5000