⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 thread.cpp

📁 D-ITG2.4源代码
💻 CPP
字号:
 /*	Component of the D-ITG 2.4 Platform
 *
 * 	
 *	copyright	: (C) 2004  	by Stefano Avallone, Alessio Botta, Donato Emma, 
 *					Salvatore Guadagno, Antonio Pescape'
 *					DIS Dipartimento di Informatica e Sistemistica				 
 *					(Computer Science Department)
 *					University of Naples "Federico II"	
 *	email:		: {stavallo, pescape}@unina.it, {abotta, demma, sguadagno}@napoli.consorzio-cini.it
 *
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 */
 
 

#ifdef WIN32
    int num = 0;
#endif

#include "thread.h"




int createThread(void *argument, void *(nameFunction) (void *), void *attrib, pthread_t &idThread)
{
	
	int ret = 0;
#ifdef WIN32	
	
	void *pid;
	






	(HANDLE)idThread =
	    CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) nameFunction, argument, (DWORD) NULL,
	    (unsigned long *) &pid);
#ifdef DEBUG
    printf("Return value CreateThread (WIN32) %d\n",(int)idThread);
#endif
	if (idThread == NULL) ret = -1;
#ifdef DEBUG
	printf("Return value createThread : %d\n",ret);
#endif
#endif

#ifdef LINUX_OS
	
	 
	ret = pthread_create(&idThread, NULL, nameFunction, argument);
	if (ret != 0) ret = -1;
#ifdef DEBUG
    printf("Return value pthread_create (LINUX) %d\n",(int)idThread);
    printf("Return value createThread : %d\n",ret);
#endif
#endif
	return ret;
};


int terminateThread(pthread_t idThread)
{
	
	int ret = 0;
#ifdef WIN32
	


	ret = TerminateThread((HANDLE) idThread, 0);
	if (ret == 0) ret = -1;
	else ret = 0;
#endif
#ifdef LINUX_OS
	
	ret = pthread_cancel(idThread);
	if (ret != 0) ret = -1;
#endif
#ifdef DEBUG
    printf("Return value terminateThread : %d\n",ret);
#endif
	return ret;
}; 


void exitThread()
{
#ifdef WIN32
	

	ExitThread(0);
#endif
#ifdef LINUX_OS
	
	pthread_exit(0);
#endif
};


int joinThread(int numFlow, pthread_t hThr[])
{
	int ret = 0;
#ifdef WIN32
	DWORD temp = 0;
	




	temp = WaitForMultipleObjects(numFlow, (const HANDLE *) hThr, TRUE, INFINITE);
#ifdef DEBUG
    printf("Return value WaitForMultipleObjects(WIN32) : %d\n",(int)temp);
#endif
	if (temp == WAIT_FAILED) ret = -1;
#endif
#ifdef LINUX_OS
	
	int ret2 = 0;
	for(int i = 0; i < numFlow; i++) {
		ret2 = pthread_join(hThr[i], NULL);
		if (ret2 != 0)
			ret = -1;
	}
#endif
#ifdef DEBUG
    printf("Return value joinThread : %d\n",ret);
#endif
    return ret;
};



#ifdef WIN32

int mutexThreadInit(HANDLE &mutex)
{
	int ret = 0;
	



	 char stringa[30];
	 char nameMutex[30]="";
	 num++;

	  
	  strcat(nameMutex, nameProgram);
      sprintf(stringa, "%d", num);
	  strcat(nameMutex, stringa);
	 
	 
	 mutex = CreateMutex(NULL, FALSE, nameMutex);

#ifdef DEBUG
	 printf("Return value CreateMutex (WIN32) : %d \n",(int)mutex);
      printf("Name mutex %s \n", nameMutex);
#endif

	 if (mutex == NULL) ret = -1;
#ifdef DEBUG
     printf("Return value mutexThreadInit : %d \n",ret);
#endif
	return ret;
}
#endif

#ifdef LINUX_OS

int mutexThreadInit(void* mutex)
{
	int ret = 0;
	
	ret = pthread_mutex_init((pthread_mutex_t *)mutex, NULL);
#ifdef DEBUG
	printf("Return value pthread_mutex_init (LINUX) : %d \n",mutex);
	printf("Return value mutexThreadInit : %d \n",ret);
#endif
	return ret;
}
#endif
	

int mutexThreadLock(void *mutex)
{
	int ret = 0;
#ifdef LINUX_OS
	
	ret = pthread_mutex_lock((pthread_mutex_t *) mutex);
	if (ret != 0 ) ret = -1;
#endif
#ifdef WIN32
	DWORD temp = 0;
	


	temp = WaitForSingleObject((HANDLE)mutex,INFINITE);
#ifdef DEBUG
    printf("Return value WaitForSingleObject(WIN32) : %d\n",(int)temp);
#endif
	if (temp == WAIT_FAILED) ret = -1;
#endif
#ifdef DEBUG
    printf("Return value mutexThreadLock : %d\n",ret);
#endif
	return ret;
};


int mutexThreadUnlock(void *mutex)
{
	int ret = 0;
#ifdef LINUX_OS
	
	ret = pthread_mutex_unlock((pthread_mutex_t *) mutex);
	if (ret != 0 ) ret = -1;
#endif
#ifdef WIN32
	BOOL flag = 0;
	

	flag = ReleaseMutex((HANDLE)mutex);
#ifdef DEBUG
    printf("Return value ReleaseMutex(WIN32) in mutexThreadUnlock : %d\n",flag);
#endif
	if (flag == 0) ret = -1;
#endif
#ifdef DEBUG
    printf("Return value mutexThreadUnlock : %d\n",ret);
#endif
	return ret;
};



int mutexThreadRelease(void* mutex)
{
	int ret = 0;
#ifdef LINUX_OS
	
	ret = pthread_mutex_destroy((pthread_mutex_t *)mutex);
	if (ret != 0) ret = -1;
#endif
#ifdef WIN32
		BOOL flag = 0;
		

		flag = CloseHandle((HANDLE)mutex);
		if (flag == 0) ret = -1;
#endif
#ifdef DEBUG
    printf("Return value mutexThreadRelease : %d\n",ret);
#endif
	return ret;
}





int closeSock(int socket)
{
	int ret = 0;
#ifdef LINUX_OS
	
	ret = close(socket);
#endif
#ifdef WIN32
	
	ret = closesocket(socket);
	if (ret == SOCKET_ERROR) ret = -1;
#endif
#ifdef DEBUG
    printf("Return value closeSock : %d\n",ret);
#endif
	return ret;
};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -