#include <QCoreApplication> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <iostream> #include <unistd.h> #include <errno.h> #include <pthread.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include<dirent.h> using namespace std; pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; int count_value = 0; void * MyFunc(void * arg) { if(arg == NULL) { printf("param is not allow NULL. "); return NULL; } //加锁--所有线程都能访问的全局变量加锁 pthread_mutex_lock(&mutex); int *pnumx = (int*) arg; int i = 0; while(i < 1000) { printf("thread%d count=%d ", *pnumx, count_value++); sleep(1); i++; } pthread_mutex_unlock(&mutex); return NULL; } void * MyFunc2(void * arg) { if (arg == NULL) { printf("param is not allow NULL! "); return NULL; } //这个锁和myfunc中的锁是同一个锁,myfunc被锁,这里也会被锁 pthread_mutex_lock(&mutex); int * pnumx = (int *) arg; int i = 0; while (i < 1000) { printf("thread%d count=%d ", *pnumx, count_value++); sleep(1); i++; } //解锁 pthread_mutex_unlock(&mutex); return NULL; } //Already running return true. bool IsSingleProcess () { int no_os_flag=1; #ifdef linux no_os_flag=0; std::cout<<"It is in Linux OS!"<<endl; #endif #ifdef _UNIX no_os_flag=0; std::cout<<"It is in UNIX OS!"<<endl; #endif #ifdef __WINDOWS_ no_os_flag=0; std::cout<<"It is in Windows OS!"<<endl; #endif #ifdef _WIN32 no_os_flag=0; std::cout<<"It is in WIN32 OS!"<<endl; #endif if(1==no_os_flag){ std::cout<<"No OS Defined ,I do not know what the os is!"<<endl; } long pid = 0; char full_name[1024] = {0}; char proc_name[1024] = {0}; int fd; pid = getpid(); std::cout << "pid = " << pid << " "; sprintf(full_name, "/proc/%ld/cmdline", pid); if (access(full_name, F_OK) == 0) { fd = open (full_name, O_RDONLY); if (fd == -1) return false; read (fd, proc_name, 1024); close (fd); } else return false; char self_proc_name[512] = {0}; char * p = proc_name; int pt = 0; while (*p != ' ' && *p != '