void Scene::computeColorsAntialiasing(std::function<void(int, int, ColorCRef)> paint, int threadCount, int level) {
     upto(threadCount, 1);
     downto(threadCount, 4);
     std::vector< std::vector <ColorFloat> > m(screen.width, std::vector<ColorFloat> (screen.height, ColorFloat()));
     computeColors([paint, &m](int x, int y, ColorCRef c) { m[x][y] = c; paint(x, y, c); }, threadCount);
     if (level > 1) {
         for (int x = 1; x + 1 < screen.width; x++) {
             for (int y = 1; y + 1 < screen.height; y++) {
                 int badness = 0;
                 for (int dx : {-1, 1})
                     for (int dy : {-1, 1})
                         badness += (m[x][y] - m[x + dx][y + dy]).norm();
                 if (badness > badnessLimit) {
                     paint(x, y, Color(255, 0, 0));
                 }
             }
         }
         int taskCount = 10;
         ThreadPool pool;
         for (int t = 0; t < taskCount; t++)
             pool.addTask([this, &paint, &m, level, t, taskCount]()
                          { multiThreadAntialiasePart(paint, m, level, t, taskCount); });
         pool.executeAll(threadCount);
     }
 }
示例#2
0
void Server::serve(int port, int maxClients, int numThreads) {
    // set up the server socket
    debug("Server::serve -- initializing listening socket");
    int serverId = this->initSocket(port);
    
    // set up the client manager
    debug("Server::serve -- setting up client manager");
    ClientManager* mgr = new ClientManager(maxClients);
    
    // set up the thread pool
    debug("Server::serve -- setting up thread pool");
    ThreadPool* pool = new ThreadPool(numThreads);
    pool->run(&run, mgr);
    
    // set up client
    int clientId;
    struct sockaddr_in client_addr;
    socklen_t clientlen = sizeof(client_addr);

      // accept clients
    debug("Server::serve -- waiting for client");
    while ((clientId = accept(serverId, (struct sockaddr *)&client_addr, &clientlen)) > 0) {
        ClientProxy* client = new ClientProxy(clientId);
        debug("Server::serve -- adding client to queue");
        mgr->pushClient(client);
    }
    
    // close the server socket
    debug("Server::serve -- closing server socket");
    close(serverId);

    // clean up some memory (even though this will likely not ever be reached)
    delete mgr;
    delete pool;
}
示例#3
0
//-----------------------------------------------------------------------------
TEST(ThreadPool, Sequential_WithMPSCBoundedQueue) {
  // add N jobs to the queue, and wait until all will be completed
  // this test should be faster than Sequential_WithStdQueue
  ThreadPool pool;

  MPMCBoundedQueue<std::future<int>> queue(128);

  const int repetitions = 100;

  for (int i = 1; i <= repetitions; i++) {
    queue.push(std::move(pool.process([i]() {
      std::this_thread::sleep_for(std::chrono::milliseconds(20));
      return i;
    })));
  }

  int sum_actual = 0;
  std::future<int> item;
  while (queue.pop(item)) {
    sum_actual += item.get();
  }

  auto sum = [](int n) -> int { return n * (n + 1) / 2; };

  ASSERT_EQ(sum_actual, sum(repetitions));
}
示例#4
0
int main(int argc, char **argv){

	bool ret = true;

	long size = sysconf(_SC_NPROCESSORS_ONLN);
	cout<<"number of workers: "<<size<<endl;

	ThreadPool p;

	ret = p.Init(size, start_routine, arg);
	if (!ret){
		return -1;
	}

	ret = p.Start();
	if (!ret){
		p.Shutdown();
		return -1;
	}

	int n = 3;
	while(n-->0){
		sleep(2);
	}
	p.Shutdown();

	return 0;
}
示例#5
0
void* ThreadPool::poll(void *arg) {
	ThreadPool* ths = (ThreadPool*)arg;
	while (true) {
		if (!ths->prioritypooling) {
			if (ths->wpool->tasksPending()) {
				Task *task = ths->wpool->getTask();
				PoolThread *idleThread = ths->getIdleThread();
				idleThread->task = task;
				idleThread->idle = false;
				cout << "got task" << endl;
				idleThread->mthread->interrupt();
			} else {
				Thread::mSleep(1);
			}
		} else {
			if (ths->wpool->tasksPPending()) {
				Task *task = ths->wpool->getPTask();
				PoolThread *idleThread = ths->getIdleThread();
				idleThread->task = task;
				idleThread->idle = false;
				idleThread->mthread->interrupt();
			} else {
				Thread::mSleep(1);
			}
		}
	}
	return NULL;
}
示例#6
0
void render()
{
	scene.beginFrame();
	vector<Rect> buckets = getBucketsList();

	if (!scene.settings.interactive && (scene.settings.wantPrepass || scene.settings.gi)) {
		// We render the whole screen in three passes.
		// 1) First pass - use very coarse resolution rendering, tracing a single ray for a 16x16 block:
		for (Rect& r: buckets) {
			for (int dy = 0; dy < r.h; dy += 16) {
				int ey = min(r.h, dy + 16);
				for (int dx = 0; dx < r.w; dx += 16) {
					int ex = min(r.w, dx + 16);
					Color c = raytraceSinglePixel(r.x0 + dx + ex / 2, r.y0 + dy + ey / 2);
					if (!drawRect(Rect(r.x0 + dx, r.y0 + dy, r.x0 + ex, r.y0 + ey), c))
						return;
				}
			}
		}
	}

	MainRenderTask mtrend(buckets);
	pool.run(&mtrend, scene.settings.numThreads);

	if (scene.settings.needAApass()) {
		// the previous render was without any anti-aliasing whatsoever, and the
		// scene file specifies that AA is desired. Detect edges here, and refine in another pass:
		detectAApixels(buckets);

		RefineRenderTask refineTask(buckets);
		pool.run(&refineTask, scene.settings.numThreads);
	}
}
示例#7
0
bool ApService::InitApService(std::string ApListenIp, int ApListenPort, int threadPoolNum) {
    bool status = true;

    _ApListenIp = ApListenIp;
    _ApListenPort = ApListenPort;
    _threadPoolNum = threadPoolNum;

    SharedPointer<IProcessor> xp(
        new apapiProcessor(
            SharedPointer<apapi>(
                new ApService)));

    ServiceManager sm;
    sm.add_service(xp);

    ThreadPool tp;
    tp.init(_threadPoolNum);
    Server* server = new Server(&sm, &tp, _ApListenPort);

    if (0 != server->serve()) {
        status =  false;
    }

    return status;
}
示例#8
0
void testExecPerSecond(int numberOfThreads, int numberOfKeys)
{
	ThreadPool<Task> pool;

	pool.init(numberOfThreads, numberOfKeys, 10000);

	for (long long int i = 0; ; ++i) {
		Task t(i % numberOfKeys);
		pool.enqueue(t);

		if (i % 100000 == 0) {
			int execCounterCur = execCounter.load();
			int millisCur = getMilliseconds();

			if (execCounterPrev != 0) {
				int counterDif = execCounterCur - execCounterPrev;
				long long int millisDif = millisCur - millisPrev;

				long long int execPerSec = (counterDif / millisDif) * 1000;

				cout << "exec per sec: " << execPerSec << "" << endl;
			}

			execCounterPrev = execCounterCur;
			millisPrev = millisCur;
		}
	}
}
示例#9
0
	int testThreadPool()
	{
#ifdef __LINUX__
		cout << "this is linux" << endl;
#endif

#ifdef __WINDOWS__
		cout << "this is windows" << endl;
#endif

		ThreadPool pool;
		pool.Run(8);

		pool.AddTask(new MyTask());

		MySignal si;

		for (int i = 0; i < 8; ++i)
			//while (running)
		{
			pool.AddTask(new MyTask());
			cout << "add task ...." << endl;
			//Timer::Sleep(500);
		}

		int sec = 0;
		while (running)
		{
			cout << "wait sec " << sec++ << endl;
			Timer::Sleep(1000);
		}

		return 0;
	}
