Exemple #1
0
NPT_Result TaskGroup::startTask(Task *task)
{
	WriteLocker locker(m_stateLock);

	if (m_state != State_Running) {
		delete task;
		return NPT_ERROR_INVALID_STATE;
	}

	joinThreads();

	task->m_taskGroup = this;

	{
		TaskThread *thread = NULL;
		WriteLocker locker(m_freeThreadListLock);
		NPT_List<TaskThread*>::Iterator it = m_freeThreadList.GetFirstItem();
		if (it) {
			thread = *it;
			m_freeThreadList.Erase(it);
			thread->setTask(task);
			return NPT_SUCCESS;
		}
	}

	return startNewThread(task);
}
/*******************************************************************************
 * Overloaded Constructor
 * This is public, and it initializes a new UdpRelay object with the given 
 * address parameter. This object instansiates a Socket object and a UdpMulicast
 * object to facilitate communications with local and remote hosts. Private
 * helper functions spin off instance threads and join them when they are done
 * being used.
 * @param udpPort       a character string representing the UDP port number
 *                      the format expected is ###.###.###.###:#####
 */
UdpRelay::UdpRelay(char* udpAddr) {
   // Parse parameter and assign members
   groupIp = strtok(udpAddr, ":");
   groupPort = (unsigned short int)atoi(strtok(NULL, "\0"));
   udpRelayRunning = true;
   // Initialize dynamic members
   localSocket = new Socket(TCP_PORT);
   localUdpMulticast = new UdpMulticast(groupIp, groupPort);
   msgBuffer = new char[BUFFER_MAX_SIZE];
   msgMutex = new pthread_mutex_t;
   cond = new pthread_cond_t;
   // Print welcome message
   printf("> booted up at %s:%d\n", groupIp, groupPort);
   // Create persistent threads
   createThreads();
   // Join persistent threads
   joinThreads();
   // Clean up dynamic members
   delete localSocket;
   localSocket = NULL;
   delete localUdpMulticast;
   localUdpMulticast = NULL;
   delete msgBuffer;
   msgBuffer = NULL;
   delete msgMutex;
   msgMutex = NULL;
   delete cond;
   cond = NULL;
}
Exemple #3
0
int scanRange(const char *ip, int port, int max_threads) {

	int maxthreads = max_threads > 254 ? 254 : max_threads;
	pthread_t threads[255] = {0};
	threaddata data[255] = {0};

	pthread_attr_t attr;
	size_t stack;
	pthread_attr_init(&attr);
	pthread_attr_getstacksize(&attr, &stack);
	stack = 32768;
	pthread_attr_setstacksize(&attr, stack);

	pthread_mutex_init(&mutex, NULL);

	volatile int x = 1;
	while(done < 254) {
		while(x < 255 && running((pthread_t*) threads, x) < maxthreads) {
			threaddata* d = &data[x];
			snprintf(d->host, sizeof(d->host), "%s.%d", ip, x);
			d->port = port;
			d->status = -1;
			if (!pthread_create(&threads[x], &attr, scanHost, &data[x])) {
				x++;
			}
			else {
				break;
			}
		}
		joinThreads(threads, x);
	}

	joinThreads(threads, x);

	pthread_attr_destroy(&attr);

	int i = 0;
	for (; i < 255; i++) {
		if (data[i].status > 0) dprintf(1, "%s\n", data[i].host);
	}

	pthread_mutex_destroy(&mutex);
	return 0;
}
int main (int argc, char ** argv)
{
	if (argc != 3) 
	{
		printf("Use: %s <N> <T>,  onde 'N' é a dimensão da matriz unitária e 'T' é o número de threads.\n", argv[0]);
		return 1;
	}

	//Retira valores dos argumentos passados pela linha de comando
	matrix_size = atoi(argv[1]);
	number_of_threads = atoi(argv[2]);

	//Aloca espaço para variáveis e ponteiros para colunas
	A = allocateMatrix(matrix_size);

	//Gera matrizes A e B
	generateMatrix(A, matrix_size);

	//Imprime A e B (teste)
	if (matrix_size < 20)
		printMatrix(A, matrix_size, "A");

	//Ponteiro com threads
	pthread_t * p_threads;
	//Alocação de espaço para threads de acordo com o número de threads
	p_threads = (pthread_t *) malloc(number_of_threads * sizeof(pthread_t));


	//Inicializa o mutex
  	pthread_mutex_init(&mut, NULL);

	//Captura tempo inicial
	clock_gettime(CLOCK_MONOTONIC, &initial_time);

	//Criação de threads
	createThreads(p_threads, number_of_threads, matrix_size, A);

	//Join das threads
	joinThreads(p_threads);

	//Captura tempo final
	clock_gettime(CLOCK_MONOTONIC, &end_time);
  	
  	//imprime resultado
	printf("A soma de todos os termos é: %d\n", sum);

	//Calcula tempo final
	double elapsed = end_time.tv_sec - initial_time.tv_sec;
	elapsed += (end_time.tv_nsec - initial_time.tv_nsec) / 1000000000.0;
 	printf ("O tempo de processamento foi de: %f segundos\n", elapsed);   
	
	//Inicializa o mutex
  	pthread_mutex_destroy(&mut);  
	return 0;
}
Exemple #5
0
void finalizeRMC ()
{
  printDebugMessage ("before join threads RMC");

  joinThreads (ll_threads);
  for (unsigned i = 0; i < worker_threads.size(); i++ )
    {
      delete worker_threads [i];
    }
  worker_threads.clear ();
  delete communicator_thread;

  printDebugMessage ("after join threads RMC");
}
Exemple #6
0
/**
* Main
*
* @TODO better address management
*
* @param argc
* @param **argv
* @return Exit code
*/
int main(int argc, char **argv) {

    std::atexit(on_exit);

    std::cout << "\n ************ Address Setup ***********\n";
    std::string input = "";
    char myChar = {0};
    std::cout << "Choose an address: Enter 0 for master, 1 for child, 2 for Master with 1 Arduino routing node (02), 3 for Child with Arduino routing node  (CTRL+C to exit) \n>";
    std::getline(std::cin,input);

    if(input.length() > 0) {
        myChar = input[0];
        if(myChar == '0'){
            thisNodeAddr = 00;
            otherNodeAddr = 1;
        }else if(myChar == '1') {
            thisNodeAddr = 1;
            otherNodeAddr = 00;
		}else if(myChar == '2') {
            thisNodeAddr = 00;
            otherNodeAddr = 012;
		}else if(myChar == '3') {
            thisNodeAddr = 012;
            otherNodeAddr = 00;
        } else {
            std::cout << "Wrong address! Choose 0 or 1." << std::endl;
            exit(1);
        }

        std::cout << "ThisNodeAddress: " << thisNodeAddr << std::endl;
        std::cout << "OtherNodeAddress: " << otherNodeAddr << std::endl;
        std::cout << "\n **********************************\n";
    } else {
            std::cout << "Wrong address! Choose 0 or 1." << std::endl;
            exit(1);
    }


    configureAndSetUpTunDevice();
    configureAndSetUpRadio();

    //start threads
    tunRxThread.reset(new boost::thread(tunRxThreadFunction));
    tunTxThread.reset(new boost::thread(tunTxThreadFunction));
    radioRxTxThread.reset(new boost::thread(radioRxTxThreadFunction));

    joinThreads();

    return 0;
}
Exemple #7
0
    ~Private()
    {
        if (!--driver->ref) {
            joinThreads();

            if (driver->driver->isValid()) {
                if (!driver->driver->close()) {
                    yWarning() << "Cannot close device" << name;
                }
            }

            delete driver;
            driver = NULL;
        }
    }
