コード例 #1
0
	void deposit(string rec) {
		while (count == lenght - 1) {
			not_full.wait();
		}
		
		buffer[rear] = rec;
		if (rear < lenght)
			rear++;
		if (rear == lenght)
			rear = 0;
		count++;
		not_empty.post();
	}	
コード例 #2
0
	string fetch() {
		while (count == 0) {
			not_empty.wait();
		}
		string result = buffer[front];

		if (front < lenght)
			front = (front + 1) % lenght;
		if (front == lenght)
			front = 0;
		count--;
		not_full.post();
		//buffer[front] = "";
		return result;
	}
コード例 #3
0
ファイル: sqlite.cpp プロジェクト: omniis-jay/nmbls
void sqliteworker(void)
{
    while(!done)
    {
        sign.wait();
        int nexttoworkon;
        while (workqueue.pop(nexttoworkon))
        {
            auto workingon = requests.begin() + nexttoworkon;
            std::string sqlfile = workingon->hostroot +  "data.sqlite";
            std::string sqlinitfile = workingon->hostroot +  "data.init";

            void (*addJsonRowFunction)(std::ostringstream &output, int row, sqlite3_stmt *stmt) = addJsonTableRow;
            void (*finiJsonFunction)(std::ostringstream &output, bool haverows) = finiJsonTableRow;

            try
            {
                workingon->params.at("formatdatatable");
                addJsonRowFunction = addGDataTableRow;
                finiJsonFunction = finiGDataTable;
            }
            catch (const std::out_of_range &x)
            {

            }

            boost::posix_time::ptime starttime;
            measureuSTime(starttime, is_running_as_debug);

            sqlite3 *dbhandle = NULL;
            try
            {
                dbhandle = dbhandles.at(workingon->host);
            }
            catch(const std::out_of_range &x)
            {
                bool needscreating = false;
                if ( !boost::filesystem::is_regular_file(sqlfile) )
                {
                    needscreating = true;
                }

                std::cout << "Need to open db" << std::endl;
                if ( SQLITE_OK == sqlite3_open_v2(sqlfile.c_str(),  &dbhandle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL))
                {
                    dbhandles[workingon->host] = dbhandle;
                    dbhandle = dbhandle;

                    if (needscreating)
                    {
                        char *errmsg = 0;
                        std::ifstream insql(sqlinitfile);
                        if ( !insql.fail())
                        {
                            std::string createsql(static_cast<std::stringstream const&>(std::stringstream() << insql.rdbuf()).str());
                            int rc = sqlite3_exec(dbhandle, createsql.c_str(), NULL, 0, &errmsg);
                            if( rc != SQLITE_OK )
                            {
                                std::cerr << "Error running SQL: " <<  errmsg <<std::endl;

                                std::ostringstream doc;
                                genHttpDoc("", doc);

                                workingon->postResponceCallback("500", errmsg, doc.str());
                                sqlite3_free(errmsg);

                                sqlite3_close_v2(dbhandle);
                                backlog--;
                                continue;
                            }

                            measureuSTime(starttime, is_running_as_debug, "Time taken (sqlite3_exec - init)");
                        }
                    }
                }
            }

            if ( NULL != dbhandle )
            {
                measureuSTime(starttime, is_running_as_debug, "Time taken (sqlite3_open_v2)");

                try
                {
                    std::string insql;
                    readFile(workingon->filename, insql);

                    measureuSTime(starttime, is_running_as_debug, "Time taken (read sql)");

                    sqlite3_stmt *stmt;
                    const char *sqlltail;
                    int rc = sqlite3_prepare_v2(dbhandle, insql.c_str(), insql.size(), &stmt, &sqlltail);

                    measureuSTime(starttime, is_running_as_debug, "Time taken (sqlite3_prepare_v2)");

                    if( rc != SQLITE_OK )
                    {
                        std::string errormsg = "{error: \"";
                        errormsg += "Error preparing SQL statement";

                        std::cerr << "Error preparing: " << insql << std::endl;

                        errormsg +=  "\"}";

                        std::ostringstream doc;
                        genHttpDoc("", doc);

                        workingon->postResponceCallback("200", "OK", doc.str());
                    }
                    else
                    {
                        getAndBindParamsFromSql(insql, stmt, workingon);

                        measureuSTime(starttime, is_running_as_debug, "Time taken (bind)");

                        std::ostringstream jsondoc;
                        bool haverows = false;

                        // Have some sencible limit
                        for ( int row = 0; row < 10000; row++)
                        {
                            int step_result = sqlite3_step(stmt);
                            if ( SQLITE_ROW != step_result )
                            {
                                // Probably SQLITE_DONE
                                break;
                            }

                            haverows = true;
                            addJsonRowFunction(jsondoc, row, stmt);
                            //addGDataTableRow(jsondoc, row, stmt);
                        }
                        sqlite3_finalize(stmt);

                        //finiGDataTable(jsondoc, haverows);
                        finiJsonFunction(jsondoc, haverows);

                        std::ostringstream doc;
                        genHttpDoc(jsondoc.str(), doc);

                        workingon->postResponceCallback("200", "OK", doc.str());
                    }
                }
                catch (std::ifstream::failure e)
                {
                    std::cerr << "Exception opening/reading/closing file: " << workingon->filename << std::endl;
                }
            }
            else
            {
                workingon->postResponceCallback("404", "Not found", "Something bad just happened...");
            }

            //sqlite3_close_v2(dbhandle);
            backlog--;
        }
    }
}
コード例 #4
0
ファイル: boosthelp.cpp プロジェクト: peidright/devshare
void worker(const std::string& s) 
{
	g_iosem.wait();
	std::cout<<s<<std::endl;
}