DWORD WINAPI ThreadPool::WorkerThread(LPVOID lpParam)
{
	CoInitialize(NULL);
	ThreadPool *obj = (ThreadPool *)lpParam;

	CARBONLOG_CLASS_PTR logger(carbonLogger::getLoggerPtr());
	CARBONLOG_INFO(logger, "[WorkerThread] : starting worker thread now..");

	while(obj->shouldContinue)
	{
		std::string jobMsg, jobOutput;
		if(obj->getJobMsg(jobMsg))
		{
			CARBONLOG_DEBUG(logger, "[WorkerThread] : Processing job ..");
			processJob(jobMsg,jobOutput);
			if(!obj->writeJobOutput(jobOutput))
				CARBONLOG_ERROR(logger, "[WorkerThread] : Failed to write packet to BI");
		}

		else
			Sleep(10);
	}

	CARBONLOG_INFO(logger, "[WorkerThread] : exiting worker thread now..");
	CoUninitialize();

	return 0;

}
示例#11
0
void CpuNonbondedForce::calculateDirectIxn(int numberOfAtoms, float* posq, const vector<RealVec>& atomCoordinates, const vector<pair<float, float> >& atomParameters,
                const vector<set<int> >& exclusions, vector<AlignedArray<float> >& threadForce, double* totalEnergy, ThreadPool& threads) {
    // Record the parameters for the threads.
    
    this->numberOfAtoms = numberOfAtoms;
    this->posq = posq;
    this->atomCoordinates = &atomCoordinates[0];
    this->atomParameters = &atomParameters[0];
    this->exclusions = &exclusions[0];
    this->threadForce = &threadForce;
    includeEnergy = (totalEnergy != NULL);
    threadEnergy.resize(threads.getNumThreads());
    gmx_atomic_t counter;
    gmx_atomic_set(&counter, 0);
    this->atomicCounter = &counter;
    
    // Signal the threads to start running and wait for them to finish.
    
    ComputeDirectTask task(*this);
    threads.execute(task);
    threads.waitForThreads();
    
    // Combine the energies from all the threads.
    
    if (totalEnergy != NULL) {
        double directEnergy = 0;
        int numThreads = threads.getNumThreads();
        for (int i = 0; i < numThreads; i++)
            directEnergy += threadEnergy[i];
        *totalEnergy += directEnergy;
    }
}
    static void NO_INLINE execute(const Source & data, size_t num_threads, std::vector<std::unique_ptr<Map>> & results,
                        Creator && creator, Updater && updater,
                        ThreadPool & pool)
    {
        results.reserve(num_threads);
        for (size_t i = 0; i < num_threads; ++i)
            results.emplace_back(new Map);

        for (size_t i = 0; i < num_threads; ++i)
        {
            auto begin = data.begin() + (data.size() * i) / num_threads;
            auto end = data.begin() + (data.size() * (i + 1)) / num_threads;
            auto & map = *results[i];

            pool.schedule([&, begin, end]()
            {
                for (auto it = begin; it != end; ++it)
                {
                    typename Map::iterator place;
                    bool inserted;
                    map.emplace(*it, place, inserted);

                    if (inserted)
                        creator(place->second);
                    else
                        updater(place->second);
                }
            });
        }

        pool.wait();
    }
