void loadVector(graph &pass, string name)
{
	ifstream fin;
	fin.open(name.c_str());
	string temp;
	string xx,yy;
	int size = 0;
	bool first = true, xflag = false, yflag = false;
	if(name.size() != 0)
	{
		while(!fin.eof())
		{
			temp = "";
			fin >> temp;
			if(temp.size()!= 0)
			{
				first = true;
				xx=""; 
				yy="";
				size = temp.size();
				for (int x = 0; x < size; ++x)
				{
					if(temp.at(x) == ',')
					{
						first = false;
					}
					if(temp.at(x) == '-')
					{
						if(first)
						{
							xflag = true;
						}
						else
						{
							yflag = true;
						}	
					}	
					if(temp.at(x)-48 > -1 && temp.at(x)-48 < 10) // checks if it is a number
					{
						if(first)
						{
							xx += temp.at(x);
						}
						else
						{
							yy += temp.at(x);
						}
					}
				}
				//cout << xx << " " << yy << endl;

				pass.push(stringToInt(xx,xflag), stringToInt(yy,yflag));
				xflag = false;
				yflag = false;

			}
		}
	}
	else 
예제 #2
0
파일: graph.cpp 프로젝트: mohan82/jparksrc
int t_thread_bfs_1()
{
    // setup graph
    static list<int> fnode;
    static graph g(5);
    static map<int, int> xmap;
    struct thread_bfs *thr_bfs;

    numTerm = 0;
    int numCre = 0;
    
    g.push(1, 2, 0);
    g.push(1, 4, 0);
    g.push(1, 5, 0);
    g.push(2, 3, 0);
    g.push(3, 5, 0);
    g.push(4, 3, 0);
    g.push(4, 5, 0);

    pthread_t thr;
    // main thread is waiting for the list to be updated, and
    fnode.push_back(1);
    xmap[1]++; // mark visited. Otherwise you might end up with revisit the node
    
    bool done = false;
    
    while (!done) {
        
        int err = pthread_mutex_lock(&mtx);
        if (err)
            cout << " error in mutex_lock " << endl;

        // if work to do in the Q then create a thread do a task
        // Or, I can create as much tasks as possible by taking all the tasks in the Q
        if (!fnode.empty()) {
            int n = fnode.front();
            fnode.pop_front();
            cout << endl << "visited " << n << endl;
            // setup data for the worker thread to access
            thr_bfs = new struct thread_bfs;
            thr_bfs->pfnode = &fnode;
            thr_bfs->pg = &g;
            thr_bfs->pmap = &xmap;
            thr_bfs->node = n;
            int err = pthread_create(&thr, NULL, threadFunc1, thr_bfs); // at the moment, I am not worry about joining the created thread.
            if (err)
                cout << " error in creating thread " << endl;
            numCre++; // num created threads

        } else if (numCre > numTerm) {
            // we do not have anything in the Q but worker threads are pending
            // wait for signal from the thread to avoid busy waiting
            cout << endl << "wait for new Data" << endl;
            
            err = pthread_cond_wait(&cond, &mtx);
            if (err)
                cout << " error in cond_wait " << endl;

        } else if (numCre == numTerm) {
            done = true; // if all threads are terminated and nothing to do more.
            cout << endl << "processed all" << endl;
        } else {
            cout << "error" << endl;
        }

        err = pthread_mutex_unlock(&mtx);
        if (err)
            cout << " error in mutex_unlock " << endl;

    }

}