
pthread w32版本线程终止(pthread_exit)后的返回值的问题。
通过pthread_exit终止线程的执行pthread exit,pthread_join等待线程终止返回的值在pthread w32版本和标准pthread不一样。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
int *exitStatus = NULL;
int count = 0;
void* run_up(void *arg)
{
printf("&exitStatus: %d, exitStatus: %d, *exitStatus: %d, &*exitStatus: %d\n", &exitStatus, exitStatus, *exitStatus, &*exitStatus);
pthread_exit((void *) exitStatus);
//pthread_exit(NULL);
//const char *s = "Thread funtion finished!";
//pthread_exit((void *) s);
//return (void *) exitStatus;
//return NULL;
//return (void *) "Thread funtion finished!";
}
int main()
{
exitStatus = (int *) malloc(sizeof(int));
*exitStatus = 8;
pthread_t pthread1, pthread2;
pthread_create(&pthread1,
NULL,
run_up,
(void *) 0);
int *p_status = (int *) malloc(sizeof(int));
int **status = &p_status;
pthread_join(pthread1, (void **) status);
printf("&p_status: %d, p_status: %d, *p_status: %d, &*p_status: %d\n", &p_status, p_status, *p_status, &*p_status);
printf("&status: %d, status: %d, *status: %d, &*status: %d, **status: %d, &**status: %d\n", &status, status, *status, &*status, **status, &**status);
printf("final count: %d\n", count);
return 0;
}
include_pthread = /cygdrive/d/usr/lib/pthreads-w32-2-8-0-release/Pre-built.2/include lib_pthread = /cygdrive/d/usr/lib/pthreads-w32-2-8-0-release/Pre-built.2/lib OUTPUT = ../Debug/ objs = $(OUTPUT)*.o make: make-std make-nostd: clean g++ -c pthread_exit_test2.cpp -o ../Debug/pthread_exit_test2.o # -L$(lib_pthread) 指定lib库文件的位置:-L/cygdrive/d/usr/lib/pthreads-w32-2-8-0-release/Pre-built.2/lib # 如果没有指定lib库文件的位置:-L$(lib_pthread),需要将lib库文件拷贝到可以查找到的目录下。我这里是cygwin # 环境: $ cp -rf /cygdrive/d/usr/lib/pthreads-w32-2-8-0-release/Pre-built.2/lib/libpthreadGC2.a /cygdrive/d/sbin/lib # 这样就可以不指定lib库文件的位置: # g++ $(objs) -o $(OUTPUT)pthread_exit_test2 -lpthreadGC2 g++ $(objs) -o $(OUTPUT)pthread_exit_test2 -L$(lib_pthread) -lpthreadGC2 # g++ $(objs) -o $(OUTPUT)pthread_exit_test2 -lpthreadGC2 make-std: clean g++ -c pthread_exit_test2.cpp -o ../Debug/pthread_exit_test2.o g++ $(objs) -o $(OUTPUT)pthread_exit_test2 -lpthread clean: rm -Rf ./*.bak rm -Rf ./*.o rm -Rf ./*.exe rm -Rf ../Debug/*
pthread使用的是win版本:pthreads-w32-2-8-0-release,Pre-built中包含支持linux的pthread库:libpthreadGC2.a、libpthreadGCE2.a
将Pre-built中包含支持linux的pthread库拷贝到cygwin下:/cygdrive/d/sbin/lib
$ cp -rf /cygdrive/d/usr/lib/pthreads-w32-2-8-0-release/Pre-built.2/lib/libpthreadGC2.a /cygdrive/d/sbin/lib/

make
$ make -f pthread_exit_test2.Makefile make
运行
$ ./pthread_exit_test2.exe
运行结果
&exitStatus: 4210712, exitStatus: 536937064, *exitStatus: 8, &*exitStatus: 536937064 &p_status: 2272376, p_status: 536937064, *p_status: 8, &*p_status: 536937064 &status: 2272372, status: 2272376, *status: 536937064, &*status: 2272376, **status: 8, &**status: 536937064
这个结果是正常的。

但奇怪的是,如果pthread使用的是win版本:pthreads-w32-2-8-0-release
make
$ make -f pthread_exit_test2.Makefile make-nostd
运行
$ ./pthread_exit_test2.exe
运行结果
运行输出:

&p_status: 2272376, p_status: 536937080, *p_status: 0, &*p_status: 536937080 &exitStatus: 4210712, exitStatus: 536937064, *exitStatus: 8, &*exitStatus: 536937064 &status: 2272372, status: 2272376, *status: 536937080, &*status: 2272376, **status: 0, &**status: 536937080
这个结果就不正常了,和预期结果不一致。
有时候还没打印出exitStatus的值:
&p_status: 2272376, p_status: 536937080, *p_status: 0, &*p_status: 536937080 &status: 2272372, status: 2272376, *status: 536937080, &*status: 2272376, **status: 0, &**status: 536937080
这个就奇怪了。
这个问题后来看了下,发现pthread_join函数调用失败,返回错误:error: errno: 3
根据errno.h中的定义:

#define ESRCH 3 /* No such process */
No thread with the ID thread could be found.
意思是没找到这个线程?
所以这里pthread_join调用后并没有获取到线程终止后的返回值,并没有将线程终止后的返回值拷贝到pthread_join的第二个参数。
只是我程函数中可以打印出线程id, 而且和printf("pthread: %ld\n", pthread1);打印出来的值是一致的,而且线程函数也执行了,怎么就找不到这个线程呢?
pid_t pid = getpid();
// pid_t tid = gettid(); // Glibc does not provide a wrapper for this system call; call it using syscall(2).
pthread_t tid = pthread_self();
printf("pid: %ld, tid: %ld\n", pid, tid);
那么这个pthread_join调用失败具体是什么原因?
一般情况下 , 线程运行结束之后 , 线程函数正常返回 , 但是应用程序可以调用 terminatethread 强行终止 (异常终止一个线程) 某一线程的执行 。其实ajax实现同步和异步很简单,只要设置async的属性值就行,默认的设置值为true,这种情况为异步方式,就是说当ajax发送请求后,在等待server端返回的这个过程中,前台会继续 执行ajax块后面的脚本,直到server端返回正确的结果才会去执行success,也就是说这时候执行的是两个线程,ajax块发出请求后一个线程 和ajax块后面的脚本(另一个线程),两个线程互不影响。从线程函数中返回,此方法不适用main函数pthread exit,congmain函数中返回相当于调用exit(),终止整个进程。
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-109932-1.html
在菲越面前不好交待
马云就是废话太多