int main(int argc, char **args) {
    int ret = log_init("./conf", "simple_log.conf");
    if (ret != 0) {
        printf("log init error!");
        return 0;
    }
    if (argc < 2) {
        LOG_ERROR("usage: ./http_multi_thread_demo [port]");
        return -1;
    }

    pthread_key_create(&g_tp_key,NULL);
    
    ThreadPool tp;
    tp.set_thread_start_cb(test_start_fn);
    tp.set_thread_exit_cb(test_exit_fn);
    tp.set_pool_size(4);

    HttpServer http_server;
    http_server.set_thread_pool(&tp);

    http_server.add_mapping("/hello", hello);

    http_server.add_bind_ip("127.0.0.1");
    http_server.set_port(atoi(args[1]));
    http_server.set_backlog(100000);
    http_server.set_max_events(100000);
    http_server.start_async();
    //sleep(1);
    http_server.join();
    return 0;
}
示例#14
0
//-----------------------------------------------------------------------------
TEST(ThreadPool, Sequential_WithStdQueue) {
  // add N jobs to the queue, and wait until all will be completed

  ThreadPool pool;

  std::queue<std::future<int>> queue;

  const int repetitions = 100;

  for (int i = 1; i <= repetitions; i++) {
    queue.push(std::move(pool.process([i]() {
      std::this_thread::sleep_for(std::chrono::milliseconds(20));
      return i;
    })));
  }

  int sum_actual = 0;
  while (!queue.empty()) {
    sum_actual += queue.front().get();
    queue.pop();
  }

  auto sum = [](int n) -> int { return n * (n + 1) / 2; };

  ASSERT_EQ(sum_actual, sum(repetitions));
}
示例#15
0
void DoObjectLockTest( void )
{
    cout << "Starting DoObjectLockTest" << endl;

    LockableObjects & objects = GetLockableObjects();
    objects.reserve( ObjectCount );
    for ( unsigned int ii = 0; ii < ObjectCount; ++ii )
    {
        LockableObject * object = new LockableObject( ii );
        objects.push_back( object );
    }

    {
        ThreadPool pool;
        pool.Create( ThreadCount, &RunObjectTest );
        pool.Start();
        pool.Join();
    }

    unsigned int totalFails = 0;
    for ( unsigned int ii = 0; ii < ThreadCount; ++ii )
    {
        const unsigned int failCount = FailCounts[ ii ];
        ::Loki::Printf( "Thread: [%u]  Failures: [%u]\n" )( ii )( failCount );
        totalFails += failCount;
    }
    const char * result = ( 0 == totalFails ) ? "Passed" : "FAILED";

    cout << "Finished DoObjectLockTest.  Total Fails: " << totalFails << "  Result: "
        << result << endl;
}
示例#16
0
    void run()
    {
        std::mt19937 generator(randomSeed());
        std::uniform_int_distribution<size_t> distribution(0, queries.size() - 1);

        for (size_t i = 0; i < concurrency; ++i)
            pool.schedule(std::bind(&Benchmark::thread, this, connections.get()));

        InterruptListener interrupt_listener;
        info_per_interval.watch.restart();
        delay_watch.restart();

        /// Push queries into queue
        for (size_t i = 0; !max_iterations || i < max_iterations; ++i)
        {
            size_t query_index = randomize ? distribution(generator) : i % queries.size();

            if (!tryPushQueryInteractively(queries[query_index], interrupt_listener))
                break;
        }

        shutdown = true;
        pool.wait();
        info_total.watch.stop();

        if (!json_path.empty())
            reportJSON(info_total, json_path);

        printNumberOfQueriesExecuted(info_total.queries);
        report(info_total);
    }