int main (int argc, char *argv[])
{
   struct timespec tStart;
   struct timespec tEnd;
   pthread_t *aThreads;
   THREAD_PARAMS *aParams;
   int64_t * aArray = NULL;

   int64_t iret = 0;
   int64_t cThreads = 0;
   int64_t cTasks = 0;
   bool fBlock = false;
   bool fSimple = true; 
   bool fRandom = false;

   iret = parseCommandLine(argc, argv, &cThreads, &cTasks, &fBlock, &fSimple, &fRandom);

   if (0 == iret) {
      iret = allocateArray(fRandom, cTasks, &aArray);
   }

   if (0 == iret) {
      iret = clock_gettime(CLOCK_REALTIME, &tStart);
   }
   
   if (0 == iret) {
      iret = createThreads(cThreads, cTasks, fBlock, fSimple, aArray, &aThreads, &aParams);
   }

   if (0 == iret) {
      iret = joinThreads(cThreads, cTasks, aThreads, aParams);
      aThreads = NULL;
      aParams = NULL;
   }

   clock_gettime(CLOCK_REALTIME, &tEnd);

   if (0 == iret) {
      //test(fSimple, cTasks, aArray);
   }

   free(aArray);

   struct timespec tsDiff = diff(tStart, tEnd);
   //printf("Elapsed Time: %d.%09d Sec.\n", tsDiff.tv_sec, tsDiff.tv_nsec);
   printf("%d, %d, %s, %s, %s, %d.%09d\n", cThreads, cTasks, fBlock ? "block":"cyclic", fSimple ? "negation":"factorial", fRandom ? "random":"ramp",tsDiff.tv_sec, tsDiff.tv_nsec);
   return(iret);
}
int main(int argc, char* argv[])
{
    puts(license); // show GPL at start of program output
    
    int threads = promptForMoreThreads();
    ThreadData td[threads];
    createThreads(td, threads);
    
    puts("Main thread created all child threads..");

    joinThreads(td, threads);

    puts("Main thread joined all child threads. Now exiting.\n");

    return 0;
}
Exemple #10
0
		void finalize(){
			if(isInitialized){
				//if not join ,join.
				joinThreads();
				//destroy things
				for(int i=0; i<nSockets; i++){
					threadGroups[i]->finalize();
					delete threadGroups[i];
				}
				threadGroups.clear();
				//
				for(int i=0; i<nSockets; i++){
					socketChannels[i]->destroy();
					delete socketChannels[i];
				}
				socketChannels.clear();
				//
				pthread_barrier_destroy(&barrier_);	
				//
				isInitialized = false;
			}
		}
Exemple #11
0
void joinRunners ()
{

  joinThreads (ll_threads);
  the_runners.clear();
}