#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 not the allow IS NULL \ the n-. " ); Return NULL; } // locked - Global variables can be accessed by all threads lock pthread_mutex_lock (& mutex ); int * pnumx = ( int * ) Arg; int I = 0 ; the while (I < 1000 ) { the printf ( " Thread% D COUNT =% D \ n- " , * pnumx, count_value ++ ); SLEEP ( . 1 ); I++ ; } pthread_mutex_unlock ( & the mutex); return NULL; } void * myfunc2 ( void * Arg) { IF (Arg == NULL) { the printf ( " param Not the allow IS NULL \ n-! " ); Return NULL; } / / the lock and the lock is myfunc same lock, myfunc is locked, there will be locked the pthread_mutex_lock (& the mutex); int * pnumx = ( int * ) Arg; int I = 0 ; the while (i < 1000) { printf("thread%d count=%d\n", *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 (if1==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 != '\0') { self_proc_name[pt] = *p; p++; pt++; } string self_final_name = basename(self_proc_name); std::cout << " self_final_name = " << self_final_name << std::endl << flush; DIR *dir; struct dirent * result; dir = opendir ("/proc"); while ((result = readdir (dir)) != NULL) { if (! strcmp(result->d_name, ".") || ! strcmp (result->d_name, "..") || ! strcmp (result->d_name, "thread-self") || ! strcmp (result->d_name, "self") || atol (result->d_name) == pid) continue; memset(full_name, 0, sizeof(full_name)); memset(proc_name, 0, sizeof(proc_name)); sprintf(full_name, "/proc/%s/cmdline", result->d_name); if (access(full_name, F_OK) == 0) { fd = open (full_name, O_RDONLY); if (fd == -1) continue; read (fd, proc_name, 1024); close (fd); char *q = proc_name; pt = 0; memset(self_proc_name, 0, sizeof (self_proc_name)); while (*q != ' ' && *q != '\0') { self_proc_name[pt] = *q; q++; pt++; } string other_final_name = basename(self_proc_name); if (self_final_name == other_final_name) { cout << "full_name = " << full_name << endl << flush; cout << "other_final_name: " << other_final_name << endl; return true; } } } return false; } int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); //condition compile #ifdef _WIN32 if(true == IsSingleProcess()) { std::cout << "process is running in another place" << endl << flush; return 0; } #else if(true == IsSingleProcess()) //Already running return true. { std::cout << "......"<< endl; return 0; } #endif cout<<".............START............."<<endl; pthread_t thr1,thr2; pthread_attr_t attr; pthread_attr_init(&attr); //设置进程为可分离状态 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); int aa = 1, bb = 2; //创建线程 if (pthread_create(&thr1, &attr, MyFunc, &aa) != 0) { printf("create thread is failed ! error message :%s\n", strerror(errno)); return -1; } //释放进程属性对象 pthread_attr_destroy(&attr); if (pthread_create(&thr2, NULL, MyFunc2, &bb) != 0) { printf("create thread is failed ! error message :%s\n", strerror(errno)); return -1; } pthread_join(thr2, NULL); printf("main end\n"); return 0; return a.exec(); }