示例#17
0
unsigned RainbowTable::findPasswordByHash(unsigned* hash) {
    int i;
    Chain resultChain;
    vector<vector<string> > result;

    ThreadPool* myPool = new ThreadPool(THREADS_COUNT);
    myPool->initializeThreads();

    for (i = CHAIN_LENGTH - 1; i >= 0; --i) {

        PasswordFindWorkerThread* myThread =
                new PasswordFindWorkerThread(i, hash);
        myPool->assignWork(myThread);

        if (PasswordFindWorkerThread::result != 0) {
            break;
        }

    }

    myPool->destroyPool(1);
    delete myPool;

    printf("false alarm count: %d\n",
            PasswordFindWorkerThread::falseAlarmCount);

    if (PasswordFindWorkerThread::result != 0) {
        return PasswordFindWorkerThread::result;
    }

    return 0;
}
示例#18
0
int main (int argc, const char * argv[])
{
	//Create a new thread pool
	ThreadPool pool;

	//Get the number of threads, this defaults to the number of threads on your machine
	const unsigned int numThreads = pool.getThreadPoolSize();

	//Set the number of tasks that we want to pass to the thread pool
	const unsigned int numTasks = 100;

	//Setup a vector to store the results
	std::vector< double > results( numTasks );
	std::vector< std::future< void > > status( numTasks );

	//Add some tasks to the pool
	for(unsigned int i=0; i<numTasks; i++){
		status[i] = pool.enqueue( task, 99999, results[i] );
	}

	//Wait for the tasks to complex
	for(unsigned int i=0; i<numTasks; i++){
		status[i].wait();
	}

	//Get the maximum value across all the task results
	double maxValue = results[0];
	for(unsigned int i=1; i<numTasks; i++){
		if( results[i] > maxValue ) maxValue = results[i];
	}

	infoLog << "maximum value: " << maxValue << std::endl;
	
    return EXIT_SUCCESS;
}
FoundPoint percolate (const int size, ThreadPool& pool) {

	std::vector<std::future<FoundPoint>> points;
	FoundPoint max = FoundPoint();
	int numThreads = pool.getSize();
	int numOpThreadM = size/ numThreads;
	int numOpThreadR = size % numThreads;
	bool end = false;  

	for (int i = 0; i < numThreads; ++i) {
            
            int startIndex = numOpThreadM * i;
            int lastIndex = numOpThreadM * (i + 1);

            if ((i + 1 == numThreads && numOpThreadR > 0) 
            	|| numOpThreadM == 0) {
             	lastIndex += numOpThreadR;
                end = true;
            }    

            points.emplace_back(pool.enqueue_return(calc, startIndex, 
            	lastIndex, size));
            
            if (end) break;
    }

    for (auto& f: points) {
    	FoundPoint tmpFP = f.get();
    	if (tmpFP.value < max.value){
    		max = tmpFP;
    	}
    }

	return max;
}
int main()
{
    std::string input;
    std::string output;
    double score;
    
    corrector * corr = new corrector();
    
    std::string cFile = "../Training/PrideAndPrejudice/PrideAndPrejudice.txt";
    std::string tFile = "../Training/PrideAndPrejudice/tess2.txt";
    
    corr->loadDictionary("../Dictionary/unigrams.txt");
    corr->loadErrors("../trained5.txt");
    //corr->learn(tFile, cFile);
    //std::cout<<"learned"<<std::endl;
    //corr->writeErrors("trained5.txt");
    
    sqlite3 *db;
    int rc;
    rc = sqlite3_open("../Dictionary/BigramDatabase.db", &db);
    if (rc)
    {
        std::cout<<"Can't open database"<<std::endl;
        return 1;
    }
    
    std::string first = "0BEGIN.0";
    double confidence;
    
    ThreadPool tpool (4);
    
    while (std::cin>>input)
    {
        //wordList = new std::list<entry>();
        
        //corr->fillPossibleWordListLin(wordList, input, -20);
        
        //entry e = wordList->front();
        
        
        output = correct(input, corr, first, db, &tpool);
        std::cout<<"returned"<<std::endl;
        std::cout<<output<<std::endl;
        
        /*output = Viterbi3( input, corr, db, 56000000, 50, 300);
        std::cout<<"returned"<<std::endl;
        std::cout<<output<<std::endl;*/
        
        std::stringstream ss (output);
        while (ss>>first);
        
        //delete wordList;
    }
    
    tpool.shutdown();
    
    delete corr;
    return 0;
}
示例#21
0
	void __declspec(dllexport) GetSleep(LPVOID poolarg, LPVOID vectargs)
	{
		ThreadPool* pool =(ThreadPool*) poolarg;
		std::vector<std::wstring>* vect = (std::vector<std::wstring>*) vectargs;
		int count = _wtoi(vect->at(0).data());
		for (int i = 0; i< count; i++)
			pool->AddTask(WorkerGotDream,NULL);
	}
