日期:2014-05-16 浏览次数:20869 次
void thread_func() { int i_val; printf("[线程%d]i_val = \t0x%016p\n",pthread_self(),&i_val); int* pi = new int(); if(pi == NULL) { fprintf(stderr,"分配内存失败\n"); return; } printf("[线程%d]pi = \t\t0x%016p\n",pthread_self(),pi); char* pMalloc; if((pMalloc = (char*) malloc(10)) == NULL) { return; } printf("[线程%d]pMalloc = \t\t0x%016p\n",pthread_self(),pMalloc); sleep(2); }
#include<pthread.h> #include<stdio.h> #include<stdlib.h> void* thread_func(void* arg) { int i_val; printf("[thread %d]i_val= \t0x%016p\n",pthread_self(),&i_val); int* pi = new int(); if(pi == NULL) { fprintf(stderr,"·Öä´Ã); } printf("[thread %d] pi = \t\t0x%016p\n",pthread_self(),pi); char* pMalloc; if((pMalloc = (char*) malloc(10)) == NULL) { } printf("[thread %d]pmalloc = \t\t0x%016p\n",pthread_self(),pMalloc); } int main(int argc,int argv[]) { int error; int *temptr; pthread_t thread_id1, thread_id2; pthread_create(&thread_id1,NULL,thread_func,NULL); pthread_create(&thread_id2,NULL,thread_func,NULL); if(error=pthread_join(thread_id1,NULL)) { perror("pthread_join"); exit(EXIT_FAILURE); } if(error=pthread_join(thread_id2, NULL)) { perror("pthread_join"); exit(EXIT_FAILURE); } return 0; }