void signalHandler(int sig) {
	ThreadPool* pool = ThreadPool::Instance();

	// JoinAll will deal with the memory de-allocation
	// if all the threads are safely quited.
	// so calling CancelAll here is fine
	pool->CancelAll();
}
 void TaskBlock::execute(ThreadPool& pool)
 {
   for(auto&&task : tasks)
     pool.schedule_task(task);
   
   pool.assist_until([this](){return done();});
   //pool.print_accounts();
 }
示例#24
0
void *ThreadPool::threadFunc(void *args)
{
	ThreadPool *pool = (ThreadPool *)args;
	Task *task;
	while (task = pool->getTask()) {
		task->taskFunc(task->objArg, task->args);
		delete task;
	}
}
示例#25
0
 void Scene::computeColors(std::function<void(int, int, ColorCRef)> paint, int threadCount) {
     figuresKDTree.init(figures);
     upto(threadCount, 1);
     downto(threadCount, 4);
     int taskCount = 10;
     ThreadPool pool;
     for (int t = 0; t < taskCount; t++)
         pool.addTask([this, &paint, t, taskCount]() { multiThreadComputePartOfColors(paint, t, taskCount); });
     pool.executeAll(threadCount);
 }
示例#26
0
//-----------------------------------------------------------------------------
TEST(ThreadPool, ProcessJob) {
  ThreadPool pool;

  std::future<int> r = pool.process([]() {
    std::this_thread::sleep_for(std::chrono::milliseconds(1));
    return 42;
  });

  ASSERT_EQ(42, r.get());
}
示例#27
0
int main() {
    ThreadPool<Thread> *threadPool = new ThreadPool<Thread>(3);
    for (int i = 0; i < 999; ++ i) {
        usleep(1000 * 1000);  
        cout<<"add Task " << endl;
        threadPool->addTask();
    }
    //threadPool->stop();
    delete threadPool;
}
示例#28
0
文件: client.cpp 项目: OspreyHub/ATCD
int
ACE_TMAIN(int argc, ACE_TCHAR *argv[])
{
  try
    {
      CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);

      if (parse_args (argc, argv) != 0)
        return 1;

      CORBA::Object_var poa_object = orb->resolve_initial_references("RootPOA");

      if (CORBA::is_nil (poa_object.in ()))
        ACE_ERROR_RETURN ((LM_ERROR,
                           " (%P|%t) Unable to initialize the POA.\n"),
                          1);

      PortableServer::POA_var root_poa = PortableServer::POA::_narrow (
      poa_object.in ());
      PortableServer::POAManager_var poa_manager =
      root_poa->the_POAManager();

      Subscriber_impl subscriber(orb.in ());
      PortableServer::ObjectId_var id =
      root_poa->activate_object (&subscriber);

      CORBA::Object_var object = root_poa->id_to_reference (id.in ());

      Subscriber_var subscriber_var = Subscriber::_narrow (object.in ());

      object = orb->string_to_object(ior);
      Publisher_var publisher = Publisher::_narrow(object.in());

      publisher->subscribe(subscriber_var.in());

      poa_manager->activate();

      ThreadPool pool (orb.in ());
      if (pool.activate(THR_NEW_LWP | THR_JOINABLE, 5) != 0)
        ACE_ERROR_RETURN ((LM_ERROR,
                           "Cannot activate client threads\n"),
                          1);

      pool.thr_mgr ()->wait ();

      ACE_DEBUG ((LM_DEBUG, "event loop finished\n"));
    }
  catch (const CORBA::Exception& ex)
    {
      ex._tao_print_exception ("Exception caught:");
      return 1;
    }

    return 0;
}
示例#29
0
int main()
{
	Util::Title("Enqueued Work Items"); 

	std::cout << "\n  main thread id = " << std::this_thread::get_id();

	ThreadPool<WorkResult> processor;
	processor.start();

	// define 1st work item

	WorkItem<WorkResult> wi1 = []() {
		std::cout << "\n  working on thread " << std::this_thread::get_id();
		return "Hello from wi1";
	};

	processor.doWork(&wi1);

	// define 2nd work item

	WorkItem<WorkResult> wi2 = []()
	{
		std::cout << "\n  working on thread " << std::this_thread::get_id();
		size_t sum = 0;
		for (size_t i = 0; i < 100000; ++i)
			sum += i;
		return "wi2 result = " + Utilities::Converter<size_t>::toString(sum);
	};

	processor.doWork(&wi2);

	// the following calls to result() block until results are enqueued

	// define 3rd work item

	WorkItem<WorkResult> wi3 = []() {
		std::cout << "\n  working on thread " << std::this_thread::get_id();
		return "Hello from wi3";
	};

	processor.doWork(&wi3);

	std::cout << "\n  " << processor.result();
	std::cout << "\n  " << processor.result();
	std::cout << "\n  " << processor.result();
	processor.doWork(nullptr);
	processor.doWork(nullptr);
	processor.doWork(nullptr);


	// wait for child thread to complete

	processor.wait();
	std::cout << "\n\n";
}
示例#30
0
void* start_thread(void* arg)
{
    ThreadPool* tp = (ThreadPool *)arg;
    if (tp->m_scb != NULL) {
        tp->m_scb();
    } else {
        LOG_DEBUG("thread start cb is null");
    }
    tp->execute_thread();
    return NULL;
}